代码之家  ›  专栏  ›  技术社区  ›  d-cubed Tyler Rinker

从Lappy向PDF添加单独的图例

  •  0
  • d-cubed Tyler Rinker  · 技术社区  · 7 年前

    我创建了一个多页PDF,其中包含由数百个唯一标识符生成的绘图。基本上,我希望每页添加一个单独的图例面板。 PDF基本上按照详细信息构建 here here

    关于如何使用网格为一些图形对象添加单独的图例,有几十个演练。安排,最有希望的是 here here .

    基本上,步骤是:

    1. 创建数据库
    2. 使用Lappy创建图形对象列表,并
    3. 创建pdf—将图形对象列表分块。

    我怀疑这个过程在第3步就失败了——将图例添加到Grob列表中。


    复制 问题所在

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

    生成图例的步骤

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

    使用cowplot获取图例的函数

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

    创建所有图形对象的步骤

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

    制作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阶段正在崩溃。理想情况下,我会将图例作为图形对象添加到网格中/网格处。安排步骤,例如。,

    grobs[12]<- leg
    

    但运气不好,也是 plist() 进程似乎尚未完全转换为图形对象。

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   baptiste    7 年前

    有多种选择( 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()