这是交叉发布在ggplot2 Google组上
我的情况是我正在使用一个函数,该函数可以输出任意数量的绘图(取决于用户提供的输入数据)。该函数返回n个图的列表,我想以2 x 2的形式排列这些图。我正在为同时存在的问题而苦苦挣扎:
- 如何允许将灵活性分配给任意(n)个图?
- 我还要如何指定我想要的布局2 x 2
我当前的策略使用grid.arrange
了该gridExtra
软件包。这可能不是最佳的,尤其是因为这是关键,所以它完全不起作用。这是我评论过的示例代码,尝试了三个图:
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)
就像我不愿意做的那样,我谦卑地the缩在角落里,热切地等待着比我聪明得多的社区的睿智反馈。特别是如果我使这一过程变得比需要做的要难。
2
在一个非常出色的问题上表示赞扬。我将以此为例,说明如何编写一个好的SO [r]问题。
—
JD
尤其是“谦卑的拥挤”部分-就像是一个不错的丛林:-)
—
Ben Bolker
@JD和@Ben-我受宠若惊,伙计们。真诚的 我真的很感谢您的帮助。
—
briandk 2011年