print several plots in PDF

Hi,

I work in agronomy, I study the density along pieces of wood.
With R I know how to draw the density profile for many cores and print them in a PDF, one core per page.
But now I have more than 1000 cores to draw, and I don't want to have 1000 pages in my PDF. So I would like to have 9 plots on each page (a square of 3x3) to reduce the number of pages.
And I don't find how to do it, because i don't want to organise differents plots with facet_grid, and I don't know how much pages I will have (it depends on the species)

I put my code below, it's the code I use for one plot per page.

pdf("TEST.pdf")
by(data, data$Code,function(x) {
ggplot()+
geom_line(x, mapping=(aes(x=posR,y=dens)))+
geom_point(x,mapping=(aes(x=posR,y=moyseg)),color="#33CCFF",size=2)+
geom_line(x,mapping=(aes(x=posR,y=Dr1)),color="#FF0033",size=1,linetype="dashed")+
labs(x="position relative", y="densité", title = unique(x$lib_espar))
})
dev.off()‚Äč

If anyone have a clue or an idea i'd be very grateful :slight_smile:

Hi,

ggpubr is a good solution :

library("ggpubr")
manchots = palmerpenguins::penguins
var_num = names(manchots)[sapply(manchots, is.numeric)]
graphiques = as.list(numeric(6))

k = 0
for (i in seq_along(var_num)) {
  for (j in seq_len(i-1)) {
    k = k+1
    graphiques[[k]] = ggplot(manchots, aes_string(var_num[i], var_num[j], col = "species")) + geom_point()
  }
}

gg = ggarrange(plotlist = graphiques, ncol = 2, nrow = 1, common.legend = TRUE)      
ggexport(filename = "test.pdf", plotlist = gg, width = 12, height = 6)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.