r - Arrange multiple ggplots from a list on multiple pages with specified page breaks -
i'm trying take plots list , output multipage pdf. can using gridextra:marrangegrob i'm having issues getting page breaks in right place. data in sets of 7 4 pages 2 plots on each page , break after 7th plot 8th starting on new page (as after 14th, 21st etc.).
my list contains (currently 84 ggplots, more in future)
i looked @ ggplot list of plots 1 pdf different page layouts don't want have set each page individually there many of them (i might want change 3 or 4 per page once have initial ones , don't want have re-work.
i've made example using diamonds dataframe, assuming want split pages plots 2 different clarities aren't on same page
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)
but has plots continuously no breaks after 7 etc. tried splitting plots list of
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)
but gives: "error in glist(list(list(data = list(carat = c(0.32, 1.01, 0.43, 1.22, : 'grobs' allowed in "glist""
any ideas?
try this,
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()
Comments
Post a Comment