It is useful to overlay the expected density function over a histogram. This can give you a visual cue to if the data actually fits the expected distribution, however this should not be a substitute for a goodness-of-fit test.

In R base graphics there are two ways to do this.

##### Find the min and max of the data and create a sequence to feed into the density function.

1 2 3 4 5 |
y <- rnorm(100) hist(y, prob=TRUE) x <- seq(min(y),max(y),by=0.01) lines(x, dnorm(x), col="purple") |

There is a quicker way by using the `curve`

function. It requires that a variable x be in the function call and will evaluate the function along the same length as the original plot.

##### Use a built in function that generates x sequence value based on the plot.

1 2 3 4 |
y <- rnorm(100) hist(y, prob=TRUE) curve(dnorm(x), col="purple", add=TRUE) |

You must log in to post a comment.