在直方图上绘制高斯曲线时遇到问题? (使用以前的例子,没有运气)

Having issues plotting Gaussian curve on histogram? (Used previous examples and no luck)

首先,这是代码:

hist(sumCol)
curve(dnorm(sumCol, mean=Mean, sd=SD), 
      col="darkblue", lwd=2, add=TRUE, yaxt="n")

我使用了我找到的代码 here 但没有成功。任何帮助将非常感激。平均值为 49.9,SD 为 6.66。

> dput(sumCol)
c(47.4105366033036, 58.3324683033861, 58.1094471025281, 49.9950564198662, 
46.136499152286, 57.6314454714302, 55.9323056084104, 42.4964612387121, 
56.1618362078443, 42.376149847405, 56.1894942307845, 50.9596610828303, 
44.9340054308996, 56.2675485799555, 44.5740411255974, 55.4805521473754, 
50.7398278019391, 48.7541372219566, 36.393867429113, 46.3503022803925, 
55.629230362596, 41.7389209344983, 37.9173863746691, 49.6265010556672, 
52.5780587899499, 48.2867740916554, 47.6546685318463, 55.3406274791341, 
42.1973585763481, 44.8090796419419, 45.2378696959931, 49.4975818633102, 
49.5211400222033, 66.1860005331691, 64.2629869871307, 52.9526992985047, 
43.8075632608961, 52.2976646479219, 49.4498609972652, 43.0183454982471
)

容易得到的是高度为概率密度时的直方图。

hist(sumCol, prob=TRUE)
sumCol_norm = function(x) { dnorm(x, mean=mean(sumCol), sd=sd(sumCol)) }
curve(sumCol_norm,  col="darkblue", lwd=2, add=TRUE, yaxt="n")

这给出了您想要的形状,但 y-axis 标签是密度,而不是计数。为了使用原始直方图中的计数来执行此操作,我们需要通过所有 bin 的总计数来缩放密度。我们可以通过保存直方图的输出并进行少量计算来得到它。

H = hist(sumCol)
area = sum(diff(H$breaks)*H$counts)
sumCol_norm = function(x) { area*dnorm(x, mean=mean(sumCol), sd=sd(sumCol)) }
curve(sumCol_norm,  col="darkblue", lwd=2, add=TRUE, yaxt="n")