简体   繁体   English

直方图上的密度曲线是平坦的

[英]Density curve on histogram is flat

I am trying to plot a curve that follows the trend of the histogram of my data, I have looked around and have tried out other peoples code but I still get a flat line.我正在尝试 plot 一条遵循我的数据直方图趋势的曲线,我环顾四周并尝试了其他人的代码,但我仍然得到一条平线。 Here is my code这是我的代码

hist(Ferr,xlab = "Ferritin Plasma Concentration", ylab = "Frequency", main = "Histogram of Ferritin 
Plasma Concentration", xlim = c(0,250), ylim = c(0,50), cex.axis=0.8, cex.lab=0.8,cex.main = 1)
curve(dnorm(x, mean = mean(Ferr), sd = sd(Ferr)), col="blue", add=TRUE)
lines(density(Ferr), col="red")

If anyone can help me to see where I have gone wrong, that would be great thank you.如果有人可以帮助我看看我哪里出错了,那将非常感谢。

Unlike an histogram, the integral of a density function over the whole space is equal to 1:与直方图不同,密度 function 在整个空间上的积分等于 1:

sum(density(x)*dx) = 1

To scale the density function to the histogram, you can multiply it by the maximum value of the histogram bins and divide it by the distance between points.要将密度 function 缩放到直方图,可以将其乘以直方图 bin 的最大值,然后除以点之间的距离。

Let's take mtcars$mpg as example:我们以mtcars$mpg为例:

Ferr <- mtcars$mpg
d <- density(Ferr)
dx <- diff(d$x)[1] 

sum(d$y)*dx
[1] 1.000851

h <- hist(Ferr)
lines(x=d$x,y=max(h$counts)*d$y/dx)

在此处输入图像描述

You need to set freq = FALSE (and remove the constraints on ylim and xlim and change "Frequency" to "Density" ):您需要设置freq = FALSE (并删除对ylimxlim的约束并将"Frequency"更改为"Density" ):

hist(Ferr,
     freq= FALSE, 
     xlab = "Ferritin Plasma Concentration", ylab = "Density", 
     main = "Histogram of Ferritin Plasma Concentration", 
     cex.axis=0.8, cex.lab=0.8,cex.main = 1)
curve(dnorm(x, mean = mean(Ferr), sd = sd(Ferr)), col="blue", add=TRUE)
lines(density(Ferr), col="red")

Toy data:玩具数据:

Ferr <-  rnorm(1000)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM