knitr:禁止在do.call时立即绘图(grid.arrange,list_of_plots)

时间:2016-03-13 10:30:59

标签: r ggplot2 latex knitr do.call

我有一个两行的数字,由第一行的情节和第二行的情节列表组成。

knitr我做

\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}

产生

enter image description here

问题在于我do.call我的图表列表,它会立即向图形设备发送图表。

是否可以在knitr或在将do.call传递到grob时避免python manage.py changepassword mickey 吐出图片时对此进行修复?

1 个答案:

答案 0 :(得分:4)

使用??grid.arrange,我们会找到arrangeGrob的帮助页面。这指定了以下内容:

  
      
  • arrangeGrob:返回没有绘图的grob
  •   
  • grid.arrange:在当前设备上绘图
  •   
  • marrangeGrob:可以在多个页面上发送的arrangeGrob接口
  •   

因此,解决方案是使用arrangeGrob代替grid.arrange

另一个好处是可以使用grobs参数传递一个grob列表,这样我们就可以取消do.call构造。