在pdf中的多个页面上打印多个图

问题描述:

我有很多质谱数据图,我想合理地分布在pdf的几页中.想法是,将图垂直对齐以便于比较.这就是为什么我需要它们具有一定的可读性,并且我希望每组绘图在一页上放在一起,下一组在下一页上依此类推. 到目前为止,我设法做到了:在多个文件上每个文件一页上绘制尺寸合理的图;或:一份pdf中几页上尺寸不佳的图.我想在一张pdf的几页上绘制尺寸合理的图.

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.

我的数据在COMBO

这是我的解决方案代码(1):

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

在我的案例中,这为每组绘图创建了间隔良好的A4 pdf,总共有8个pdf文件.

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

示例(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.

我该怎么做才能将所有通过ggsave的图绘制到一个文件中?或者,如何更改图的尺寸,以使pdf()以合适的尺寸拾取它们?

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?

感谢您的帮助!

尝试一下:

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