如何获得ggplots的“不平衡"网格? [英] How can I obtain an 'unbalanced' grid of ggplots?

查看:127
本文介绍了如何获得ggplots的“不平衡"网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用grid.arrange,我可以使用以下方式将多个ggplot人形排列在一个网格中以实现多面板人形:

With grid.arrange I can arrange multiple ggplot figures in a grid to achieve a multi-panel figure by using something like:

library(ggplot2)
library(grid)
library(gridExtra)

生成一些ggplot2图,然后

generate some ggplot2 plots , then

plot5 <- grid.arrange(plot4, plot1, heights=c(3/4, 1/4), ncol=1, nrow=2)

如何获得不平衡"的2列布局,在整个第一个列中有一个图,在第二个列中有三个图? 我尝试使用grid.arrange相对于另一图绘制一个网格(例如上面的plot5),以网格化"的方式玩弄,但是获得了:

How can I obtain an 'unbalanced' 2 col layout with one plot in the entire first col and three plots in the second col? I toyed with a 'grid-of-grids' approach by trying to use grid.arrange to plot one grid (e.g. plot5, above) against another plot, but obtained:

rangingGrob(...,as.table = as.table,clip = clip,main = main,错误: 输入必须是杂项!

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs!

更新:

感谢您的建议.我将研究viewportsgrid.同时,由于使用@ DWin,'wq'包中的layOut函数对于我的Sweave文档中的编译图非常有效:

Thanks for the advice. I will look into viewports and grid. In the meantime, thanks to @DWin, the layOut function in the 'wq' package worked very well for the compilation figure in my Sweave document:

更新2:

arrangeGrob命令(由@baptiste建议)也可以很好地工作,并且看起来非常直观-至少很容易更改两列的宽度.它还具有不需要`wq'软件包的好处.

The arrangeGrobcommand (as suggested by @baptiste) also works well, and seems very intuitive - at least it was easy to alter widths of the two columns. It also has the benefit of not requiring the `wq' package.

例如这是我的Sweave文件中的代码:

e.g. Here is the code from my Sweave file:

<<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'吗?

BTW, Anyone tell me why the '>NA' appears?

推荐答案

grid.arrange直接在设备上绘制;如果要将其与其他网格对象组合,则需要arrangeGrob,如

grid.arrange draws directly on the device; if you want to combine it with other grid objects you need arrangeGrob, as in

 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参数,

Edit (07/2015): with v>2.0.0 you can use the layout_matrix argument,

 grid.arrange(p,p,p,p, layout_matrix = cbind(c(1,1,1), c(2,3,4)))

这篇关于如何获得ggplots的“不平衡"网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆