Putting multiple plots on each page of a large pdf

2019-07-29 01:55发布

问题:

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.

回答1:

Edited response. Is this what you were after? If not please explain further.
This seems like a case where bringing the data in the proper format is much easier than fiddling with ggplot. Once the data is in the long format instead of wide format the for loop is not necessary anymore. This code produces the following graph:

library(tidyverse)
raw_data = read_delim("stackoverflowdata.csv", col_names = TRUE, delim = ";") %>%
  gather(compound, value, -Group)

ggplot(raw_data,aes(x=Group, y =value)) +
  geom_boxplot(na.rm = TRUE) +
  facet_wrap(vars(compound), scales="free_y") +
  labs(y = 'Relative Intensity') +
  theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1 ))



回答2:

You can use marrangeGrob() function from the gridExtra package

library(ggplot2)

names_vec <- c('glycine', 'serine', 'alanine', 'threonine')
plot_lst <- vector("list", length = length(names_vec))

for (i in seq_along(names_vec)) {
  g <- 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))
  plot_lst[[i]] <- g
}

Put plot list into multiple pages each of which is has 4 plots

library(gridExtra)
ml <- marrangeGrob(plot_lst, nrow = 2, ncol = 2)

## interactive use
ml

## non-interactive use, multipage pdf
## ggsave("multipage.pdf", ml)

Data:

library(readr)

raw_data <- read_table("ID 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")

Created on 2018-10-29 by the reprex package (v0.2.1.9000)



标签: r pdf ggplot2