r - 如何获得 'unbalanced' 的 ggplots 网格?

标签 r ggplot2 r-grid

使用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 文档中的编译图效果非常好: enter image description here

更新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}

产生以下输出: enter image description here

顺便说一句,有人告诉我为什么会出现“>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/

相关文章:

r - 循环遍历未命名的列以在 R 中的一张图上绘制

r - 按标识符组织 csv 并在 R 中打开/处理子集

r - 如何在R中动态更改图表标题?

r - geom_bar 精确绑定(bind)到 x 和 y 轴(不聚合)

r - R ggplot geom_tile没有填充颜色

r - 在ggplot2 facet_wrap中跨列组合多个方面条

r - ggplot2 中 geom_point 的黑星符号

r - 模拟来自(非标准)密度函数的数据

r - 相同的代码在 R 和 R studio 中的工作方式不同