1

After much editing and several useful comments...

I would like to loop through four files and plot the data in each one in four different plots, on the same page. I need to do this for every value of laneCombination, which are column numbers. Using the code below (minimal example of the error), I get the desired result except that the plots contain the same data.

library(ggplot2)
library(grid)
library(gridExtra)

plots <- list()

for (laneCombination in 3:3) {
  for (depVar in 1:4) {
    simdata <- data.frame(cond = rep(c("A", "B"), each=10),
                                    xvar = 1:20,
                                    yvar = 1:20 + rnorm(20,sd=3))
    plots[[depVar]] <- ggplot(simdata, aes(xvar, simdata[,laneCombination]), environment = environment()) +
    geom_point() + geom_line() + labs(x="Duration", y=depVar) + theme(legend.position="none")
  }
  grid.arrange(plots[[1]], plots[[2]], plots[[3]], plots[[4]], ncol = 1, main = colnames(simdata[laneCombination]))
}

First three plots are overwritten with the last: results

@eipi10 has correctly identified the problem in the comments: simdata[,laneCombination] causes scope issues. However, the solution in the SO question referenced in his/her comment (Addressing x and y in aes by variable number) does not seem to solve the problem. I need to loop through the column numbers for my task.

Community
  • 1
  • 1
David Prentiss
  • 548
  • 1
  • 6
  • 16
  • How many lines do you expect to see on each panel? – jbaums Oct 29 '14 at 00:13
  • @jbaums, many. In fact, the last plot (with 'Y' axis label "Average speed, cars") is correct. The others unfortunately are overwritten somehow with this plot. – David Prentiss Oct 29 '14 at 00:15
  • 2
    Just style-wise and to save some typing, you don't need all those `plots[[depVar]] <- plots[[depVar]] + ...`. You can just put a `+` at the end of each line and put the whole plot together with a single assignment to `plots[[depVar]]`. – eipi10 Oct 29 '14 at 00:29
  • I can't replicate this with dummy datasets... we might need to see a [minimal dataset that produces the problem](http://stackoverflow.com/q/5963269/489704). – jbaums Oct 29 '14 at 00:37
  • 1
    As @jbaums said, without a reproducible example, it's difficult to know for sure what's going on, but one potential issue is `simdata[[depVar]][,laneCombination]`. See [this SO question](http://stackoverflow.com/questions/15323269/addressing-x-and-y-in-aes-by-variable-number) for details on how to reference variable names by position. – eipi10 Oct 29 '14 at 00:45
  • 1
    Besides the other comments on reproducability - you might also benefit from leveraging ggplot's facetting capabilities instead of coding complicated loops. To this end you would create a single, long data frame with a value column storing the variable values and a variable column storing the variable type. If you then create the ggplot object on the combined data frame you create separate plots for the different vars by facetting: `facet_wrap(~ variable, scales = "free")` – CMichael Oct 29 '14 at 10:41
  • You sure you want `main` related to `simdata[[depVar]]` *outside* the `depVar` loop? Also - try just plotting each grob by itself to verify the difference you "expect" . Without seeing `simdata` values we can't tell whether the underlying data (as opposed to,say column labels) are actually different. – Carl Witthoft Oct 29 '14 at 12:02
  • I have plotted each separately. I can attest that the data differ in each file. The problem seems to be that `plots[[2]] <- ggplot(...)` overwrites `plots[[1]]`. Expanding `simdata[[depVar]]` to `simdata1`, `simdata2`, etc. fixes the problem, but then I can't use a loop because I need a variable name for each file. – David Prentiss Oct 29 '14 at 15:05
  • 2
    If you could post a sample of your data using `dput`, we would be able to try out your code on your data and perhaps figure out where things are going wrong. Also, to amplify @CMichael's comment, if you can stack the `simdata` data frames into a single data frame, adding a category column that says which data frame each row originally came from, then you can facet on that column and avoid the loop. – eipi10 Oct 29 '14 at 15:51
  • @eipi10 is correct about the scope issue. However the referenced solution does not seem to fix the problem. – David Prentiss Oct 29 '14 at 16:48
  • I think it will work if you switch to using `aes_string` and pull out the *name* of the column of interest. Try replacing your `aes` with `aes_string("xvar", names(simdata[laneCombination]))`. – aosmith Oct 30 '14 at 14:48

0 Answers0