【问题标题】:knitr: restraining from immediately plotting when do.call(grid.arrange, list_of_plots)knitr:禁止在 do.call(grid.arrange, list_of_plots) 时立即绘图
【发布时间】:2016-03-13 10:30:59
【问题描述】:

我有一个两行图,它由第一行的图和第二行的图列表组成。

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}

产生

问题是我do.call我的绘图列表,它立即将绘图发送到图形设备。

是否有解决此问题的方法,无论是在 knitr 中还是通过避免 do.call 在将情节传递给 grob 时吐出情节?

【问题讨论】:

  • 我认为你应该改用arrangeGrob。
  • @Axeman 这就是答案。

标签: r ggplot2 latex knitr do.call


【解决方案1】:

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

  • arrangeGrob:返回一个不绘制的 grob
  • grid.arrange:在当前设备上绘制
  • marrangeGrob:arrangeGrob 的接口,可以在多个页面上调度

因此,解决方案是使用arrangeGrob 而不是grid.arrange。

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

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2013-06-20
    • 1970-01-01
    • 2012-06-07
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多