Search code examples
rhistogramcurve

exponential curve fit on histogram in R


I made a histogram in R and I have to fit an exponential curve on it.

enter image description here

But the curve doesn't appear on the histogram.

This is the code:

hist(Adat$price, main="histogram",xlab="data")
curve(dexp(x, rate=1,log=FALSE), add = TRUE)

Could someone help me please?


Solution

  • You need to add set the argument freq=FALSE if you want the histogram to be normalized:

    set.seed(32418)
    sim <- rexp(100) + rnorm(100,0,.01)
    hist(sim, freq=FALSE)
    curve(dexp(x, rate=1, log=FALSE), add = TRUE)
    

    hist output

    Otherwise, the height of the bins will be a function of the number of samples. In fact, the curve technically did appear on your graph, it's just so small that you can't distinguish it from a flat line at y = 0.