I have the code below that generates upto 150 plots. My aim is to output the plots in groups of 4 by 4 in each page of my word/pdf output in Rmarkdown. For example if the number of plots is 80 i want the ouput to be 5 pages with 16 images each
plot_list<-c()
Plots_data<-select(data,c(model, Make,Average.Dist))
for (model1 in Plots_data$model){
model_df <- subset(Plots_data, model == model1)
plot2 <-ggplot(model_df, aes(x=Average.Dist))+
geom_density(color = '#3d85c6ff',fill="#45a7dd")+theme_bw()+
labs(title = paste0("Density plots for ",model_df$model," Make ",model_df$Make), x = 'Average Dist', y = 'Density') +
theme(text=element_text(face="bold", size=11)) +
theme(plot.title = element_text(hjust = 0.5))
box2<-ggplot(model_df, aes(x=model,y =Average.Dist )) +
geom_boxplot(color = '#45a7dd')+
labs(title = paste0("Boxplots for ",model_df$model), x = 'Model', y = 'Average Dist') +
theme(text=element_text(face="bold", size=11)) +
theme_bw()+
theme(plot.title = element_text(hjust = 0.5))+
theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)))+
theme(axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 10, l = 0)))
plot_list[[model1]] <- list(plot2,box2)
}
When i use print(plot_list)
it prints out individual plots in each page returning too many pages but i want the plots to be grouped to save on space
I have tried this :allplots <- cowplot::plot_grid(grobs = plot_list)
and this: allplots <- grid.arrange(grobs = plot_list)
and this: allplots <- ggarrange(grobs = plot_list)
but all these return blank or an error: only 'grobs' allowed in "gList"
Who can help me on the best way to do this? Thank you in advance