title: How to store plots in a list when inside a loop?
body: <p>I'm working inside a loop and I'd like to save plots in a list so I can plot them together in a .pdf. The problem is that the list is not filled up properly and re-updates with the results of the last run. So, in the end what I get is a list with five elements that are exactly the same.</p>
  
  <p>I'm aware the loops may seem useless, but I just have them to create a test code (with a reproducible error) as close to the real one as possible. So, I need to leave the loops as they are.</p>
  
  <pre><code>library (ggplot)
  library (gridExtra)
  
  plist &lt;- list()
  
  for (z in 1:5){
    n &lt;- 100
    k &lt;- seq(0, to=4500+z*2000, length=n)
    tmp &lt;- numeric(n)
    for (i in 1:n){
      tmp[i] &lt;- (5*(i*3)^2)}
  
    plist[[z]] &lt;- ggplot() +
      geom_line(aes(x = k, y = tmp)) +
      theme_bw()
  
    pdf(sprintf("p%s.pdf", z),
        width = 6, height = 4, onefile = T)
    plot(plist[[z]])
    dev.off()
  }
  
  do.call(grid.arrange, c(plist, ncol = 5))
  </code></pre>