title: Save the multiple plot in loop in R to pdf
body: <p>I have two different plots. I want to put them together, I use them to save in pdf. However, the pdf can not open. I am using the following simple template to save the figures in pdf, can you please help me with that? Thanks.</p>
  <pre><code>for(i in 1:4){
  
     par(mfrow=c(2, 1))
     # plot 1 
     layout(matrix(c(rep(1,6),rep(2,4)),nrow=2,ncol=5))
  
      plotModelFit(dataBlock,fit)
      plotMarginal(extract(fit,&quot;beta[1]&quot;),predictor=&quot;dataset age&quot;,col=&quot;blue&quot;)
  
      #plot 2
      plotModelFit(dataBlock,fit)
      plotMarginal(extract(fit,&quot;beta[1]&quot;),predictor=&quot;dataset age&quot;,col=&quot;blue&quot;)
  
      # save to pdf
      pdf(file=sprintf(&quot;/Users/test.pdf&quot;,responseVars[i]),width=10,height=6)
      dev.off()
      }
  </code></pre>