【问题标题】:Plot title at bottom of plot using ggplot2使用 ggplot2 在绘图底部绘制标题
【发布时间】:2015-05-04 09:36:37
【问题描述】:

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

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

将标题放在情节的中间和顶部,而我希望它位于情节的中间和底部

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    这是使用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))
    

    【讨论】:

    • 您必须直接打开/关闭设备:pdf("file.pdf"); print(qplot(...)); grid.text(...); dev.off()
    【解决方案2】:

    在开发版本中,您可以使用标题参数,

    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")
    

    【讨论】:

      猜你喜欢
      • 2018-01-02
      • 2016-01-02
      • 2017-09-27
      • 1970-01-01
      • 2017-04-02
      • 2023-03-22
      • 1970-01-01
      • 2022-08-13
      • 2018-04-24
      相关资源
      最近更新 更多