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

10,421

I personally like this solution provided by cowplot maintainers on github:

Make two plots

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

Use cowplot::plot_grid to combine the plots

p <- plot_grid(p1, p2, labels=c('A', 'B'))

Make a title

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

Add title

plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1)) # rel_heights values control title margins
Share:
10,421
Admin
Author by

Admin

Updated on June 19, 2022

Comments

  • Admin
    Admin almost 2 years

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

    Does anybody know this and can help me out? Many thanks!

  • Admin
    Admin almost 8 years
    It works for me!!!! Many thanks!