结合`for loop`和ggplotGrob

时间:2016-11-23 08:54:53

标签: r plot ggplot2

我只想使用ggplot2for loop一起绘制多个方面,并在此帖ggplot2-plots-over-multiple-pages中找到解决方案。

但是,我希望使用ggplotGrop修改这些构面的外观,以便在for loop之后减小构面的条带大小。

我在这里提供了可重复的前一个问题示例,仅用于绘制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)中的错误:尝试应用非功能

我只是想知道如何为循环实现ggplotGrop

1 个答案:

答案 0 :(得分:2)

主要问题是你在错误的对象上使用ggplotGrob。 你必须在每个循环中使用它。 然后你必须grid.arrange来制作多页pdf

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

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

实际上,您甚至可以使用grid.arrangeggplotGrob,而无需使用facet来进行更多自定义。