在具有指定分页符的多个页面上排列列表中的多个 ggplot

Arrange multiple ggplots from a list on multiple pages with specified page breaks

我正在尝试从列表中绘制图表并输出多页 pdf。我可以使用 gridExtra:marrangeGrob 轻松做到这一点,但是我在正确位置获取分页符时遇到了问题。我的数据是 7 组,所以我想要 4 页,每页有两个图,然后在第 7 个图之后休息,第 8 个从新页面开始(如第 14、21 等之后)。

我的列表包含(目前有 84 个 ggplots,将来可能会更多)

我查看了 ,但我不想单独设置每个页面,因为它们太多了(一旦我有,我也可能想更改为每页 3 或 4 个)最初的那些,不想重新工作。

我已经使用 diamonds 数据框做了一个例子,假设我想拆分页面,这样来自两个不同清晰度的图不在同一页面上

egsplitdf <- diamonds %>% distinct(color, clarity) %>% arrange(clarity)
egPlotfun <- function(i, filterdf){
  dat = filter(diamonds, color == filterdf[["color"]][i] & clarity == 
       filterdf[["clarity"]][i])
  ggplot(dat, aes(x= x, y = y))+
       geom_point()+
       labs(title = paste(filterdf[["clarity"]][i], filterdf[["color"]][i]))
}

egPlots <- lapply(1:56, egPlotfun,filterdf = egsplitdf)
ArrangedPlots <- marrangeGrob(egPlots, nrow = 2, ncol = 1)
ggsave("egNotsplit.pdf", ArrangedPlots, height = 10,width = 7)

但这只是情节连续不断,在 7 等之后没有中断。 我还尝试将我的地块分成

列表
plotseq <- lapply(0:8,function(x) seq(from = (x*7+1), length.out = 7))
ListofPlots <- lapply(plotseq, function(x) lapply(x, egPlotfun, filterdf = egsplitdf ))
testSplit <-marrangeGrob(ListofPlots , nrow = 2, ncol = 1)
ggsave("TrySplit.pdf", testSplit, height = 10,width = 7)

但这给出了: “gList(list(list(data = list(carat = c(0.32, 1.01, 0.43, 1.22, : 在 "gList""

中只允许 'grobs'

有什么想法吗?

试试这个,

library(gridExtra)
library(ggplot2)

pl <- lapply(1:84, function(i) ggplot() + ggtitle(i))

spl <- split(pl, (seq_along(pl)-1) %/% 7)
ppl <- lapply(spl, function(g) marrangeGrob(grobs = g, layout_matrix = matrix(c(1,2))))

pdf("test.pdf")
print(ppl)
dev.off()