knitr:在 do.call(grid.arrange, list_of_plots) 时禁止立即绘图
knitr: restraining from immediately plotting when do.call(grid.arrange, list_of_plots)
我有一个双行图,它由第一行的图和第二行的图列表组成。
和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
时吐出情节?
我们使用 ??grid.arrange
找到 arrangeGrob
的帮助页面。这指定了以下内容:
- arrangeGrob: return a grob without drawing
- grid.arrange: draw on the current device
- marrangeGrob: interface to arrangeGrob that can dispatch on multiple pages
因此,解决方案是使用 arrangeGrob
而不是 grid.arrange
。
另一个好处是可以使用 grobs
参数传递 grob 列表,因此我们可以取消 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}
产生
问题是我 do.call
我的绘图列表,它会立即将绘图发送到图形设备。
是否有解决此问题的方法,在 knitr
中或通过避免 do.call
在将情节传递给 grob
时吐出情节?
我们使用 ??grid.arrange
找到 arrangeGrob
的帮助页面。这指定了以下内容:
- arrangeGrob: return a grob without drawing
- grid.arrange: draw on the current device
- marrangeGrob: interface to arrangeGrob that can dispatch on multiple pages
因此,解决方案是使用 arrangeGrob
而不是 grid.arrange
。
另一个好处是可以使用 grobs
参数传递 grob 列表,因此我们可以取消 do.call
结构。