2016-06-16 18 views
2

カウントの代わりにグループ内の密度を表示することはできますか?ggplot2 :: stat_bin2dがカウントの代わりに密度を表示する

library(ggplot2);data(diamonds) 
ggplot(diamonds, aes(carat, depth)) + 
    stat_bin2d(bins=40)+ facet_wrap(~color) 

これにより、当然のことながら、いくつかのグループが発生する可能性があるため、グループ間のパターンの比較が容易になります。

質問は、回答にも欠けているHow to scale (normalise) values of ggplot2 stat_bin2d within each column (by X axis)と少し類似しています。

答えて

8
ggplot(diamonds, aes(carat, depth)) + 
    stat_bin2d(bins=40, aes(fill = ..density..))+ facet_wrap(~color) 

resulting plot

それとも、カーネル密度推定と幸せでしょうか?

ggplot(diamonds, aes(carat, depth)) + 
    stat_density2d(aes(fill = ..density..), geom = "tile", contour = FALSE, n = 25) + 
    facet_wrap(~color) + 
    scale_fill_gradient(low = "light blue", high = "dark red") 

resulting plot

か、デフォルトのグリッドを持つ:

ggplot(diamonds, aes(carat, depth)) + 
    stat_density2d(aes(fill = ..density..), geom = "tile", contour = FALSE) + 
    facet_wrap(~color) + 
    scale_fill_gradient(low = "light blue", high = "dark red") 

resulting plot

+0

あなたがstat_bin2dの間で、このような異なる密度のスケールを取得し、stat_density2d理由にコメントすることができますか?これは、_bin2dが正確であるように見えますが、すべてが1になると仮定すると、density2dは高すぎます。 – val

関連する問題