title: save grobs made in function of loop
body: <p>I made this long function that ends with a grob figure (3 ggplots). However, when I use this function in a loop and try to put all the resulting grobs into one super figure, I run into problems. I obtain a figure with a lot of grobs, but it's always the same grob (the first one)! </p>
  
  <p>To demonstrate my problem I made this simplified example:</p>
  
  <pre><code>library(ggplot2)
  library(gridExtra)
  
  data(iris)
  
  #the function
  multi.plot=function(data,heading){
    p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ggtitle(heading)
    p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()
    p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()
    grob1=arrangeGrob(p1,ncol=2)
    grob2=arrangeGrob(p3,p2,ncol=2)
    grob3=arrangeGrob(grob1,grob2)
    # grob3  / return(grob3)  / print(grob3)   =&gt; all tried but non of them helps
  }
  
  # the loop
  list.grob=list()
  for(i in unique(iris$Species)){
    select=iris[iris$Species==i,]
    multi.plot(data=select,heading=i)
    list.grob[[which(unique(iris$Species)==i)]]=grob3  
  }
  
  # the final figure
  png(file="superplot.png")
  do.call("grid.arrange", list.grob)  
  dev.off()
  </code></pre>
  
  <p>So how do I make a figure with a lot of grobs using a function and a loop? Also, my title ("heading") doesn't appear as it should be. </p>
  
  <p>Thanks for any advice!</p>