Normalizing y-axis in histograms in R ggplot to proportion by group
Solution 1:
Like this? [edited based on OP's comment]
ggplot(all,aes(x=value,fill=dataset))+
geom_histogram(aes(y=0.5*..density..),
alpha=0.5,position='identity',binwidth=0.5)
Using y=..density..
scales the histograms so the area under each is 1, or sum(binwidth*y)=1.
As a result, you would use y = binwidth*..density..
to have y represent the fraction of the total in each bin. In your case, binwidth=0.5
.
IMO this is a little easier to interpret:
ggplot(all,aes(x=value,fill=dataset))+
geom_histogram(aes(y=0.5*..density..),binwidth=0.5)+
facet_wrap(~dataset,nrow=2)