从lapply添加单独的图例到PDF

问题描述 投票:0回答:1

我创建了一个多页PDF,其中包含从几百个唯一标识符生成的图。基本上,我想在每页添加一个单独的图例面板。 PDF基本上构造为详细的herehere

关于如何使用grid.arrange为一些图形对象添加单独的图例有几十个步骤,最有希望的是herehere

基本上,步骤是:

  1. 创建数据库
  2. 使用lapply来创建图形对象列表
  3. 在图形对象列表中创建一个pdf块。

我怀疑这个过程在第3步崩溃了 - 将图例添加到了grob列表中。


To reproduce the problem

color.names <- setNames(c("A", "B", "C", "D", "F"), c("green3", "chocolate1", "darkgoldenrod1", "firebrick1"))    

group.colors <- c(A = "#333BFF", B = "#CC6600", C ="#9633FF", D = "#E2FF33", F = "#E3DB71")

SOexample <- data.frame(
        studentid = runif(100,min=500000, max=999999),
        grade = runif(100, min=20, max=100),
        lettergrade = sample(c("A", "B","C","D","F"),size=100,replace=TRUE),
        firstname = sample(c("Alan", "Billy","Charles","Donna","Felicia"),size=100,replace=TRUE)
        )

To generate the legend

df <- SOexample
gl <- ggplot(df, aes(x=" ", y=as.numeric(grade), ymin=50, ymax=100))+ geom_boxplot()+ guides(fill=FALSE) + geom_point(aes(colour=lettergrade)) + labs( x=" ", y=" ") + ggtitle(sprintf("%s", df$firstname), aes(cex=.05)) + scale_colour_manual(name="Number", values=group.colors) +              scale_fill_manual(name="", values="red") + theme_grey() +               theme(legend.position="none", plot.title = element_text(size = 8, face = "bold"), plot.subtitle=element_blank()) +                theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) 

The function to grab the legend using cowplot

install.packages("cowplot")
library(cowplot)
leg <- get_legend(gs + theme(legend.position="right"))

To make all the graphical objects

plist = lapply(split(SOexample, factor(SOexample$studentid)), function(df) { ggplot(df, aes(x=" ", y=as.numeric(grade), ymin=50, ymax=100))+               geom_boxplot()+ guides(fill=FALSE) + geom_point(aes(colour=lettergrade)) +                labs( x=" ", y=" ") + ggtitle(sprintf("%s", df$firstname), aes(cex=.05)) + scale_colour_manual(name="Number", values=group.colors) +                scale_fill_manual(name="", values="red") + theme_grey() +theme(legend.position="none", plot.title = element_text(size = 8, face = "bold"), plot.subtitle=element_blank()) + theme(axis.title.x=element_blank(),axis.text.x=element_blank(), axis.ticks.x=element_blank())})

Making the PDF

   pdf("allpeople.pdf", pointsize=8)
    for (i in seq(1, length(plist), 11)) {
        grid.arrange(grobs=plist[i:(i+11)],
                     ncol=4, left="Magic Numbers", bottom=" ")
    }
    dev.off()

我怀疑创建PDF阶段的过程正在崩溃。理想情况下,我会将图例添加为grid.arrange步骤中的图形对象,例如,

grobs[12]<- leg

但没有运气,而且plist()进程中的最后一项似乎还没有完全转换为图形对象。

使用这种自动生成方法,即无法单独列出图形对象,如何将图例添加到PDF的每个页面?

r ggplot2 pdf-generation r-grid
1个回答
1
投票

有各种选项(例如ggsave('file.pdf',marrangeGrob(plist,ncol=4,nrow=3))),但我可能会做这样的事情来实现更好的控制:

pl <- split(plist, gl(10,10))
pdf("allpeople.pdf", pointsize=8)
for (i in seq_along(pl)) {
  grid.arrange(grobs=c(pl[[i]], list(leg)), 
               ncol=4, 
               left="Magic Numbers", 
               bottom=" ")
}
dev.off()
© www.soinside.com 2019 - 2024. All rights reserved.