0

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?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
rintrah
  • 3
  • 1

1 Answers1

0

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()

enter image description here

Graphically, this might be clearer/more noticeable if you shaded the area under the Normal distribution curve

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Ah, I made some mistake with the freq=FALSE Thank you a lot! It was really beginning to drive me mad. – rintrah Jan 23 '22 at 20:29