John legend2
John legend2

Reputation: 920

save multiple plots (from ggplot2) using a for-loop by side-by-side

I saved ggplot in for loop like this. I have total around 300 plots (i = 1,,,300). I would like to save the plots with with pdf file but save 4 plots in a page (2 by 2) in the pdf file. If so, the output of pdf file should have 75 pages.

for( i in 1:300){ plot_list[[i]] = ggplot(out, aes(basket_size_group_by2dol, pct_trips_w_item, colour=channel2)) + ylim(min00,max00) + geom_point() }

Upvotes: 0

Views: 1189

Answers (2)

Peter
Peter

Reputation: 7800

Since you have generated a list of plots you can generate the pdf using the ggsave function fro the ggplot2 package and the marrangeGrob function form the gridExtra package. Here is a small example that generates eight plots and saves them, four to a page, in a two page pdf.

library(ggplot2)
library(gridExtra)

plot_list <- vector("list", 8) 
plot_list[[1]] <- ggplot(mtcars) + aes(x = wt, y = mpg)   + geom_point() + ggtitle("This is plot 1")
plot_list[[2]] <- ggplot(mtcars) + aes(x = cyl, y = mpg)  + geom_point() + ggtitle("This is plot 2")
plot_list[[3]] <- ggplot(mtcars) + aes(x = disp, y = mpg) + geom_point() + ggtitle("This is plot 3")
plot_list[[4]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 4")
plot_list[[5]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 5")
plot_list[[6]] <- ggplot(mtcars) + aes(x = qsec, y = mpg) + geom_point() + ggtitle("This is plot 6")
plot_list[[7]] <- ggplot(mtcars) + aes(x = vs, y = mpg)   + geom_point() + ggtitle("This is plot 7")
plot_list[[8]] <- ggplot(mtcars) + aes(x = gear, y = mpg) + geom_point() + ggtitle("This is plot 8")


### Use your plot_list here:
glist <- lapply(plot_list, ggplotGrob)
ggsave("plots.pdf", marrangeGrob(glist, nrow = 2, ncol = 2))

enter image description here

Upvotes: 1

mjhalwa
mjhalwa

Reputation: 533

I would recommend to use the cowplot and plot_grid() function and append the grid with ggplot2 objects. E.g. see: https://cran.r-project.org/web/packages/cowplot/vignettes/plot_grid.html

[EDIT]:
Example code (tested code):

library(cowplot)
library(ggplot2)
dat <- data.frame(x=1:10,y=2*(1:10),z=(1:10)^2)
p1 <- ggplot(dat, aes(x=x, y=y)) + geom_point()
p2 <- ggplot(dat, aes(x=x, y=z)) + geom_point()
plot_grid(p1, p2, labels=c("A","B"))

the following logic may allow to extend the idea to a dynamic for-loop:

plotlist <- list(p1,p2)
plot_grid(plotlist=plotlist)

[EDIT2]:
Here is a small demo for using the split command. I generate a vector x (works with lists same way) from 1 to 12. Then I generate my factor f which defines the groups (same number, same group). using split returns 4 lists of x , grouped by the factor f.

x <- 1:12
f <- rep(seq(1,4),each=3)
split(x,f)

$1 [1] 1 2 3

$2 [1] 4 5 6

$3 [1] 7 8 9

$4 [1] 10 11 12

You may compare with this extra code to understand what happens:

f1 <- rep(seq(1,4), times=3)
split(x,f1)

$1 [1] 1 5 9

$2 [1] 2 6 10

$3 [1] 3 7 11

$4 [1] 4 8 12

Also you get the help pages for R functions by:

?split
?plot_grid

and even for packages

?cowplot

Upvotes: 0

Related Questions