【问题标题】:R ggplot annotated with atop using three values and bgroupR ggplot 使用三个值和 bgroup 用 atop 注释
【发布时间】:2017-03-27 09:33:45
【问题描述】:

有没有办法用三行在另一行上注释 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 中的所有三行,但我无法正确粘贴。如果有什么我可以尝试的,请指教。非常感谢!

【问题讨论】:

  • 分享一个可重现的数据示例!

标签: r ggplot2 plotmath geom-text


【解决方案1】:

为了使所有内容完美居中(\n 不会这样做),无论行数如何,都保持每段文本的大小相同,同时能够调整行间距,改用这个:

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

然后使用labeller=label_parsed

这也适用于facet_grid、title 和ylab

注意atoptextstyle 用于定位文本,同时保持其大小相同,scriptscriptstyle("") 用于控制行间距。您还可以根据需要使用 scriptstylescriptscriptstyle 使用不同的相对大小的文本,当然在 theme 部分中使用 element_text(size=whatevernumber)

【讨论】:

  • @user971102 我鼓励这个答案被接受
猜你喜欢
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 2022-06-21
  • 2013-11-25
  • 2017-06-17
相关资源
最近更新 更多