Product: TIBCO Spotfire®
How can I overlay a density plot (normal curve) over a histogram?
User looking for S+ code to overlay a density plot (normal curve) over a histogram.
Here is one approach to combining the density (normal curve) and the histogram. The key here is the probability=TRUE argument to hist(), which puts it on a 0-1 vertical scale rather than a scale of bin counts. Then, computing the interquartile range is a fairly reasonable way to set the horizontal scale of the density function.
x <- rnorm(500)
hist(x, prob=T)
iqd <- summary(x)[5] - summary(x)[2]
lines(density(x, width=2*iqd))
References:
You can view the help files on any of the functions used in the code above by typing a question mark followed by the function name at your S+ command prompt. For example:
> ?hist
> ?density
Comments
0 comments
Article is closed for comments.