3
votes

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!

1
Have you tried to use par(mfrow = c(9, 1)) for example?F. Privé
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
Maybe you should use your first solution and merge the PDF files later. For example, you can use plotflow:::mergePDF (see stackoverflow.com/a/26846704/6103040).F. Privé
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
Did you use devtools::install_github("trinker/plotflow") to install it? I didn't have to install any packages.F. Privé

1 Answers

1
votes

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()