3

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.

My data is here

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")
Steve G
  • 85
  • 9
  • It looks like your `for()` loop sequences through 1, 3, 5, etc. Maybe you want `seq(1, length(plist), 4))`? – aosmith Aug 09 '18 at 15:38
  • I did try that out, and when I use 4 it just puts out 1 page with my first 4 graphs, the other 3 in the list aren't put into the pdf. I do get the error code `Error in gList(Q2 = list(grobs = list(list(x = 0.5, y = 0.5, width = 1, : only 'grobs' allowed in "gList" ` – Steve G Aug 09 '18 at 15:58
  • Please [see here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on posting an R question that is easy to answer. It's preferable that you post a representative sample of your data in the post, such as by using `dput`, instead of linking to a third-party download. Please also try to pare down the code to just the essentials necessary for this problem. – camille Aug 09 '18 at 16:15
  • That said, take a look at the `cowplot` package, which is designed for just this purpose. See their [vignette](https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html) on sticking `ggplot` objects together with shared legends. – camille Aug 09 '18 at 16:16
  • 1
    Because your list isn't divisible by 4, you have `NULL` in your final list you give to `grid.arrange()`. Take a look at `plist[5:(5+3)]` (this is assuming you have less than 8 plots). This is causing the error. Some nice ways to remove `NULL` from a list [here](https://stackoverflow.com/questions/34221981/remove-nulls-from-multiple-lists-in-list). I thought the `purrr::compact()` solution looked pretty handy! – aosmith Aug 09 '18 at 16:17
  • using `plist[5:(5+3)]` I get a blank output. I have 7 plots it should be outputting. When I use`purrr::compact(plist)` it outputs all of my graphs, but I don't see how to use this in grid.arrange? – Steve G Aug 09 '18 at 16:34
  • @SteveG: you can try [marrangeGrob](https://stackoverflow.com/a/50930640/786542) too – Tung Aug 09 '18 at 16:41
  • 1
    @Tung That totally worked and is much simpler, thank you! – Steve G Aug 09 '18 at 16:49
  • @SteveG: great. Please update your question with the final working code to help future readers – Tung Aug 09 '18 at 16:52
  • 1
    Tung & @aosmith Thank you both for your help and actually answering the question – Steve G Aug 09 '18 at 20:33
  • 1
    Glad you worked it out! Rather than adding the solution to your original question, please add it as an answer so this can be marked as solved. – aosmith Aug 10 '18 at 14:59

1 Answers1

3

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")
Steve G
  • 85
  • 9