title: Storing ggplot objects in a list from within loop in R
body: <p>My problem is similar to <a href="https://stackoverflow.com/questions/1820590/problem-storing-plot-objects-in-a-list-in-r">this one</a>; when I generate plot objects (in this case histograms) in a loop, seems that all of them become overwritten by the most recent plot. </p>
  
  <p>To debug, within the loop, I am printing the index and the generated plot, both of which appear correctly. But when I look at the plots stored in the list, they are all identical <strong>except</strong> for the label. </p>
  
  <p>(I'm using multiplot to make a composite image, but you get same outcome if you <code>print (myplots[[1]])</code>
  through <code>print(myplots[[4]])</code> one at a time.) </p>
  
  <p>Because I already have an attached dataframe (unlike the poster of the similar problem), I am not sure how to solve the problem.</p>
  
  <p>(btw, column classes are factor in the original dataset I am approximating here, but same problem occurs if they are integer)</p>
  
  <p>Here is a reproducible example:</p>
  
  <pre><code>library(ggplot2)
  source("http://peterhaschke.com/Code/multiplot.R") #load multiplot function
  
  #make sample data
  col1 &lt;- c(2, 4, 1, 2, 5, 1, 2, 0, 1, 4, 4, 3, 5, 2, 4, 3, 3, 6, 5, 3, 6, 4, 3, 4, 4, 3, 4, 
            2, 4, 3, 3, 5, 3, 5, 5, 0, 0, 3, 3, 6, 5, 4, 4, 1, 3, 3, 2, 0, 5, 3, 6, 6, 2, 3, 
            3, 1, 5, 3, 4, 6)
  col2 &lt;- c(2, 4, 4, 0, 4, 4, 4, 4, 1, 4, 4, 3, 5, 0, 4, 5, 3, 6, 5, 3, 6, 4, 4, 2, 4, 4, 4, 
            1, 1, 2, 2, 3, 3, 5, 0, 3, 4, 2, 4, 5, 5, 4, 4, 2, 3, 5, 2, 6, 5, 2, 4, 6, 3, 3, 
            3, 1, 4, 3, 5, 4)
  col3 &lt;- c(2, 5, 4, 1, 4, 2, 3, 0, 1, 3, 4, 2, 5, 1, 4, 3, 4, 6, 3, 4, 6, 4, 1, 3, 5, 4, 3, 
            2, 1, 3, 2, 2, 2, 4, 0, 1, 4, 4, 3, 5, 3, 2, 5, 2, 3, 3, 4, 2, 4, 2, 4, 5, 1, 3, 
            3, 3, 4, 3, 5, 4)
  col4 &lt;- c(2, 5, 2, 1, 4, 1, 3, 4, 1, 3, 5, 2, 4, 3, 5, 3, 4, 6, 3, 4, 6, 4, 3, 2, 5, 5, 4,
            2, 3, 2, 2, 3, 3, 4, 0, 1, 4, 3, 3, 5, 4, 4, 4, 3, 3, 5, 4, 3, 5, 3, 6, 6, 4, 2, 
            3, 3, 4, 4, 4, 6)
  data2 &lt;- data.frame(col1,col2,col3,col4)
  data2[,1:4] &lt;- lapply(data2[,1:4], as.factor)
  colnames(data2)&lt;- c("A","B","C", "D")
  
  #generate plots
  myplots &lt;- list()  # new empty list
  for (i in 1:4) {
    p1 &lt;- ggplot(data=data.frame(data2),aes(x=data2[ ,i]))+ 
      geom_histogram(fill="lightgreen") +
      xlab(colnames(data2)[ i])
    print(i)
    print(p1)
    myplots[[i]] &lt;- p1  # add each plot into plot list
  }
  multiplot(plotlist = myplots, cols = 4)
  </code></pre>
  
  <p>When I look at a summary of a plot object in the plot list, this is what I see</p>
  
  <pre><code>&gt; summary(myplots[[1]])
  data: A, B, C, D [60x4]
  mapping:  x = data2[, i]
  faceting: facet_null() 
  -----------------------------------
  geom_histogram: fill = lightgreen 
  stat_bin:  
  position_stack: (width = NULL, height = NULL)
  </code></pre>
  
  <p>I think that <code>mapping:  x = data2[, i]</code> is the problem, but I am stumped! I can't post images, so you'll need to run my example and look at the graphs if my explanation of the problem is confusing.</p>
  
  <p>Thanks!</p>