I've attempted to use code from the post ggplot2: Plots over Multiple pages. I can get it to work somewhat but I can't seem to figure out how to change the number of graphs to 4 per page. My code will do 4 graphs per page, but on the second page it starts back with the graph that is 3rd in the list (which it already plots of the first page), replots the 4th in the list, then goes to the 5th and 6th while completley skipping the 7th.
I also can't seem to get the text for the left side of the pdf output to work.
Here is the code that I currently have:
plist = lapply(split(ChlFPALL, ChlFPALL$Tank), function(d) {
ggplot(data=d, aes(x=TimePoint, y=Chl, group=Variable, colour=Color, shape=Shape)) +
geom_line(size=1) +
geom_point(size=4) + scale_x_continuous(breaks = seq(0, 2, 1)) +
geom_point() +
facet_wrap(~ Tank) +
scale_y_continuous(limits=c(0, max(ChlFPALL$Chl, na.rm=TRUE))) +
theme(plot.margin=unit(rep(0.4,4),"lines"),
axis.title=element_blank()) + theme(plot.subtitle = element_text(vjust = 1),
plot.caption = element_text(vjust = 1),
axis.text = element_text(size = 10,
face = "bold", colour = "black"),
legend.text = element_text(size = 10,
face = "bold"), legend.key = element_rect(fill = NA),
legend.background = element_rect(fill = NA)) + scale_colour_manual(values = c("Bluegreen" = "#528B8B", "Cryptophyta" = "#8B4513", "Diatoms"="#A52A2A", "Green Algae" = "#008B00", "Total conc." = "#000000", "Yellow substances"= "#EEEE00"))
})
# Four pages of plots in one PDF file
pdf("FPplotsQs.pdf", 11, 8.5)
for (i in seq(1, length(plist), 2)) {
grid.arrange(grobs=plist[i:(i+3)],
ncol=2, left=expression(bold(paste("Chlorophyll", italic(" a "), mu, gL^-1))), bottom="Time (Hours)")
}
dev.off()
Also, is there a way to get just one common legend on the page? I've tried the code from another example but can't make it work.
pdf("FPplotsQs.pdf", 11, 8.5)
for (i in seq(1, length(plist), 2)) {
ggarrange(grobs=plist[i:(i+3)],
ncol=2, left=expression(bold(paste("Chlorophyll", italic(" a "), mu, gL^-1))), bottom="Time (Hours)", common.legend = TRUE)
}
dev.off()
UPDATE: Adding the following code to save the outputs of the graph works perfectly:
plots <- marrangeGrob(plist, nrow = 2, ncol = 2)
ggsave("multipage.pdf", plots, width = 11, height = 8.5, units = "in")