代码之家  ›  专栏  ›  技术社区  ›  Alexander

组合“for loop”和ggplotGrob

  •  2
  • Alexander  · 技术社区  · 8 年前

    ggplot2 结合 for loop 并在这篇文章中找到了解决方案 ggplot2-plots-over-multiple-pages .

    但是,我想用 ggplotGrop 用于在此之后减小刻面的条带尺寸 for循环 .

    我在这里提供了前一个问题的可复制示例,仅用于绘图 facets

    library(ggplot2)
    library(vcd) # For the Baseball data
    data(Baseball)
    
    pdf("baseball.pdf", 7, 5)
    
    aa<- for (i in seq(1, length(unique(Baseball$team87)), 6)) {
           print(ggplot(Baseball[Baseball$team87 %in% levels(Baseball$team87)[i:(i+5)], ], 
                          aes(hits86, sal87)) + 
            geom_point() +
            facet_wrap(~ team87) +
            scale_y_continuous(limits=c(0, max(Baseball$sal87, na.rm=TRUE))) +
            scale_x_continuous(limits=c(0, max(Baseball$hits86))) +
            theme_bw())
        }
        dev.off()
    

    想要实现 ggplotGrob 以减小条带尺寸。

    library(gridExtra)
             library(grid)
             g = ggplotGrob(aa)
    
             pos =  c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
             for(i in pos) g$heights[i-1] = unit(0.4,"cm")
    
             grobs = which(grepl("strip", g$layout$name))
             for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc") 
    
            grid.draw(g)
            dev.off()      
    

    plot_clone(plot)中出错:尝试应用非函数

    我只是想知道如何实现 GplotGrop 到那个for循环。

    1 回复  |  直到 7 年前
        1
  •  2
  •   timat    8 年前

    主要的问题是你正在使用 ggplotGrob 在错误的对象上。 grid.arrange 制作多页pdf

    第一种方法:使用技巧 GplotGrob 创建空白页

    pdf("baseball.pdf", 7, 5)
    
    for (i in seq(1, length(unique(Baseball$team87)), 6)) {
      temp <- ggplot(Baseball[Baseball$team87 %in% levels(Baseball$team87)[i:(i+5)], ], 
                   aes(hits86, sal87)) + 
              geom_point(na.rm=TRUE) + ## to avoid warnings
              facet_wrap(~ team87) +
              scale_y_continuous(limits=c(0, max(Baseball$sal87, na.rm=TRUE))) +
              scale_x_continuous(limits=c(0, max(Baseball$hits86))) +
              theme_bw()
      pdf(file=NULL) ## because ggplotGrob will create a blank page
      g <- ggplotGrob(temp)
      pos =  c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
      for(i in pos) g$heights[i-1] = unit(0.4,"cm")
    
      grobs = which(grepl("strip", g$layout$name))
      for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc") 
      dev.off() ## to close the fake device
      grid.arrange(g)
    
    
    
    }
    
    dev.off()
    

    第二种方法:避免使用假冒设备

    plotlist <- list()
    j <- 1
    
    for (i in seq(1, length(unique(Baseball$team87)), 6)) {
      temp <- ggplot(Baseball[Baseball$team87 %in% levels(Baseball$team87)[i:(i+5)], ], 
                   aes(hits86, sal87)) + 
              geom_point(na.rm=TRUE) +
              facet_wrap(~ team87) +
              scale_y_continuous(limits=c(0, max(Baseball$sal87, na.rm=TRUE))) +
              scale_x_continuous(limits=c(0, max(Baseball$hits86))) +
              theme_bw()
      g <- ggplotGrob(temp)
      pos =  c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
      for(i in pos) g$heights[i-1] = unit(0.4,"cm")
    
      grobs = which(grepl("strip", g$layout$name))
      for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc") 
    
      plotlist[[j]] <- g
      j <- j+1
    
    
    }
    pdf("baseball.pdf", 7, 5)
    
    for (i in (1:length(plotlist))) {
      grid.arrange(plotlist[[i]])
    }
    
    dev.off()
    

    实际上,你甚至可以使用 网格排列 GplotGrob facet 使其更加可定制。