Reputation: 3
I need to overlay a normal distribution on a histogram using curve()
and dnorm()
. dnorm
should not have the parameters of my data, but be a generalized normal distribution with mean = 0 and sd = 5.
Using this site and a lot of tutorials, I would expect the code to be
hist(mtcars$mpg)
curve(dnorm(x, 0, 5), add= TRUE, col="red")
But all I get is a flat line. If I use a stand alone example, leaving out add=TRUE
I get the desired density function:
curve(dnorm(x, 0, 5), col="red")
Does anyone have any idea what mistake I am making?
Upvotes: 0
Views: 950
Reputation: 226182
Over the range of the data (10-35), there is very little probability in the Normal distribution with a mean of 0 and a SD of 5 (i.e. the curve starts about at the upper end of the 95% confidence interval of the distribution).
If we add freq= FALSE
to the hist()
call (as is appropriate if you want to compare a probability distribution to the histogram), we can see a little bit of the red curve at the beginning (you could also multiply by a constant if you want the tail to be more visible). (Distribution shown for a more plausible value of the mean as well [blue line].)
## png("tmp.png"); par(las=1, bty="l")
hist(mtcars$mpg, freq=FALSE)
curve(dnorm(x, 0, 5), add = TRUE, col = "red")
curve(dnorm(x, 20, 5), add = TRUE, col = "blue")
## dev.off()
Graphically, this might be clearer/more noticeable if you shaded the area under the Normal distribution curve
Upvotes: 0