【发布时间】:2011-12-28 01:44:29
【问题描述】:
使用grid.arrange,我可以将多个ggplot 图形排列在一个网格中,以使用以下方式实现多面板图形:
library(ggplot2)
library(grid)
library(gridExtra)
生成一些ggplot2图,然后
plot5 <- grid.arrange(plot4, plot1, heights=c(3/4, 1/4), ncol=1, nrow=2)
如何获得一个“不平衡”的 2 列布局,在整个第一列中包含一个图,在第二列中包含三个图?
我通过尝试使用grid.arrange 将一个网格(例如上面的plot5)与另一个图绘制出来来玩弄“网格网格”方法,但获得了:
arrangeGrob(..., as.table = as.table, clip = clip, main = main, 中的错误: 输入必须是 grobs!
更新:
感谢您的建议。我会调查viewports 和grid。同时,感谢@DWin,'wq' 包中的layOut 函数对我的Sweave 文档中的编译图非常有效:
更新 2:
arrangeGrobcommand(由@baptiste 建议)也很有效,而且看起来非常直观——至少很容易改变两列的宽度。它还有一个好处是不需要 `wq' 包。
例如这是我的 Sweave 文件中的代码:
<<label=fig5plot, echo=F, results=hide>>=
plot5<-grid.arrange(plot4, arrangeGrob(plot1, plot2, plot3, ncol=1),
ncol=2, widths=c(1,1.2))
@
\begin{figure}[]
\begin{center}
<<label=fig5,fig=TRUE,echo=T, width=10,height=12>>=
<<fig5plot>>
@
\end{center}
\caption{Combined plots using the `arrangeGrob' command.}
\label{fig:five}
\end{figure}
产生以下输出:
顺便说一句,有人告诉我为什么会出现“>NA”吗?
【问题讨论】:
-
您可能必须自己设置视口——
grid.arrange可能不够灵活(在 stackoverflow 中搜索“[r] grid viewport”) -
@BenBolker 使用
grid为您指明了一个富有成效的方向。另请参阅 Hadley 的 ggplot2 书,第 8.4.2 节。 -
@BenBolker
grid.arrange可以与嵌套视口一起使用,使用它的同伴arrangeGrob(本质上返回一个gTree),如下面的示例所示。 -
您对
plot5的最终分配不是必需的,因为grid.arrange不返回任何内容(NULL)。如果您想保存生成的 grob,请再次使用arrangeGrob(并使用grid.draw来显示它)。