knitr: restraining from immediately plotting when do.call(grid.arrange, list_of_plots)

Viewed 235

I have a two-rows figure which is composed by a plot on the first row and by a list of plots on the second row.

With knitr I do

\documentclass{article}

\begin{document}

<<plots, fig.width='\\textwidth', echo = FALSE, fig.height=5, fig.width = 10, warning = FALSE>>=

require(ggplot2) 
plot <- ggplot() + geom_point(data=cars, aes(speed, dist)) + labs(x=NULL, y=NULL)

# create plot with function
makePlot <- function(myLabel) {
  ggplot() + geom_point(data=cars, aes(speed, dist)) + labs(x=NULL,y=NULL,title=myLabel)
}
list_plot <- lapply(c("one","two","three"), makePlot)

require(gridExtra)
grob <- do.call(grid.arrange, c(list_plot, nrow=1, ncol=3)) # here R sends the plots to the graphical device!

grid.arrange(plot, 
             grob,
             nrow=3)
@



\end{document}

which produces

enter image description here

The issue is that I do.call my list of plots, which immediately send to the graphical devices the plots.

Is there a fix to this, either in knitr or by avoiding do.call to spit the plot when passing it to grob?

1 Answers
Related