2016-06-16 7 views
2

私は2つのファセットggplotオブジェクトを作成しようとしており、グリッドを2つの列(grid.arrange列)に整列しようとしています。 2番目の列では、各ファセットメンバーのヒストグラムを挿入したいと思います。 2つの列のデータソースも異なります。これまでは、2つの面取りされたggplotオブジェクトをグリッドに配置することしかできませんでした。 grid.arrangeの2番目の列にどのようにインセットするかを知る必要があります。'facet_grid'edとgrid.arrange'edプロットの刺しゅう

以下のコードは、私のデータを非常に単純化したものです。

#Two data sources (d1,d2). They will have even different facetting parameters as well, 
#but here I have simplified it. 

d1<-data.frame(FacetRow=c(rep('a',4),rep('b',4)), 
      FacetCol=rep(c(rep('c',2),rep('d',2)),2), 
      X=rep(c(1,2),4), 
      Y=runif(8) 
      ) 
d2<-data.frame(FacetRow=c(rep('a',4),rep('b',4)), 
       FacetCol=rep(c(rep('c',2),rep('d',2)),2), 
       X=rep(c(1,2),4), 
       Y=runif(8) 
) 

#GGPlot object for First column of grid.arrange 
g.col.1<-ggplot(data=d1,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) 
#GGPlot object for Second column of grid.arrange 
g.col.2<-ggplot(data=d2,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) 
grid.arrange(g.col.1,g.col.2,ncol=2) 

Thisです。だから私は、(e、f、g)X(h、i)プロットのそれぞれの値のヒストグラムを挿入する方法を知る必要があります。

また、私はd1とd2を変えることによってpdfの別のページにこれをプロットしておく必要があります。だから、私は最終的に作成されたggplotオブジェクトを一度しか印刷することができないと思うし、d1 & d2を変更してからもう一度印刷してください。

誰でもこの問題を解決できる場合は、感謝します。

感謝

答えて

3

annotation_customは、残念ながら、(私に聞かないでください)異なるパネルに異なるgrobsを配置することを意図していない、便利です。あなたは自動的にこれらのアノテーションレイヤを生成するためにplyrを使用することができ、例えば:この最小限の変更には、注意

library(ggplot2) 
library(gridExtra) 
annotation_custom2 <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data) 
    { 
    layer(data = data, stat = StatIdentity, position = PositionIdentity, 
      geom = ggplot2:::GeomCustomAnn, 
      inherit.aes = TRUE, params = list(grob = grob, 
              xmin = xmin, xmax = xmax, 
              ymin = ymin, ymax = ymax)) 
    } 

inset <- qplot(rnorm(10)) 

p1 <- ggplot(data=d1,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) 
p2 <- ggplot(data=d2,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) + 
    annotation_custom2(ggplotGrob(inset), data=data.frame(FacetRow="b", FacetCol = "c", X=1, Y=1), 
        xmin=-Inf, xmax=1.5, ymin=-Inf, ymax=0.6) 

grid.arrange(p1, p2, ncol=2) 

enter image description here

、それを回避します

library(plyr) 

insets <- dlply(d2, c("FacetRow", "FacetCol"), function(d){ 
    annotation_custom2(grob = ggplotGrob(qplot(d$Y) + 
             ggtitle(sprintf("col: %s\nrow: %s", 
                 unique(d$FacetCol), unique(d$FacetRow)))), 
        data = data.frame(X=min(d$X), Y=min(d$Y), 
             FacetRow=unique(d$FacetRow), 
             FacetCol=unique(d$FacetCol)), 
        xmin=-Inf, xmax=1.5, ymin=-Inf, ymax=0.6) 
}) 

p1 <- ggplot(data=d1,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) 
p2 <- ggplot(data=d2,aes(x=X,y=Y))+geom_line()+facet_grid(FacetRow~FacetCol) + 
    insets 

grid.arrange(p1, p2, ncol=2) 

enter image description here

+0

ソリューションバティストのおかげで、しかし、あなたは私が(つまり、各FacetRowsとFacetCols組み合わせで別々にある)各パネル内の値(Y値)のヒストグラムを計算する方法を知っていることができますか?今では、各パネルに対して一定のヒストグラムとして 'qplot(rnorm(10))'を使用しています。これらをデータフレームから自動的に推論する方法はありますか? –

関連する問題