R 多页,每页有多个ggplot2图形和表格,来自多个数据帧,每一页都是一个公共因子的级别
我有不同的数据帧,提供同一因素的相同水平的信息。 最后,我希望有一个pdf,每个页面包含ggplot2图形、gridExtra表格和文本作为标题,来自不同的数据帧。每页将显示1级因子的信息。 使用cowplot,我成功地组织了1页,但我找不到一种方法来创建for循环或其他任何方法来自动组织~1000页 以下是我的数据的可复制示例:R 多页,每页有多个ggplot2图形和表格,来自多个数据帧,每一页都是一个公共因子的级别,r,ggplot2,gridextra,cowplot,R,Ggplot2,Gridextra,Cowplot,我有不同的数据帧,提供同一因素的相同水平的信息。 最后,我希望有一个pdf,每个页面包含ggplot2图形、gridExtra表格和文本作为标题,来自不同的数据帧。每页将显示1级因子的信息。 使用cowplot,我成功地组织了1页,但我找不到一种方法来创建for循环或其他任何方法来自动组织~1000页 以下是我的数据的可复制示例: # Loading necessary packages library(dplyr) library(ggplot2) library(grid) library(
# Loading necessary packages
library(dplyr)
library(ggplot2)
library(grid)
library(gridExtra)
library(gtable)
library(cowplot)
# Creating first data frame df1
fact <- c("level1", "level1", "level1", "level1", "level1", "level1", "level2", "level2", "level2", "level2", "level2", "level2")
fact <- as.factor(fact)
x1 <- c("1A", "1A", "1A", "1B", "1B", "1B", "1A", "1A", "1A", "1B", "1B", "1B")
x2 <- c(0, 1, 5, 0, 1, 5, 0, 1, 5, 0, 1, 5)
x3 <- c(5, 2, 4, 5, 6, 3, 2, 5, 6, 4, 6, 8)
df1 <- cbind.data.frame(fact, x1, x2, x3)
# Creating second data frame df2
fact <- c("level1", "level1", "level2", "level2")
fact <- as.factor(fact)
x4 <- c("1A", "1B", "1A", "1B")
x5 <- c("good", "bad", "good", "good")
df2 <- cbind.data.frame(fact, x4, x5)
# filtering the data frames to keep 1 level of factor fact
i <- "level1"
df1_plot <- df1 %>% filter(fact == i)
df2_table <- df2 %>% filter(fact == i)
# defining ggplot graphs and gridExtra table
plot1 <- ggplot(data = df1_plot, aes(x = x2, y = x3, color = x1)) + geom_line()
plot2 <- ggplot(data = df1_plot, aes(x = x2, y = x3, color = x1)) + geom_point()
table1 <- tableGrob(df2_table, theme = ttheme_minimal(), rows = NULL)
# Plotting everything in place and adding the level (i) as title of the page
pdf(file = sprintf("%s.pdf", i), width = 9, height = 12, onefile = TRUE)
table_drawn <- ggdraw() + draw_grob(table1)
right_column <- plot_grid(table_drawn, plot1, labels = c("B", "C"), ncol = 1, rel_heights = c(1, 3), scale = 0.9)
bottom_row <- plot_grid(plot2, right_column, labels = c("A", ""), nrow = 1, rel_widths = c(1.5, 2))
title1 <- ggdraw() + draw_label(i, fontface='bold', x = 0, y = 0.5, hjust = 0, vjust = 1, size = 14)
upper_row <- plot_grid(title1, hjust = 0, ncol = 1)
plot_grid(upper_row, bottom_row, ncol=1, rel_heights=c(0.1, 1))
dev.off()
#加载必要的包
图书馆(dplyr)
图书馆(GG2)
图书馆(网格)
图书馆(gridExtra)
图书馆(gtable)
图书馆(cowplot)
#创建第一个数据帧df1
事实这里有一个更简单的方法。首先,我们将两个数据帧绑定到一个数据帧中。然后在lappy
中,我们按fact
分割数据帧,并为fact
的每一层依次创建整个布局:
dat = bind_rows(df1, df2 %>% rename(x1=x4), .id="df")
lapply(split(dat, dat$fact), function(d) {
pdf(paste0(unique(d$fact),".pdf"),9,12)
p = ggplot(d %>% filter(df==1), aes(x2, x3, colour=x1))
grid.draw(plot_grid(p + geom_point(),
plot_grid(tableGrob(d %>% filter(df==2) %>% select(fact,x1,x5),
theme=ttheme_minimal(), rows=NULL),
p + geom_line(),
ncol=1, rel_heights=c(0.1,1)),
ncol=2))
dev.off()
})
除了lappy
,您还可以使用purrr
包中的map
:
library(purrr)
split(dat, dat$fact) %>% map(function(d) {
[...same as above...]
})
以下是两个图中的第一个图:
sprintf中出现错误(“%s.pdf”,test):未找到对象“test”
是≈1000个图/页真的有用吗?根据使用情况,一个动态的绘图或闪亮的绘图将更容易处理。你也可以考虑简单地使用像GHESTScript之类的东西来合并所有的PDF文件,如@ 42:对不起,编辑!阿利斯泰尔:科学出版物。。。我完全同意这是他们应该采取的方向,谢谢你的建议!谢谢,成功了!标题有一个额外的绘图网格级别。