R ggplot使用三个值和bgroup在顶部注释 [英] R ggplot annotated with atop using three values and bgroup

查看:159
本文介绍了R ggplot使用三个值和bgroup在顶部注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在三行文字之间保持相同的大小,同时在三行文字之间进行注释?

Is there any way to annotate a ggplot with three lines one on top of the other, while keeping the text sizes the same across the three lines?

我快到了,但是第三行中的文本大小不同,看来bgroup只使用了前两行,而我无法正确使用...

I am almost there but the text sizes are different in the third line, it looks like the bgroup is only using the first two lines and I cannot get this right...

我正在将文本使用添加到?plotmath"中的顶部"应用程序中,它可以正常工作,但第三行中的文本与其他两行中的文本大小不同...

I am adding the text using to "atop" applications from "?plotmath", which works fine but the text in the third line comes out in a different size compared to the other two lines...

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中获得了所有三行,但是我无法正确粘贴.如果有什么我可以尝试的,请指教.非常感谢!

I thought I was getting all the three lines within the bgroup, but I cannot get the paste right. If there is anything I can try please advise. Thanks very much!

推荐答案

要完美地使所有内容居中(\n不会做),无论行数多少且同时使每条文本的大小相同能够调整线性空间的方法,请改用此方法:

To perfectly center everything (which \n will not do), keep every piece of text the same size whatever the number of lines and at the same time being able to adjust the interlinear space, use this instead:

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

然后使用labeller=label_parsed

这也适用于facet_grid,标题和ylab

请注意atoptextstyle来放置文本,同时保持它们的大小相同,并scriptscriptstyle("")控制行之间的间距.您还可以根据需要使用scriptstylescriptscriptstyle使用不同的相对大小的文本,当然也可以在theme部分中使用element_text(size=whatevernumber)

Note the atop and textstyle to position the text whilst keeping it all the same size and the scriptscriptstyle("") to control spacing between lines. You can also use varied relative sizes of text using scriptstyle or scriptscriptstyle depending on your needs and of course use element_text(size=whatevernumber) in the theme section

这篇关于R ggplot使用三个值和bgroup在顶部注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆