Density values sum up 2 in histograms, can any one explain ...?

Hi!
I'm new to R. I have experience in other languages or statistical apps.
I'm doing a process analysis and finally I learn to do histograms, but when I selected the option for plotting densities instead of frequencies I noticed that individual values of the distribution sum up 2 instead of 1. I verified and the height of each bar is ok. Can anybody explain that behavior ...

Thanks ...

Please provide the code you ran so that we can more easily help you.

For now, maybe the code below will help clarify what's happening.

# Generate two density distributions with densities on the same x-scale
set.seed(5)
x1 = rnorm(1000, 0, 1)
x2 = rnorm(1000, 3, 1)

x1d = density(x1, from=-5, to=8)
x2d = density(x2, from=-5, to=8)

# Area under each curve is 1
sum(x1d$y * median(diff(x1d$x)))
#> [1] 1.000979
sum(x2d$y * median(diff(x2d$x)))
#> [1] 1.000978

plot(x1d, ylim=c(0, 0.45))
lines(x2d)

# Area under curve has doubled to 2 when we add the densities
lines(x1d$x, x1d$y + x2d$y, col="red")

# Renormalize so area under curve is 1
lines(x1d$x, (x1d$y + x2d$y)/2, col="blue", lwd=3)

Created on 2020-11-14 by the reprex package (v0.3.0)

This is no my real code. I'm using your sample data and the behavior is the same.
The chole example data was taken from another forum. It got worst. The sum of densities totals 0.02.
Sorry for not understanding the format for posting code

chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
x<-hist(chol$AGE,prob=T)
x
sum(x$counts)
sum(x$density)

x1 = rnorm(1000, 0, 1)
x2 = rnorm(1000, 3, 1)

Y1<-hist(x1,prob=T)
sum(Y1$density)

Y2<-hist(x2,prob=T)
sum(Y2$density)

Hi!
Here is a guide

The density values will not sum to one. The density is represented with point values, usually evenly spaced. If you increase the number of points and reduce their spacing, the sum of the density points will increase. What may sum to one, for the right kind of function, is the area under the curve. That is what @joels calculated:

1 Like

Thank you for your response.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.