overlaying exponential distribution onto histogram

user3471179 picture user3471179 · Mar 28, 2014 · Viewed 7.4k times · Source

How can i overlay an exponential distribution on a histogram of time intervals? The histogram looks like an exponential distribution. When I try to create the histogram in a similar way to superimposing a normal curve I get the following:

Error in xy.coords(x, y) : 'x' and 'y' lengths differ

I can create the histogram on its own which has an x axis from 0 to 70. And I can create an exponential distribution curve on its own but its x axis goes from 0 to 1.

I am using hist(t) where t is a list of times in seconds for the histogram and curve(dexp(x,rate=0.09)) for the exponential distribution.

Answer

Rich Scriven picture Rich Scriven · Mar 28, 2014

Make sure to use prob = TRUE in hist, and add = TRUE in curve

z <- rexp(300,rate = 0.09)
hist(z, prob = TRUE)
curve(dexp(x, rate = 0.09), col = 2, lty = 2, lwd = 2, add = TRUE)

enter image description here