How to add a title for a grid.layout figure in ggplot2?

Jin*_*eng 2 r ggplot2

I am using grid.layout() function to arrange a number of plots in one single figure. However, I do not know how to add a main title for the whole figure (at the mid-top of the figure)?

有人知道这一点并且可以帮助我吗?非常感谢!

And*_*lor 5

我个人喜欢cowplot维护者在github上提供的此解决方案:

做两个情节

p1 <- ggplot(mtcars, aes(x=disp, y=mpg)) + geom_point(colour = "blue") + background_grid(minor='none')
p2 <- ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point(colour = "green") + background_grid(minor='none')
Run Code Online (Sandbox Code Playgroud)

使用cowplot::plot_grid该地块合并

p <- plot_grid(p1, p2, labels=c('A', 'B'))
Run Code Online (Sandbox Code Playgroud)

命名

title <- ggdraw() + draw_label("MPG declines with displacement and horsepower", fontface='bold')
Run Code Online (Sandbox Code Playgroud)

添加标题

plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1)) # rel_heights values control title margins
Run Code Online (Sandbox Code Playgroud)