I'm using the grid package to display an array of plots like this:
layout <- grid.layout(2, 4)
pushViewport(viewport(layout = layout))
# print various plots
Is there some way to specify a title for the whole grid layout?
Another way:
library(gridExtra)
g = rectGrob() # dummy "plot"
grid.arrange(g, g, g, g, ncol=2, top = "Main Title")
Dummy example based on a similar SO question: Place title of multiplot panel with ggplot2
First create a layout with the required number of rows + 1 short one for title:
pushViewport(viewport(layout = grid.layout(3, 2, heights = unit(c(0.5, 5, 5), "null"))))
Create some plots there:
print(ggplot(mtcars, aes(hp)) + geom_histogram(), vp = viewport(layout.pos.row = 2, layout.pos.col = 1:2))
print(ggplot(mtcars, aes(wt)) + geom_histogram(), vp = viewport(layout.pos.row = 3, layout.pos.col = 1))
print(ggplot(mtcars, aes(mpg)) + geom_histogram(), vp = viewport(layout.pos.row = 3, layout.pos.col = 2))
Add a title to the top row:
grid.text("MAIN TITLE", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
Resulting in: