使用ggplot2在图底部显示绘图标题

15

在ggplot2中,如何将图表标题放在图表底部。

qplot(rnorm(100)) + ggtitle("My Title")

将标题放在图表的中间和顶部,但我希望它在图表的中间和底部


另一种解决方案是使用选项标签而不是标题。https://community.rstudio.com/t/figure-labels-or-captions-in-ggplot2/41067/3 - Lefty
2个回答

13

这里是使用 grid.text 的解决方案:

library("grid")
qplot(rnorm(100)) + theme(plot.margin=unit(c(0.5, 1, 2, 0.5), "lines"))
grid.text("My Title", x = unit(0.5, "npc"), y = unit(0, "npc"),
          vjust = -0.5, gp = gpar(cex=1.2))

plot


你必须直接打开/关闭设备:pdf("file.pdf"); print(qplot(...)); grid.text(...); dev.off() - rcs

13

使用开发版,您可以使用标题参数(caption argument)。

ggplot() + 
  labs(caption="Bottom Title") + 
  theme(plot.caption = element_text(hjust=0.5, size=rel(1.2)))

或者,将图形包装在grid.arrange()中,

gridExtra::grid.arrange(ggplot(), bottom="Bottom Title")

这里输入图片描述


我喜欢第一个解决方案,因为它使用了ggplot的原生功能。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接