overlaying exponential distribution onto histogram

2020-04-14 07:05发布

问题:

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.

回答1:

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)