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

查看:27
本文介绍了如何获得 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:

arrangeGrob(..., as.table = as.table, clip = clip, main = main, 中的错误:输入必须是grobs!

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))
@
egin{figure}[]
    egin{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)

Edit (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天全站免登陆