有没有什么方法可以用三行一层一层地注释 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
请注意 atop
和 textstyle
来定位文本,同时保持其大小相同,并使用 scriptscriptstyle("")
来控制间距行与行之间。您还可以根据需要使用 scriptstyle
或 scriptscriptstyle
使用不同的文本相对大小,当然,在主题
部分
关于R ggplot 使用三个值和 bgroup 用 atop 进行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577023/