When trying to call grid.arrange to put multiple plots on a same ggplot2 graph, I first build a list of the plots I want. Then I build the corresponding argument list to call grid.arrange, as was explained in a previous question. This is my code (my dataframe is called manip):
args.list <- NULL;
plot.list <- NULL;
for (m in names(manip[2:10])) {
plot.list <- c(plot.list, list(qplot(manip$side, y=manip[,m],ylab=m))
}
args.list <- c(plot.list, 1, 9)
names(args.list) <- c(names(manip)[2:10], list("nrow","ncol"))
do.call(grid.arrange, args.list)
This works, except that the 9 graphs are exactly the same! After checking, it turns out that the data is always the one corresponding to m=10
. So my guess was that the value of m
is not assigned in the loop, but evaluated later. However, the label ylab=m
is assigned correctly and is different for all the graphs.
So I don't really get what the difference is and how the interpreter chooses when to evaluate m for the plots. Can someone explain?
I will first answer your question and then show an alternative using a facet plot.
Edited
The following, much simplified, code seems to work:
It produces this ugly plot:
Without knowing your objectives, it is dangerous to try and give advice, I know. Nonetheless, have you considered using facets for your plot instead?
The following code is much simpler, executes quiker and produces a graph that is easier to interpret:
Perhaps it would be better to melt then data and use faceting?
It need some polishing in the xlab
HTH
The behavior is due to the lazy evaluation of R.
Here is a minimal(?) example:
in this case,
d[m]
is evaluated at the call ofdo.call
. som
is 3 for all panel.here is a workaround:
in this case,
d[m]
is evaluated at the call ofqplot
, and thed[m]
is stored in the output object of qplot.so, the simple solution is to pass data to
qplot()
orggplot()
.