This question already has an answer here:
- Printing multiple ggplots into a single pdf, multiple plots per page 6 answers
I am trying to print dozens of ggplot boxplots to a pdf file. I want four plots per page of the pdf. How can i use a for loop to create the plots and end up with my desired format?
names_vec <- colnames(raw_data)
pdf(file = 'test1.pdf')
for(i in names_vec) {
print(ggplot(raw_data, aes(x = Group, y = raw_data[[i]])) +
geom_boxplot(na.rm = TRUE) +
labs(title = i, y = 'Relative Intensity') +
theme(axis.text.x = element_text(size = 8, angle = 45)))
}
dev.off()
This is what I've done so far. par(mfrow = c(2,2)) is not working for me. Similarly, grid.arrange doesn't seem to be compatible with the loop strategy.
Sample code below:
Group glycine serine alanine threonine
1 Gatorade NA NA NA NA
2 Gatorade NA NA NA NA
3 Gatorade NA NA NA NA
4 Lime 17950 203400 2512000 864500
5 Lime 17950 193400 2621000 828500
6 Lime 18270 203200 2381000 885200
7 Lime 19370 214400 2623000 869000
8 Lime 17860 221200 2629000 786600
9 Lime 17570 196000 2667000 868900
10 Michelob 11820 388900 1563000 339100
11 Michelob 10670 419300 1460000 351100
12 Michelob 10240 363800 1601000 333800
13 Michelob 10550 390000 1498000 358000
14 Michelob 9073 391700 1575000 368500
15 Michelob 9507 363700 1358000 358200
16 Porch 15840 303200 3604000 229700
17 Porch 16390 290800 3769000 253900
18 Porch 15340 271900 3476000 222900
19 Porch 17590 284800 3707000 232200
20 Porch 17080 340200 3925000 262200
21 Porch 13380 265900 3595000 223000
22 26-2 Beer 17620 117100 3732000 159900
23 26-2 Beer 16350 136500 3509000 148500
24 26-2 Beer 16460 116100 3364000 143100
25 26-2 Beer 17510 131500 3440000 147500
26 26-2 Beer 15360 116700 3442000 134900
27 26-2 Beer 15770 117400 3539000 144100
28 Marathon 17150 215300 2848000 190200
29 Marathon 17480 146400 3018000 176600
30 Marathon 15450 160200 3003000 205500
31 Marathon 15070 154200 2808000 185300
32 Marathon 15610 158200 2790000 199800
33 Marathon 16610 157700 2788000 205500
names_vec <- c('glycine', 'serine', 'alanine', 'threonine')
p <- list()
for(i in names_vec) {
p[[i]] <- ggplot(raw_data, aes(x = Group, y = raw_data[[i]])) +
geom_boxplot(na.rm = TRUE) +
labs(title = i, y = 'Relative Intensity') +
theme(axis.text.x = element_text(size = 8, angle = 45))
}
pdf(file = 'test1.pdf')
multiplot(p[[1]], p[[2]], p[[3]], p[[4]], cols = 2)
dev.off()
Unfortunately, this yields a page with four plots that are identical save the titles, which are correct.