It's relatively simple using grid.arrange
in the gridExtra
package to arrange multiple plots in a matrix, but how can you arrange plots (the ones I'm working on are from ggplot2
) when some plots are intended to be larger than others? In base, I can use layout()
such as in the example below:
nf <- layout(matrix(c(1,1,1,2,3,1,1,1,4,5,6,7,8,9,9), byrow=TRUE, nrow=3))
layout.show(nf)
what is the equivalent for ggplot
plots?
Some plots for inclusion
library(ggplot2)
p1 <- qplot(x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg", data=mtcars)
p2 <- qplot(x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp", data=mtcars)
p3 <- qplot(wt,data=mtcars)
p4 <- qplot(wt,mpg,data=mtcars,geom="boxplot")
p5 <- qplot(wt,data=mtcars)
p6 <- qplot(mpg,data=mtcars)
p7 <- qplot(disp,data=mtcars)
p8 <- qplot(disp, y=..density.., geom="density", data=mtcars)
p9 <- qplot(mpg, y=..density.., geom="density", data=mtcars)
I like the interface provided by the
lay_out
function (formerly in thewq
package) . It takes arguments of the formlist(plot, row(s), column(s))
. For your example:Which yields:
(Code sourced from a prior version of the
wq
package, from the commit history on the unofficial Github CRAN mirror.)You can use the same matrix interface as layout with
grid.arrange
,and the same works for ggplots; note that NA can be used to indicate blank cells. The result is a gtable, compatible with
ggsave()
.An alternative with gtable
You can use nested
arrangeGrob
calls like this example:Edit:
I appreciate all the other answers, but Didzis Elferts's comment on the OP connected to the answer that I found easiest to implement.