Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Grid layout title

Tags:

r

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?

like image 821
nccc Avatar asked May 27 '12 18:05

nccc


2 Answers

Another way:

library(gridExtra)
g = rectGrob() # dummy "plot"
grid.arrange(g, g, g, g, ncol=2, top = "Main Title")
like image 185
baptiste Avatar answered Oct 08 '22 10:10

baptiste


Dummy example based on a similar SO question: Place title of multiplot panel with ggplot2

  1. 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"))))   
    
  2. 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))
    
  3. Add a title to the top row:

    grid.text("MAIN TITLE", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
    

Resulting in:

enter image description here

like image 29
daroczig Avatar answered Oct 08 '22 10:10

daroczig