2016-11-23 8 views
2

と組み合わせてggplot2と組み合わせて複数のファセットをプロットするだけで、このポストの解決策が見つかりましたggplot2-plots-over-multiple-pages`forループ 'とggplotGrobを組み合わせる

しかし、このfor loop以降のファセットのストリップサイズを減らすために、これらのファセットの外観をggplotGropに変更したいとします。

私はここだけストリップサイズを減らすためにggplotGrobを実装したい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() 

をプロットするための前の質問の再現性の例を提供しています。

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でエラーが発生しました(プロット):非機能

を適用しようとすると、私はちょうどループのためにそれにggplotGropを実装する方法を疑問に思います。

答えて

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を使用せずにさらにカスタマイズすることができます。

+0

お寄せいただきありがとうございます! – Alexander

関連する問題