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
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.
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!
par(mfrow = c(9, 1))
for example? – F. Privépar()
but did not fully understand what it does. – Joramplotflow:::mergePDF
(see stackoverflow.com/a/26846704/6103040). – F. Privédevtools::install_github("trinker/plotflow")
to install it? I didn't have to install any packages. – F. Privé