8

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!

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Jin peng
  • 83
  • 1
  • 1
  • 3

1 Answers1

21

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
Andrew Taylor
  • 3,438
  • 1
  • 26
  • 47