在大型pdf文件的每一页上放置多个图

时间:2018-10-29 13:54:30

标签: r pdf ggplot2

我正在尝试将数十个ggplot箱形图打印到pdf文件中。我希望每页pdf有四个图。如何使用for循环创建图并最终得到所需的格式?

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()

这是我到目前为止所做的。 par(mfrow = c(2,2))对我不起作用。同样,grid.arrange似乎与循环策略不兼容。

下面的示例代码:

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()

不幸的是,这会产生一个页面,其中包含四个相同的图,除了标题是正确的。

2 个答案:

答案 0 :(得分:1)

您可以使用marrangeGrob()软件包中的gridExtra功能

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
}

将地块列表放入多个页面,每个页面有4个地块

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

## interactive use
ml

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

数据:

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")

reprex package(v0.2.1.9000)于2018-10-29创建

答案 1 :(得分:0)

已编辑回复。这是你所追求的吗?如果没有,请进一步解释。
这似乎是一种情况,即以适当的格式带来数据比摆弄ggplot容易得多。一旦数据为长格式而不是宽格式,就不再需要for循环。 这段代码将产生以下图形:

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 ))

enter image description here