21

ggplot2 に挑戦したラティシストは助けを必要としています: ヒストグラムのファセットごとの可変ブレークを要求する構文は何ですか?

library(ggplot2)
d = data.frame(x=c(rnorm(100,10,0.1),rnorm(100,20,0.1)),par=rep(letters[1:2],each=100))
# Note: breaks have different length by par
breaks = list(a=seq(9,11,by=0.1),b=seq(19,21,by=0.2))
ggplot(d, aes(x=x) ) + 
  geom_histogram() + ### Here the ~breaks should be added
  facet_wrap(~ par,  scales="free")

jucorが指摘したように、ここでさらにいくつかの解決策があります。

特別なリクエストに応じて、なぜ私が ggplot の大ファンではないのかを示すために、latticeバージョン

library(lattice)
d = data.frame(x=c(rnorm(100,10,0.1),rnorm(100,20,0.1)),par=rep(letters[1:2],each=100))
# Note: breaks have different length by par
myBreaks = list(a=seq(8,12,by=0.1),b=seq(18,22,by=0.2))
histogram(~x|par,data=d,
          panel = function(x,breaks,...){
            # I don't know of a generic way to get the 
            # grouping variable with histogram, so 
            # this is not very generic
            par = levels(d$par)[which.packet()]
            breaks = myBreaks[[par]]
            panel.histogram(x,breaks=breaks,...)
          },
          breaks=NULL, # important to force per-panel compute
          scales=list(x=list(relation="free")))

ここに画像の説明を入力

4

4 に答える 4

17

1 つの代替手段を次に示します。

hls <- mapply(function(x, b) geom_histogram(data = x, breaks = b), 
              dlply(d, .(par)), myBreaks)
ggplot(d, aes(x=x)) + hls + facet_wrap(~par, scales = "free_x")

ここに画像の説明を入力

x の範囲を縮小する必要がある場合は、

hls <- mapply(function(x, b) {
  rng <- range(x$x)
  bb <- c(rng[1], b[rng[1] <= b & b <= rng[2]], rng[2])
  geom_histogram(data = x, breaks = bb, colour = "white")
}, dlply(d, .(par)), myBreaks)

ggplot(d, aes(x=x)) + hls + facet_wrap(~par, scales = "free_x")

ここに画像の説明を入力

于 2013-06-24T16:10:11.777 に答える
5

各ファセットに異なるブレークポイントを与えることはできないと思います。

回避策として、2 つのプロットを作成しgrid.arrange()、ライブラリの関数を使用gridExtraしてそれらをまとめることができます。geom_histogram()使用中のブレークポイントを設定しbinwidth=、ビンの幅に 1 つの値を設定します。

p1<-ggplot(subset(d,par=="a"), aes(x=x) ) + 
  geom_histogram(binwidth=0.1) +
  facet_wrap(~ par)

p2<-ggplot(subset(d,par=="b"), aes(x=x) ) + 
  geom_histogram(binwidth=0.2) +
  facet_wrap(~ par)
library(gridExtra)
grid.arrange(p1,p2,ncol=2)

ここに画像の説明を入力

于 2013-06-24T09:28:29.010 に答える
4

Didzis の例に続きます。

ggplot(dat=d, aes(x=x, y=..ncount..)) +
  geom_histogram(data = d[d$par == "a",], binwidth=0.1) +
  geom_histogram(data = d[d$par == "b",], binwidth=0.01) +  
  facet_grid(.~ par, scales="free")

編集:これはより多くのレベルで機能しますが、もちろん、より良い解決策が既にあります

# More facets
d <- data.frame(x=c(rnorm(200,10,0.1),rnorm(200,20,0.1)),par=rep(letters[1:4],each=100))

# vector of binwidths same length as number of facets - need a nicer way to calculate these
my.width=c(0.5,0.25,0.1,0.01)

out<-lapply(1:length(my.width),function(.i) data.frame(par=levels(d$par)[.i],ggplot2:::bin(d$x[d$par==levels(d$par)[.i]],binwidth=my.width[.i])))

my.df<-do.call(rbind , out)

ggplot(my.df) + geom_histogram(aes(x, y = density, width = width), stat =  "identity") + facet_wrap(~par,scales="free")

from https://groups.google.com/forum/?fromgroups=#!searchin/ggplot2/bin $20histogram$20by$20facet/ggplot2/xlqRIFPP-zE/CgfigIkgAAkJ

于 2013-06-24T22:20:17.167 に答える