Search code examples
rhistogramnormal-distribution

Histogram overlay not visible


I need to overlay a normal distribution curve based on a dataset on a histogram of the same dataset.

I get the histogram and the normal curve right individually. But the curve just stays a flat line when combined to the histogram using the add = TRUE attribute in the curve function.

I did try adjusting the xlim and ylim to check if it works but am not getting the intended results, I am confused about how to set the (x and y) limits to suit both the histogram and the curve.

Any suggestions? My dataset is a set of values for 100 individuals daily walk distances ranging from min = 0.4km to max = 10km

bd.m <- read_excel('walking.xlsx')
hist(bd.m, ylim = c(0,10))
curve(dnorm(x, mean = mean(bd.m), sd = sd(bd.m)), add = TRUE, col = 'red')

Solution

  • You need to set freq = FALSEin the call to hist. For example:

    dt <- rnorm(1000, 2)
    hist(dt, freq = F)
    curve(dnorm(x, mean = mean(dt), sd = sd(dt)), add = TRUE, col = 'red')
    

    enter image description here