title: Saving Multiple graphs in 1 pdf page from a for loop in R
body: <p>Please assume any dataset. Situation: I am running a for loop on the all independent variables to create a relationship(scatter) plot with the dependent variable. And want to save the plots as a pdf but in 1 or 2 pages of pdf for 1 file, instead of every graph in 1 individual page(that I already achieved). I am using following cases</p>
  
  <p>1st Try using dev option</p>
  
  <pre><code>library(ggplot2)
  library(gridExtra)
  
  pdf("one.pdf", onefile = TRUE)
  for (i in 1:length(dataset)) 
  {
  new_plot[[i]]=print(ggplot(data=dataset, aes(x=dataset[[i]], y=loss))+geom_point(size=1, alpha=0.3)+ 
        geom_smooth(method = lm)+
        xlab(paste0(colnames(int[i]), '\n', 'R-Squared: ',round(cor(int[[i]],int$loss, use = 'complete.obs'), 2)))+
        ylab("log(loss)") + theme_light())
  plot_list = c(new_plot[[i]])
  grid.arrange(plot_list)
  }
  dev.off()
  </code></pre>
  
  <p>2nd Try using ggsave</p>
  
  <pre><code>    for (i in 1:length(dataset)) 
      {
      new_plot[[i]]=print(ggplot(data=dataset, aes(x=dataset[[i]], y=loss))+geom_point(size=1, alpha=0.3)+ 
            geom_smooth(method = lm)+
            xlab(paste0(colnames(int[i]), '\n', 'R-Squared: ',round(cor(int[[i]],int$loss, use = 'complete.obs'), 2)))+
            ylab("log(loss)") + theme_light())
      m=marrangeGrob(new_plot[[i]],nrow=2, ncol=2)
      }
  
      ggsave("one.pdf",m)
  </code></pre>
  
  <p>Both of the time I received an error</p>
  
  <pre><code>Error in gList(data = list(list(x = c(2213.18, 1283.6, 3005.09, 939.85,  : 
  only 'grobs' allowed in "gList"
  </code></pre>
  
  <p>Also if possible then share how the graphs can be posted in 2*2(example) on every page. I do appreciate all the help.Thanks in Advance!</p>