I have 12 plots generated by a loop and I want to plot them with 3 rows and 2 columns on one page (2 pages in total). I know how to do it in R
pdf("1.pdf")
par(mfrow = c(3, 2))
for (i in 1:12) {
x <- 1:10
y <- 2*x + rnorm(x)
plot(x, y)
}
dev.off()
But how to do this using ggplot
?
library(ggplot2)
library(grid)
library(gridExtra)
for (i in 1:12) {
x <- 1:10
y <- 2*x + rnorm(x)
qplot(x, y)
}
I think I have to use grid.arrange
but need some help on that. Thanks.
You might want to take a look at the
cowplot
package that allows more flexibility than just using a nakedgrid.arrange
.This works - albeit a bit inelegantly:
Another more elegant but somewhat obtuse way to do the
grid.arrange
is the following (thanks to Axeman and beetroot - note the comments).or this:
They all result in this - (think two of these - they look the same anyway):
marrangeGrob
is a convenient wrapper for multiple pages,or you can call
grid.arrange()
explicitly twice,