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", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5)))
)
(taken from: http://www.sthda.com/english/wiki/ggplot2-density-plot-quick-start-guide-r-software-and-data-visualization )
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density..), alpha=0.5,
position="identity")+
geom_density(alpha=.2)
but when I change aes(y=..density..) to aes(y=..scaled..) I get an error.
If I use this example with my (big) data the density goes up to 120. So basically this: ![bar]http://www.sthda.com/sthda/RDoc/figure/easy-ggplot2/ggplot2-histogram-multiple-groups3.png with a different y-axis. (so that all bars of one type add up to 1)
I am not sure if it is statistically legit to use geom_smooth?