使用ggplot2中的计数数据向直方图添加密度线

时间:2014-12-26 20:51:19

标签: r ggplot2 histogram density-plot

我想在直方图中添加密度线(实际上是正常密度)。

假设我有以下数据。我可以通过ggplot2绘制直方图:

set.seed(123)    
df <- data.frame(x = rbeta(10000, shape1 = 2, shape2 = 4))

ggplot(df, aes(x = x)) + geom_histogram(colour = "black", fill = "white", 
                                        binwidth = 0.01) 

enter image description here

我可以使用以下方法添加密度线:

ggplot(df, aes(x = x)) + 
  geom_histogram(aes(y = ..density..),colour = "black", fill = "white", 
                 binwidth = 0.01) + 
  stat_function(fun = dnorm, args = list(mean = mean(df$x), sd = sd(df$x)))

enter image description here

但这不是我真正想要的,我希望这个密度线适合计数数据。

我发现了一个类似的帖子(HERE)提供了解决此问题的方法。但它在我的情况下不起作用。我需要一个任意的扩展因子来得到我想要的东西。这根本不是一般性的:

ef <- 100 # Expansion factor

ggplot(df, aes(x = x)) + 
  geom_histogram(colour = "black", fill = "white", binwidth = 0.01) + 
  stat_function(fun = function(x, mean, sd, n){ 
    n * dnorm(x = x, mean = mean, sd = sd)}, 
    args = list(mean = mean(df$x), sd = sd(df$x), n = ef))

enter image description here

我可以用来概括这个

的任何线索
  • 首先进行正态分发,
  • 然后到任何其他bin尺寸,
  • 最后对任何其他发行版都非常有帮助。

1 个答案:

答案 0 :(得分:11)

魔术不会发生分配函数。你必须明确地做。一种方法是在fitdistr(...)包中使用MASS

library(MASS)    # for fitsidtr(...)
# excellent fit (of course...)
ggplot(df, aes(x = x)) + 
  geom_histogram(aes(y=..density..),colour = "black", fill = "white", binwidth = 0.01)+
  stat_function(fun=dbeta,args=fitdistr(df$x,"beta",start=list(shape1=1,shape2=1))$estimate)

# horrible fit - no surprise here
ggplot(df, aes(x = x)) + 
  geom_histogram(aes(y=..density..),colour = "black", fill = "white", binwidth = 0.01)+
  stat_function(fun=dnorm,args=fitdistr(df$x,"normal")$estimate)

# mediocre fit - also not surprising...
ggplot(df, aes(x = x)) + 
  geom_histogram(aes(y=..density..),colour = "black", fill = "white", binwidth = 0.01)+
  stat_function(fun=dgamma,args=fitdistr(df$x,"gamma")$estimate)

编辑:回应OP的评论。

比例因子是binwidth✕样本大小。

ggplot(df, aes(x = x)) + 
  geom_histogram(colour = "black", fill = "white", binwidth = 0.01)+
  stat_function(fun=function(x,shape1,shape2)0.01*nrow(df)*dbeta(x,shape1,shape2),
                args=fitdistr(df$x,"beta",start=list(shape1=1,shape2=1))$estimate)