3

I have a lot of plots of mass spec data that I want to spread out reasonably over several pages in a pdf. The idea is, that the plots are aligned vertically for easy comparison. That's why I need them to have a certain readable dimension, and I want each set of plots to go together on one page, the next set on the next page and so on. So far, I managed to get either: well dimensioned plots on 1 page per file for multiple files; or: poorly dimensioned plots on several pages in one pdf. I'd like to have well dimensioned plots on several pages in one pdf.

My data is in COMBO

Here is my code for solution (1):

totalrows <- nrow(COMBO)
pagesneeded <- ceiling(totalrows/9)

for(i in 1:pagesneeded){
combolongrow <- melt(COMBO, id.vars = "UnID")
pl<- ggplot(combolongrow, aes(x=variable , y=value, group=UnID)) +
    geom_line() +
    theme(strip.text.y = element_text(size=6)) +
    xlab("Fraction") +
    ylab("iBAQ") +
    facet_wrap_paginate(~UnID, ncol = 1, nrow = 9, page = i, 
    strip.position="top", scales="free_y") 
    ggsave(paste("plot-", i, ".pdf", sep=""), width=21, height=29, units ="cm", dpi = 300)
}

This creates a nicely spaced A4 pdf for each set of plots for a total of 8 pdf files in my case.

Output: all spacing is nice, but every page is a separate file All spacing is nice, but every page is a separate file.

Example (2):

totalrows <- nrow(COMBO)
pagesneeded <- ceiling(totalrows/9)

pdf("Plots.pdf", paper = "a4")
for(i in 1:pagesneeded){
combolongrow <- melt(COMBO, id.vars = "UnID")
pl<- ggplot(combolongrow, aes(x=variable , y=value, group=UnID)) +
    geom_line() +
    theme(strip.text.y = element_text(size=6)) +
    xlab("Fraction") +
    ylab("iBAQ") +
    facet_wrap_paginate(~UnID, ncol = 1, nrow = 9, page = i, strip.position="top", scales="free_y") 

   print(pl)
}
dev.off()

This creates one file (yay), but the plots are sized to the default graphics device size, which is the wrong aspect ratio and leaves large margins on all four sides of my plots and makes the data unreadable.

Output: Spacing is bad, but all pages are automatically put together in one document. Spacing is bad, but all pages are automatically put together in one document.

What can I do to get all plots through ggsave into one single file? Or how can I change the dimensions of my plots so that pdf() picks them up in the right size?

Thanks for your help!

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
Joram
  • 139
  • 5
  • 11
  • Have you tried to use `par(mfrow = c(9, 1)) ` for example? – F. Privé Jul 20 '17 at 19:25
  • Where would I add that to do what? I tried it now with both possibilities and couldn't change the output. I tried reading about `par()` but did not fully understand what it does. – Joram Jul 21 '17 at 13:46
  • Maybe you should use your first solution and merge the PDF files later. For example, you can use `plotflow:::mergePDF` (see https://stackoverflow.com/a/26846704/6103040). – F. Privé Jul 21 '17 at 14:53
  • Thanks. I tried, but it leads me down a rabbit hole of packages to install, that for whatever reason, throw lots of errors on my R installation (RStudio, R 3.4.x). I guess my ultimate solution will be to use an external pdf merger to combine the output files into one large pdf. – Joram Jul 25 '17 at 09:18
  • Did you use `devtools::install_github("trinker/plotflow")` to install it? I didn't have to install any packages. – F. Privé Jul 25 '17 at 09:26
  • @Joram I tried to use your code and parts of the code from ggforce manual to plot facets on different pages. It worked fine and gave me what I wanted but resulted in several errors as well. I will highly appreciate if you could have a look at the error in this question https://stackoverflow.com/questions/45475249/save-facet-wrap-on-multiple-pages-using-ggforce-ggplus – shiny Aug 03 '17 at 10:10

1 Answers1

1

Try this:

pdf("Plots.pdf", paper = "a4")
par.save <- par(mfrow = c(4, 1))
for(i in 1:20) {
  plot(1:10)
}
par(par.save)
dev.off()
F. Privé
  • 11,423
  • 2
  • 27
  • 78