Ceci est posté sur le groupe google ggplot2
Ma situation est que je travaille sur une fonction qui génère un nombre arbitraire de tracés (en fonction des données d'entrée fournies par l'utilisateur). La fonction renvoie une liste de n parcelles, et j'aimerais disposer ces parcelles en formation 2 x 2. Je lutte avec les problèmes simultanés de:
- Comment puis-je permettre à la flexibilité de recevoir un nombre arbitraire (n) de parcelles?
- Comment puis-je également spécifier que je veux les disposer 2 x 2
Ma stratégie actuelle utilise grid.arrange
le gridExtra
package. Ce n'est probablement pas optimal, d'autant plus que, et c'est la clé, cela ne fonctionne absolument pas . Voici mon exemple de code commenté, en expérimentant trois graphiques:
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)
Comme je n'ai pas l'habitude de le faire, je me blottis humblement dans le coin, attendant avec impatience les commentaires avisés d'une communauté bien plus sage que moi. Surtout si je rends cela plus difficile que nécessaire.