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

前端 未结 1 625
南旧
南旧 2021-01-03 02:27

I have a time-series that I\'m examining for data heterogeneity, and wish to explain some important facets of this to some data analysts. I have a density histogram overlaye

相关标签:
1条回答
  • 2021-01-03 03:14

    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);
    
    0 讨论(0)
提交回复
热议问题