使用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-of-grids”方法,尝试使用grid.arrange
来针对另一个图绘制一个网格(例如上面的plot5
),但获得了:
Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs!
更新:
谢谢你的建议。我将研究视口(viewport)
和网格
。同时,感谢@DWin,“wq”包中的 layOut
函数对于我的 Sweave
文档中的编译图效果非常好:
更新2:
arrangeGrob
命令(如@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
直接在设备上绘制;如果你想将它与其他网格对象组合,你需要 arrangeGrob
,如
p = rectGrob()
grid.arrange(p, arrangeGrob(p,p,p, heights=c(3/4, 1/4, 1/4), ncol=1),
ncol=2)
编辑 (07/2015):在 v>2.0.0 中,您可以使用 layout_matrix
参数,
grid.arrange(p,p,p,p, layout_matrix = cbind(c(1,1,1), c(2,3,4)))
关于r - 如何获得 'unbalanced' 的 ggplots 网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8112208/