First, read Wickham on densities in R, noting the foibles and features of each package/function.
The densities sum to 1, but that doesn't mean the curve line/points will not go above 1.
The following shows both this and the inaccuracy of (at least) the defaults of density
when compared to, say, KernSmooth::bkde
(using base plots for brevity of typing):
library(KernSmooth)
library(flux)
library(sfsmisc)
# uniform dist
set.seed(1)
dat <- runif(100)
d1 <- density(dat)
d1_ks <- bkde(dat)
par(mfrow=c(2,1))
plot(d1)
plot(d1_ks, type="l")
auc(d1$x, d1$y)
## [1] 1.000921
integrate.xy(d1$x, d1$y)
## [1] 1.000921
auc(d1_ks$x, d1_ks$y)
## [1] 1
integrate.xy(d1_ks$x, d1_ks$y)
## [1] 1
Do the same for the beta distribution:
# beta dist
set.seed(1)
dat <- rbeta(100, 0.5, 0.1)
d2 <- density(dat)
d2_ks <- bkde(dat)
par(mfrow=c(2,1))
plot(d2)
plot(d2_ks, typ="l")
auc(d2$x, d2$y)
## [1] 1.000187
integrate.xy(d2$x, d2$y)
## [1] 1.000188
auc(d2_ks$x, d2_ks$y)
## [1] 1
integrate.xy(d2_ks$x, d2_ks$y)
## [1] 1
auc
and integrate.xy
both use the trapezoid rule but I ran them to both show that and to show the results from two different functions.
The point is that the densities do in fact sum to 1, despite the y-axis values leading you to believe that they do not. I'm not sure what you are trying to solve with your manipulations.