如何使用grid.arrange来安排任意数量的ggplots? [英] How can I arrange an arbitrary number of ggplots using grid.arrange?

查看:164
本文介绍了如何使用grid.arrange来安排任意数量的ggplots?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是交叉发布在ggplot2谷歌组上

This is cross-posted on the ggplot2 google group

我的情况是, a href =https://github.com/briandk/granova/blob/dev/R/granova.contr.ggplot.R =noreferrertitle =点击这里查看我们图形分析的开发库R>中的差异包处理函数,输出任意数量的图(取决于用户提供的输入数据)。该函数返回n个地块的列表,并且我想将这些地块放置为2 x 2阵形。我正在努力解决以下问题:

My situation is that I'm working on a function that outputs an arbitrary number of plots (depending upon the input data supplied by the user). The function returns a list of n plots, and I'd like to lay those plots out in 2 x 2 formation. I'm struggling with the simultaneous problems of:


  1. 我该如何灵活处理任意数量的图?

  2. 我怎样才能指定我想让他们摆放2 x 2

我目前的策略使用 gridExtra 包中的 grid.arrange 。这可能不是最佳的,尤其是因为,这是关键,完全不起作用。这是我评论的示例代码,试验了三个图:

My current strategy uses grid.arrange from the gridExtra package. It's probably not optimal, especially since, and this is key, it totally doesn't work. Here's my commented sample code, experimenting with three plots:

library(ggplot2)
library(gridExtra)

x <- qplot(mpg, disp, data = mtcars)
y <- qplot(hp, wt, data = mtcars)
z <- qplot(qsec, wt, data = mtcars)

# A normal, plain-jane call to grid.arrange is fine for displaying all my plots
grid.arrange(x, y, z)

# But, for my purposes, I need a 2 x 2 layout. So the command below works acceptably.
grid.arrange(x, y, z, nrow = 2, ncol = 2)

# The problem is that the function I'm developing outputs a LIST of an arbitrary
# number plots, and I'd like to be able to plot every plot in the list on a 2 x 2
# laid-out page. I can at least plot a list of plots by constructing a do.call()
# expression, below. (Note: it totally even surprises me that this do.call expression
# DOES work. I'm astounded.)
plot.list <- list(x, y, z)
do.call(grid.arrange, plot.list)

# But now I need 2 x 2 pages. No problem, right? Since do.call() is taking a list of
# arguments, I'll just add my grid.layout arguments to the list. Since grid.arrange is
# supposed to pass layout arguments along to grid.layout anyway, this should work.
args.list <- c(plot.list, "nrow = 2", "ncol = 2")

# Except that the line below is going to fail, producing an "input must be grobs!"
# error
do.call(grid.arrange, args.list)

我不愿意这样做,我虚心地蜷缩在角落,热切地等待着一个远比我更聪明的社区的明智反馈。特别是如果我做得比它需要更难。

As I am wont to do, I humbly huddle in the corner, eagerly awaiting the sagacious feedback of a community far wiser than I. Especially if I'm making this harder than it needs to be.

推荐答案

你几乎在那里!问题在于 do.call 期望您的参数位于名为 list 对象中。你已经把它们放在列表中,但作为字符串,而不是命名列表项。

You're ALMOST there! The problem is that do.call expects your args to be in a named list object. You've put them in the list, but as character strings, not named list items.

我认为这应该可行:

I think this should work:

args.list <- c(plot.list, 2,2)
names(args.list) <- c("x", "y", "z", "nrow", "ncol")

正如Ben和Joshua在评论中指出的,我可以在创建列表时指定名称:

as Ben and Joshua pointed out in the comments, I could have assigned names when I created the list:

args.list <- c(plot.list,list(nrow=2,ncol=2))



or

args.list <- list(x=x, y=y, z=x, nrow=2, ncol=2)

这篇关于如何使用grid.arrange来安排任意数量的ggplots?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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