多个“顶级”textGrob标题

时间:2016-11-28 22:35:37

标签: r gridextra r-grid

以下是我的问题的一个简单示例(请原谅重复的图表 - 不能使用我的实际数据)

示例:

#packages
library(grid)
library(gridExtra)
library(ggplot2)

#simple plot
p <- ggplot(mtcars, aes(wt,mpg))


# setting-up grid of plots...2 columns by 4 rows 
sample <- grid.arrange(p + geom_point()+labs(title="Sample \nTitle One"),
                 p + geom_point()+labs(title="Sample \nTitle Two"),
                 p + geom_point(),
                 p + geom_point(),
                 p + geom_point(),
                 p + geom_point(),
                 p + geom_point(),
                 p + geom_point(),
                 ncol = 2)

输出:

enter image description here

问题:前两个图已被压缩。我试图使用textGrob,如下所示:

top = textGrob("Sample Title One",hjust = 1,gp = gpar(fontfamily = "CM Roman", size = 12))

但是,我没有看到一种方法来合并两个单独的标题。我还没有尝试使用cowplot,这可能是一种更合理的方式,但是如果有一种方法可以使用textGrob来做这件事,我很好奇。

谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

如user20650所述,您可以执行以下操作:

 grid.arrange(arrangeGrob(p,p,p,p,top=textGrob("Sample Title One"),   
 ncol=1), arrangeGrob(p,p,p,p,top=textGrob("Sample Title Two"), ncol=1),   
 ncol = 2)

要获得以下内容: enter image description here