title: Storing plot objects in a list
body: <p>I asked <a href="https://stackoverflow.com/questions/1816480/generating-names-iteratively-in-r-for-storing-plots">this</a> question yesterday about storing a plot within an object. I tried implementing the first approach (aware that I did not specify that I was using <code>qplot()</code> in my original question) and noticed that it did not work as expected.</p>
  
  <pre><code>library(ggplot2)               # add ggplot2
  
  string = "C:/example.pdf"      # Setup pdf
  pdf(string,height=6,width=9)
  
  x_range &lt;- range(1,50)         # Specify Range
  
  # Create a list to hold the plot objects.
  pltList &lt;- list()
  pltList[]
  
  for(i in 1 : 16){
  
  # Organise data 
  y = (1:50) * i * 1000                       # Get y col
  x = (1:50)                                  # get x col
  y = log(y)                                  # Use natural log
  
  # Regression
  lm.0 = lm(formula = y ~ x)                  # make linear model
  inter = summary(lm.0)$coefficients[1,1]     # Get intercept
  slop = summary(lm.0)$coefficients[2,1]      # Get slope
  
  # Make plot name
  pltName &lt;- paste( 'a', i, sep = '' )
  
  # make plot object    
  p &lt;- qplot(
      x, y,   
      xlab = "Radius [km]", 
      ylab = "Services [log]",
      xlim = x_range,
      main = paste("Sample",i)
  ) + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1)        
  
  print(p)     
  
  pltList[[pltName]] = p       
  }
  
  # close the PDF file
  dev.off() 
  </code></pre>
  
  <p>I have used sample numbers in this case so the code runs if it is just copied. I did spend a few hours puzzling over this but I cannot figure out what is going wrong. It writes the first set of pdfs without problem, so I have 16 pdfs with the correct plots.</p>
  
  <p>Then when I use this piece of code:</p>
  
  <pre><code>string = "C:/test_tabloid.pdf"
  pdf(string, height = 11, width = 17)
  
  grid.newpage()
  pushViewport( viewport( layout = grid.layout(3, 3) ) )
  
  vplayout &lt;- function(x, y){viewport(layout.pos.row = x, layout.pos.col = y)}
  
  counter = 1
  
  # Page 1
  for (i in 1:3){    
      for (j in 1:3){     
           pltName &lt;- paste( 'a', counter, sep = '' )   
           print( pltList[[pltName]], vp = vplayout(i,j) )
           counter = counter + 1
       }
   }
  
   dev.off()
  </code></pre>
  
  <p>the result I get is the last linear model line (<code>abline</code>) on every graph, but the data does not change. When I check my list of plots, it seems that all of them become overwritten by the most recent plot (with the exception of the <code>abline</code> object).</p>
  
  <p>A less important secondary question was how to generate a muli-page pdf with several plots on each page, but the main goal of my code was to store the plots in a list that I could access at a later date.</p>