将正态曲线叠加到 R 中的直方图 [英] Overlay normal curve to histogram in R

查看:48
本文介绍了将正态曲线叠加到 R 中的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法在网上找到如何将正态曲线叠加到 R 中的直方图,但我想保留直方图的正常频率"y 轴.请参阅下面的两个代码段,并注意在第二个代码段中如何将 y 轴替换为密度".我如何将 y 轴保持为频率",就像在第一个图中一样.

I have managed to find online how to overlay a normal curve to a histogram in R, but I would like to retain the normal "frequency" y-axis of a histogram. See two code segments below, and notice how in the second, the y-axis is replaced with "density". How can I keep that y-axis as "frequency", as it is in the first plot.

额外奖励:我还想在密度曲线上标记 SD 区域(最多 3 个 SD).我怎样才能做到这一点?我试过 abline,但这条线延伸到图表的顶部,看起来很丑.

AS A BONUS: I'd like to mark the SD regions (up to 3 SD) on the density curve as well. How can I do this? I tried abline, but the line extends to the top of the graph and looks ugly.

g = d$mydata
hist(g)

g = d$mydata
m<-mean(g)
std<-sqrt(var(g))
hist(g, density=20, breaks=20, prob=TRUE, 
     xlab="x-variable", ylim=c(0, 2), 
     main="normal curve over histogram")
curve(dnorm(x, mean=m, sd=std), 
      col="darkblue", lwd=2, add=TRUE, yaxt="n")

在上图中,y 轴是密度".我想让它成为频率".

See how in the image above, the y-axis is "density". I'd like to get that to be "frequency".

推荐答案

这是我发现的一个很好的简单方法:

Here's a nice easy way I found:

h <- hist(g, breaks = 10, density = 10,
          col = "lightgray", xlab = "Accuracy", main = "Overall") 
xfit <- seq(min(g), max(g), length = 40) 
yfit <- dnorm(xfit, mean = mean(g), sd = sd(g)) 
yfit <- yfit * diff(h$mids[1:2]) * length(g) 

lines(xfit, yfit, col = "black", lwd = 2)

这篇关于将正态曲线叠加到 R 中的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆