Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Plotting on a grid with fewer plots than viewports

library( ggplot2 )
library( gridExtra )
p1 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p2 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p3 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p4 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p5 <- qplot( rnorm( 10 ), rnorm( 10 ) )
grid.arrange( p1, p2, p3, p4, p5, nrow=2 )

I would like to center the bottom two plots. How can do this? I can do it using split.screen but I'm unable to figure out how to do this with ggplot2. ggplot2 has cooler graphics.

Thanks in advance!

PK

like image 287
polarise Avatar asked May 28 '13 15:05

polarise


1 Answers

You could use the gtable package for flexible and convenient grid layouts, or simply nest two arrangeGrobs,

ng = nullGrob()
grid.arrange(arrangeGrob(p1, p2, p3, nrow=1),
             arrangeGrob(ng, p4, p5, ng, nrow=1, widths=c(0.5, 1, 1, 0.5)),
             nrow=2)

enter image description here


Edit: For the bottom plots to span the full width, you simply need to remove the dummy nullGrobs() in the above solution:

grid.arrange(arrangeGrob(p1, p2, p3, nrow=1),
             arrangeGrob(p4, p5, nrow=1),
             nrow=2)

enter image description here

like image 98
baptiste Avatar answered Sep 18 '22 13:09

baptiste