R ggplot 使用三个值和 bgroup 用 atop 进行注释

标签 r ggplot2 plotmath geom-text

有没有什么方法可以用三行一层一层地注释 ggplot,同时保持三行的文本大小相同?

我已经快到了,但第三行的文本大小不同,看起来 bgroup 仅使用前两行,但我无法正确理解...

我将文本添加到“?plotmath”中的“atop”应用程序中,效果很好,但与其他两行相比,第三行中的文本大小不同......

library(ggplot2)
line1 = "xxx data1"
line2 = "yyy data2"
line3 = "zzz data3"

df=data.frame(x=rep(1:8, 3), y=c(0,1,3,4,5,6,7,8, 8,7,6,3,2,1,3,4, 0,2,4,5,6,7,8,9), variable=c("x", "x","x","x","x","x","x","x","y","y","y","y","y","y","y","y","z","z","z","z","z","z","z","z"))
p <-  ggplot(df) + theme_bw() + geom_point(aes(x=x,y=y, color=variable)) + geom_line(aes(x=x,y=y, color=variable)) + 
geom_text(x=max(df$x), y = max(df$y), label = paste('bgroup("{", atop(atop("',line1,'","',line2,'"),"', line3,'"), "}")',sep=''), size=3.5,parse=TRUE)

我以为我得到了 bgroup 中的所有三行,但我无法正确粘贴。如果有什么我可以尝试的,请告知。非常感谢!

最佳答案

要使所有内容完美居中(\n 不会这样做),请使每段文本保持相同的大小,无论行数多少,同时能够调整行间空间,使用这个代替:

xlab(expression(atop(textstyle("whateverline1"),atop(textstyle("whateverline2"),atop(scriptscriptstyle(""),textstyle("whateverline3"))))))

然后使用labeller=label_parsed

这也适用于facet_grid、标题和ylab

请注意 atoptextstyle 来定位文本,同时保持其大小相同,并使用 scriptscriptstyle("") 来控制间距行与行之间。您还可以根据需要使用 scriptstylescriptscriptstyle 使用不同的文本相对大小,当然,在主题部分

关于R ggplot 使用三个值和 bgroup 用 atop 进行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577023/

相关文章:

r - 带渐变色/透明字的GGWordcloud(GGPlot Wordcloudgradient with adjustmentcolor)

r - 在 facet_wrap 图中添加 "floating"轴标签

r - 在 ggplot2 标题表达式中使用带有变量的 italic()

r - map 轴标签中的度数符号不正确

r - ggplot2:如何更改条形/列几何形状中颜色美感的宽度?

r - 在 Rmarkdown 中执行 Perl 6 代码

r - 绘制堆积条形图?

r - 需要计算 R 中向量中特定转换的数量

r - 使用 ggplot 构建聚类图

r - 关于 ggplot 注释的更好解决方案?