title: R - save multiplot to file
body: <p>I’d really appreciate your help with the following problem. I know several ways to save a single plot to a file. My question is: How do I correctly save a <strong>multiplot</strong> to a file?</p>
  
  <p>To begin with, I’m not an experienced R user. I use ggplot2 to create my plots, and another thing I should probably mention is that I use the RStudio GUI. Using an <a href="http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20%28ggplot2%29/">example</a>  from the R Cookbook, I'm able to create multiple plots in one window.</p>
  
  <p>I would like to save this so-called multiplot to a file (preferably as jpeg), but somehow fail to do this.</p>
  
  <p>I’m creating the multiplot as follows:</p>
  
  <pre><code>##define multiplot function
      multiplot &lt;- function(..., plotlist=NULL, cols) {
          require(grid)
  
          # Make a list from the ... arguments and plotlist
          plots &lt;- c(list(...), plotlist)
  
          numPlots = length(plots)
  
          # Make the panel
          plotCols = cols                          # Number of columns of plots
          plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
  
          # Set up the page
          grid.newpage()
          pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
          vplayout &lt;- function(x, y)
              viewport(layout.pos.row = x, layout.pos.col = y)
  
          # Make each plot, in the correct location
          for (i in 1:numPlots) {
              curRow = ceiling(i/plotCols)
              curCol = (i-1) %% plotCols + 1
              print(plots[[i]], vp = vplayout(curRow, curCol ))
          }
  
      }
  
  ## define subplots (short example here, I specified some more aesthetics in my script)
  plot1a &lt;- qplot(variable1,variable2,data=Mydataframe1)
  plot1b &lt;- qplot(variable1,variable3,data=Mydataframe1)  
  plot1c &lt;- qplot(variable1,variable2,data=Mydataframe2)
  plot1d &lt;- qplot(variable1,variable3,data=Mydataframe2)  
  
  ## plot in one frame
  Myplot &lt;- multiplot(plot1a,plot1b,plot1c,plot1d, cols=2)
  </code></pre>
  
  <p>This gives the desired result. The problem arises when I try to save to a file. I can do this manually in RStudio (using Export -> Save plot as image), but I would like to run everything in a script. I manage to save only subplot1d (which is last_plot()), and not the complete multiplot.</p>
  
  <p>What I’ve tried so far:</p>
  
  <ol>
  <li><p>Using ggsave</p>
  
  <pre><code>ggsave(filename = "D:/R/plots/Myplots.jpg")
  </code></pre>
  
  <p>This results in only subplot 1d being saved.</p></li>
  <li><p>Using jpeg(), print() and dev.off()</p>
  
  <pre><code>jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE)
  print(Myplot)
  dev.off()
  </code></pre>
  
  <p>This results in a completely white image (just the background I assume). print(Myplot) returns NULL.</p></li>
  </ol>
  
  <p>Not sure what I’m doing wrong here. My lack of understanding R is the reason I am stuck trying to find a solution. Can anyone explain what I’m doing wrong and perhaps suggest a way to solve my problem(s)?</p>