在大型pdf文件的每一页上放置多个图 [英] Putting multiple plots on each page of a large pdf file
问题描述
我正在尝试将数十个ggplot箱形图打印到pdf文件中.我希望每页pdf包含四个图.如何使用for循环创建图并最终得到所需的格式?
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()
这是我到目前为止所做的.par(mfrow = c(2,2))对我不起作用.同样,grid.arrange似乎与循环策略不兼容.
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.
下面的示例代码:
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.
推荐答案
已编辑的响应.这是你所追求的吗?如果没有,请进一步解释.
这似乎是一种情况,即以适当的格式带来数据比摆弄ggplot容易得多.一旦数据为长格式而不是宽格式,就不再需要for循环.这段代码将产生以下图形:
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 ))
这篇关于在大型pdf文件的每一页上放置多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!