首页 文章

在图表下方添加 Headers ,在ggplot中添加四个图表

提问于
浏览
6

由于数据的隐私性,我使用 ggplot2 中的 mtcar 数据集来解释我的问题 .

有四个图:

g1 <- ggplot(mtcars,aes(mpg,wt)) + geom_point()
g2 <- ggplot(mtcars,aes(mpg,disp)) + geom_point()
g3 <- ggplot(mtcars,aes(mpg,drat)) + geom_point()
g4 <- ggplot(mtcars,aes(mpg,qsec)) + geom_point()

我想把这四个图放在一个图中,所以我在包 grid.Extra 中使用 grid.arrange() 函数:

grid.arrange(g1,g2,g3,g4,ncol=2)

raw graph

现在,我想在这些图中的每个图下面添加 Headers ,如下图所示(我在Word中修改它,所以它不漂亮)
modified pic
在询问之前,我已经在 SO 中搜索了,我知道如何添加例如,使用 grid.text() 或这三种方法Displaying text below the plot generated by ggplot2element_text(vjust=-10) ,但是我无法将其应用于一个图中的四个图 . 同时,我在基础图How to add a title to each plot in R?Common main title of a figure panel compiled with par(mfrow)中得到了一些结果,qustion是我想在 ggplot2 中执行它并且 Headers 在每个图下方,我该如何实现它?谢谢!

1 回答

  • 7

    你可以先用arrangeGrob包装每个情节,

    g1 <- g2 <- g3 <- g4 <- ggplot()
    titles = LETTERS[1:4]
    plots = mapply(arrangeGrob, list(g1,g2,g3,g4), 
                   bottom = titles, SIMPLIFY=FALSE)
    grid.arrange(grobs = plots, ncol=2)
    

    enter image description here

相关问题