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

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

问题描述

这是在 ggplot2 谷歌群组中交叉发布的

我的情况是我正在处理一个函数,该函数输出任意数量的图(取决于用户提供的输入数据).该函数返回一个包含 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. 我怎样才能灵活地处理任意 (n) 个地块?
  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.

我认为这应该可行:

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))

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

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

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