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

时间:2016-03-14 07:23:50

标签: r plot ggplot2 title r-grid

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

有四个地块:

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.arrange()中使用grid.Extra函数:

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

raw graph

现在,我想在这些图中的每个图下方添加标题,如下图所示(我在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中执行它,标题在每个图下面,我该如何实现它?谢谢!

1 个答案:

答案 0 :(得分: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