简体   繁体   中英

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. 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:

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.

Let's take mtcars$mpg as example:

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" ):

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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