geom_density to match geom_histogram binwitdh

前端 未结 1 1780
离开以前
离开以前 2020-12-03 05:51

I would like to add a line to a distribution bar plot in ggplot2 to show the average distribution but am having trouble.

A ggplot call like this:

gg         


        
1条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 06:12

    According to answer of Brian S. Diggs given in this e-mail you should multiply value of ..count.. in geom_density() by the value of binwidth= in geom_histogram().

    set.seed(123)
    df<-data.frame(x=rnorm(1000,100,15))
    
    ggplot(df,aes(x))+
          geom_histogram(binwidth = 2.5)+
          geom_density(aes(y=2.5 * ..count..))
    

    0 讨论(0)
提交回复
热议问题