How can I make a histogram in which all bars add up to 1 and add a density layer above that fits?
set.seed(1234)
df <- data.frame(
sex=factor(rep(c(\"F\", \
Try using y = ..density../sum(..density..)
:
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5)))
)
library(ggplot2)
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density../sum(..density..)), alpha=0.5,
position="identity")+
geom_density(alpha=.2)