2016-05-07 5 views
3

私は、タスクを解決するために必要とされたどのように多くの分を私に伝えたデータがあります。濃度ヒストグラム:ラベルのバーの高さ

dat = data.frame(a = c(5.5,7,4,20,4.75,6,5,8.5,10,10.5,13.5,14,11)) 

を私はggplot2パッケージとデータの濃度ヒストグラムをプロット:

p=ggplot(dat, aes(x=a)) + geom_histogram(aes(y=..density..),breaks = seq(4,20,by=2))+xlab("Required Solving Time") 

ここでは、すべての密度バーの高さのラベルをその上に追加したいと思います。 +geom_text(label=..density..)を追加してこの に連絡しようとしました。 これはエラー

オブジェクトの..densityを.. '返すしかし

が見つかりません。誰もがgeom_text()関数の入力が 私の場合はそれらのラベルを取得するには何を知っていますか?

geom_text()を除いた解決策も問題ありませんが、ggplot2パッケージ内にとどまるのはむしろ です。

+2

これはあなたの後ですか? http://stackoverflow.com/questions/24198896/how-to-get-data-labels-for-a-histogram-in-ggplot2/24199013#24199013 – MrFlick

+0

ええ私はこの回答を見たときに私はstackoverflowを検索しましたが、私の場合は密度ヒストグラムであり、絶対周波数バーではない。私はその答えから私の問題の解決策を導くことはできませんでした... – Alias

答えて

2

あなたはggplot_build()でそれを行うことができます。

library(ggplot2) 
dat = data.frame(a = c(5.5,7,4,20,4.75,6,5,8.5,10,10.5,13.5,14,11)) 
p=ggplot(dat, aes(x=a)) + 
    geom_histogram(aes(y=..density..),breaks = seq(4,20,by=2))+xlab("Required Solving Time") 

ggplot_build(p)$data 
#[[1]] 
#   y count x xmin xmax density ncount ndensity PANEL group ymin  ymax colour fill size linetype alpha 
#1 0.19230769  5 5 4 6 0.19230769 1.0  26.0  1 -1 0 0.19230769  NA grey35 0.5  1 NA 
#2 0.03846154  1 7 6 8 0.03846154 0.2  5.2  1 -1 0 0.03846154  NA grey35 0.5  1 NA 
#3 0.07692308  2 9 8 10 0.07692308 0.4  10.4  1 -1 0 0.07692308  NA grey35 0.5  1 NA 
#4 0.07692308  2 11 10 12 0.07692308 0.4  10.4  1 -1 0 0.07692308  NA grey35 0.5  1 NA 
#5 0.07692308  2 13 12 14 0.07692308 0.4  10.4  1 -1 0 0.07692308  NA grey35 0.5  1 NA 
#6 0.00000000  0 15 14 16 0.00000000 0.0  0.0  1 -1 0 0.00000000  NA grey35 0.5  1 NA 
#7 0.00000000  0 17 16 18 0.00000000 0.0  0.0  1 -1 0 0.00000000  NA grey35 0.5  1 NA 
#8 0.03846154  1 19 18 20 0.03846154 0.2  5.2  1 -1 0 0.03846154  NA grey35 0.5  1 NA 


p + geom_text(data = as.data.frame(ggplot_build(p)$data), 
       aes(x=x, y= density , label = round(density,2)), 
       nudge_y = 0.005) 
+1

[この質問に対するコメント](http://stackoverflow.com/questions/20622332/documentation-on-internal-variables-in -ggplot-esp-panel)Hadleyは、PANELのような内部変数をggplot_build()出力の列の1つとして使用することを強く推奨しています。 ggpplot_build()の他の変数は、密度のように使用するのが安全だと考えられていますか? –

+1

ggplot_build(p)$ data $ PANELは "内部"パネルではなく、安全に使用できますか? [The docs](https://www.rdocumentation.org/packages/ggplot2/versions/2.1。0/topics/print.ggplot?)は、ggplot_build()がprint.ggplotによって返される(目に見えない)ので、何でも信頼できるようにする必要があります。私が上にリンクしているというハドリーの警告は2013年からです... –

4

あなたはgeom="text"stat_binを使用してバーにラベルを付けることができます。 stat_binは、の場合と同様に、数値を計算して密度に変換します。..density..を使用します。しかし、geom="text"に設定すると、これらの濃度値がテキストとして表示されます。また、濃度値が一致するようにgeom_histogramstat_binに同じbreaksを設定する必要があります。私は、ラベルの中に..density..を0.5倍して、バーの中央にテキストラベルを配置しました。しかし、あなたはもちろんこれを調整することができます。

breaks = seq(4,20,by=2) 

ggplot(dat, aes(x=a)) + 
    geom_histogram(aes(y=..density..), breaks = breaks) + 
    stat_bin(geom="text", aes(label=round(..density..,2), y=0.5*..density..), 
      breaks=breaks, colour="white") + 
    xlab("Required Solving Time") 

enter image description here

ちょうどバーの上のラベルを取得するには、使用することができます:あなたはこの層に伝える必要がありますので、

ggplot(dat, aes(x=a)) + 
    geom_histogram(aes(y=..density..), breaks = breaks) + 
    stat_bin(geom="text", aes(label=round(..density..,2), y=..density..), 
      breaks=breaks, vjust = -1) + 
    xlab("Required Solving Time") 

enter image description here

4

..density..は、STATから来ていますビニング統計も使用するには

p + geom_text(aes(label=round(..density.., 2), y=..density..), 
       stat="bin", breaks = seq(4,20,by=2), 
       col="white", vjust=1) 

enter image description here

関連する問題