Go over the value in R

When I try to call grid.arrange to place multiple graphs on the same ggplot2 graph, I first create a list of the graphs that I want. Then I create an appropriate argument list for calling grid.arrange, as explained in the previous question . This is my code (my dataframe is called control):

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 9 graphs are exactly the same! After checking, it turns out that the data always corresponds to the value m=10 . Therefore, I assumed that the value of m not assigned in the loop, but is evaluated later. However label ylab=m assigned correctly and is different for all schedules.

Thus, I really do not understand what the difference is and how the interpreter chooses when to evaluate m for graphs. Can someone explain?

+4
source share
3 answers

First I will answer your question and then show an alternative using the facet graph.

Edited

The following simplified code seems to work:

 library(gridExtra) manip <- mtcars plot.list <- lapply(2:11, function(x)qplot(manip$mpg, y=manip[, x], ylab=names(manip)[x])) do.call(grid.arrange, c(plot.list, nrow=10)) 

He produces this ugly plot: enter image description here


Not knowing my goals, I know that it is dangerous to try to give advice. However, do you think that instead you use facets for your plot?

The following code is much simpler, runs quiker and creates a graph that is easier to interpret:

 library(reshape2) manip <- mtcars mmanip <- melt(manip, id.vars="mpg") str(mmanip) ggplot(mmanip, aes(x=mpg, y=value)) + geom_point(stat="identity") + facet_grid(.~variable, scales="free") 

enter image description here

+3
source

Behavior is associated with a lazy grade of R.

Here is a minimal (?) Example:

 d <- 1:3 args.list <- NULL; plot.list <- NULL; for (m in 1:3) { plot.list <- c(plot.list, list(qplot(d[m], d[m], ylab=letters[m]))) } args.list <- c(plot.list, nrow=1, ncol=3) do.call(grid.arrange, args.list) 

in this case, d[m] is evaluated when do.call called. therefore, m is 3 for all panels.

here is a workaround:

 d <- 1:3 args.list <- NULL; plot.list <- NULL; for (m in 1:3) { plot.list <- c(plot.list, list(qplot(d, d, data=data.frame(d=d[m]), ylab=letters[m]))) } args.list <- c(plot.list, nrow=1, ncol=3) do.call(grid.arrange, args.list) 

in this case, d[m] is evaluated when qplot called, and d[m] stored in the qplot output object.

therefore, a simple solution is to pass data to qplot() or ggplot() .

+4
source

Perhaps it would be better to melt the data and use the cut?

 library(ggplot2) manip <- data.frame(car = row.names(mtcars), mtcars) manip.m <- melt(manip) qplot(car, value, data = manip.m) + facet_wrap(~variable, scales = "free_y") 

He needs polishing in xlab

 last_plot() + opts(axis.text.x = theme_text(angle = 90)) 

enter image description here

NTN

+2
source

All Articles