geom_density y-axis goes above 1
You have to use ..scaled..
(density estimate, scaled to maximum of 1) within geom_density
, by default it uses ..density..
.
library(ggplot2)
# In aes by default first argument is x and second argument is y
ggplot(my_measurements, aes(value, ..scaled..)) +
geom_density()
All code to reproduce result:
library(ggplot2)
p1 <- ggplot(my_measurements, aes(value, ..density..)) +
geom_density() +
ggtitle("Density")
p2 <- ggplot(my_measurements, aes(value, ..count..)) +
geom_density() +
ggtitle("Count")
p3 <- ggplot(my_measurements, aes(value, ..scaled..)) +
geom_density() +
ggtitle("Scaled")
egg::ggarrange(p1, p2, p3, ncol = 3)
I think the confusion is about discrete and continuous variables. For discrete variables all probability mass functions are in [0, 1]. For continuous variables with density, the area under the curve in 1. If a certain point has density value larger than 1, that does not imply that the particular point has probability greater than 1. Probability for that point is still zero. Value of density is combined with the range on the x-axis to calculate area under the curve. Hence, area and density value are different. Your plot is all good.