3

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

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.callgrob

4

1 回答 1

4

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

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

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

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

于 2016-03-13T11:18:21.077 回答