I would like to create a pdf document using R markdown to display a series of graphs made using ggplot in a for loop. Is it possible to control the number of graphs on each page, so that, for example, there are 2 x 2 grids on the page?
I would like to maintain flexibility so that I can change the total number of graphs so that it is divided into the desired number of pages.
My attempt to use the ggplot2 and gridExtra packages is given below, but I cannot control the number of graphs per page, it seems to want to compress them only on the sinlge page. I tried to change the arguments ncol, nrow, height, widths in grid.arrange, but it doesn't seem to help.
---
title: "ggplot Layout"
output: pdf_document
---
```{r,figure2 fig.height=8, fig.width=6,echo=FALSE,message=FALSE}
library(ggplot2)
library(gridExtra)
graphlist<-list()
count <- 1
colnums<-c(1,5,6,7,8,9,10)
for (i in colnums) {
plot.x.name<-names(diamonds[i])
p <- ggplot(data=diamonds,aes_string(x = plot.x.name)) + geom_histogram()
graphlist[[count]]<-p
count <- count+1
}
do.call("grid.arrange",c(graphlist,ncol=2))
```
, , (
http://kbroman.imtqy.com/knitr_knutshell/pages/figs_tables.html), ggplot, .
---
title: "Example Layout"
output: pdf_document
---
```{r bunch_o_figs,fig.height=8, fig.width=6, message=FALSE,echo=FALSE}
n <- 100
x <- rnorm(n)
par(mfrow=c(2,2), las=1)
for(i in 1:20) {
y <- i*x + rnorm(n)
plot(x, y, main=i)
}
``