R: ggplot2: Adding count labels to histogram with density overlay

独自空忆成欢 提交于 2019-12-03 16:50:20
crystal_ym

if you want the y-axis to show the bin_count number, at the same time, adding a density curve on this histogram,

you might use geom_histogram() first and record the binwidth value! (this is very important!), next add a layer of geom_density() to show the fitting curve.

if you don't know how to choose the binwidth value, you can just calculate:

my_binwidth = (max(Tix_Cnt)-min(Tix_Cnt))/30;

(this is exactly what geom_histogram does in default.)

The code is given below:

(suppose the binwith value you just calculated is 0.001)

tix_hist <- ggplot(tix, aes(x=Tix_Cnt)) ;

tix_hist<- tix_hist + geom_histogram(aes(y=..count..),colour="blue",fill="white",binwidth=0.001);

tix_hist<- tix_hist + geom_density(aes(y=0.001*..count..),alpha=0.2,fill="#FF6666",adjust=4);

print(tix_hist);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!