简体   繁体   English

将多个 ggplots 打印到单个 pdf 中,每页多个图

[英]Printing multiple ggplots into a single pdf, multiple plots per page

I have a list, p , where each element of p is a list of ggplot2 plotting objects.我有一个列表p ,其中p的每个元素都是 ggplot2 绘图对象的列表。

I would like to output a single pdf containing all the plots in p such that the plots in p[[1]] are on page 1, the plots in p[[2]] are on page 2, etc. How might I do this?我想 output 单个 pdf 包含p中的所有图,这样p[[1]]中的图在第 1 页, p[[2]]中的图在第 2 页等。我该怎么办这个?

Here's some example code to provide you with the data structure I'm working with--apologies for the boring plots, I picked variables at random.下面是一些示例代码,为您提供我正在使用的数据结构——为无聊的情节道歉,我随机选择了变量。

require(ggplot2)
p <- list()

cuts <- unique(diamonds$cut)
for(i in 1:length(cuts)){
    p[[i]] <- list()
    dat <- subset(diamonds, cut==cuts[i])
    p[[i]][[1]] <- ggplot(dat, aes(price,table)) + geom_point() + 
        opts(title=cuts[i])
    p[[i]][[2]] <- ggplot(dat, aes(price,depth)) + geom_point() + 
        opts(title=cuts[i])
}

This solution is independent of whether the lengths of the lists in the list p<\/code> are different.该解决方案与列表p<\/code>中的列表长度是否不同无关。

library(gridExtra)

pdf("plots.pdf", onefile = TRUE)
for (i in seq(length(p))) {
  do.call("grid.arrange", p[[i]])  
}
dev.off()

Here is a simpler version of Sven's solution for the R beginners who would otherwise blindly use the do.call and nested lists that they neither need nor understand.这是 Sven 解决方案的更简单版本,适用于 R 初学者,否则他们会盲目使用他们既不需要也不理解的 do.call 和嵌套列表。 I have empirical evidence.我有经验证据。 :) :)

library(ggplot2)
library(gridExtra)

pdf("plots.pdf", onefile = TRUE)
cuts <- unique(diamonds$cut)
for(i in 1:length(cuts)){
    dat <- subset(diamonds, cut==cuts[i])
    top.plot <- ggplot(dat, aes(price,table)) + geom_point() + 
        opts(title=cuts[i])
    bottom.plot <- ggplot(dat, aes(price,depth)) + geom_point() + 
        opts(title=cuts[i])
    grid.arrange(top.plot, bottom.plot)
}
dev.off()

Here is the most elegant solution to exporting a list of ggplot objects into a single pdf file using ggplot2::ggsave()<\/code> and gridExtra::marrangeGrob()<\/code> .这是使用ggplot2::ggsave()<\/code>和gridExtra::marrangeGrob()<\/code>将 ggplot 对象列表导出到单个 pdf 文件的最优雅的解决方案。

library(ggplot2)
library(gridExtra)

I've tried some of these solutions but with no success.我已经尝试了其中一些解决方案,但没有成功。 I researched a little more and found a solution that worked perfectly for me.我进行了更多研究,并找到了一个非常适合我的解决方案。 It saves all my graphics in a single pdf file, each chart on one page.它将我所有的图形保存在一个 pdf 文件中,每个图表在一页上。

library(ggplot2)


pdf("allplots.pdf",onefile = TRUE)
for(i in glist){
   tplot <- ggplot(df, aes(x = as.factor(class), y = value))
   print(tplot)
}
dev.off()

Here's one solution, but I don't particularly like it:这是一种解决方案,但我不是特别喜欢它:

ggsave("test.pdf", do.call("marrangeGrob", c(unlist(p,recursive=FALSE),nrow=2,ncol=1)))<\/code>

The problem is that it relies on there being the same number of plots in each group.问题在于它依赖于每组中的地块数量相同。 If all(sapply(p, length) == 2)<\/code> were false, then it would break.如果all(sapply(p, length) == 2)<\/code>为假,那么它就会中断。

"

Here's a function based on Sven's approach, including the roxygen2 documentation and an example.这是一个基于 Sven 方法的函数,包括roxygen2文档和一个示例。

#' Save list of ggplot2 objects to single pdf
#'
#' @param list (list) List of ggplot2 objects.
#' @param filename (chr) What to call the pdf.
#'
#' @return Invisible NULL.
#' @export
#'
#' @examples
#' #plot histogram of each numeric variable in iris
#' list_iris = map(names(iris[-5]), ~ggplot(iris, aes_string(.)) + geom_histogram())
#' #save to a single pdf
#' GG_save_pdf(list_iris, "test.pdf")
GG_save_pdf = function(list, filename) {
  #start pdf
  pdf(filename)

  #loop
  for (p in list) {
    print(p)
  }

  #end pdf
  dev.off()

  invisible(NULL)
}

A solution that worked for me with ggpubr<\/code> package (package on Github, code for installation: devtools::install_github("kassambara\/ggpubr")<\/code> ).一个使用ggpubr<\/code>包对我ggpubr<\/code>解决方案(Github 上的包,安装代码: devtools::install_github("kassambara\/ggpubr")<\/code> )。

Let's say you have 4 plots p1, p2, p3 and p4.假设您有 4 个图 p1、p2、p3 和 p4。

library(ggpubr)
multi.page <- ggarrange(p1,p2,p3,p4, nrow=1, ncol=1) # for one plot per page
multi.page[[1]] # for seeing the first plot
ggexport(multi.page, filename="my-plots.pdf")

A nice solution without the gridExtra package:没有gridExtra包的好解决方案:

library(plyr)
library(ggplot2)

li = structure(p, class = c("gglist", "ggplot"))
print.gglist = function(x, ...) l_ply(x, print, ...)
ggsave(li, file = "test.pdf")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM