There is no easy way to color in different overlapping areas unless you explicitly calculate the regions themselves. Here's a feature that can help calculate regions where density is swapping
densitysplit <- function(val, grp, N=200, x=seq(min(val), max(val), length.out=N)) { grp <- factor(grp) den <- Map(function(z) { dx<-density(val[grp==z]) approxfun(dx$x, dx$y)(x) }, levels(grp)) maxcat <- apply(do.call("cbind",den), 1, which.max) data.frame(x=x, ymin=do.call("pmin", den), ymax=do.call("pmax", den), top = levels(grp)[maxcat], group = cumsum(c(1,diff(maxcat)!=0)) ) }
For your data, you would do something like this
head(densitysplit(df$value, df$gender))
This gives you the data that geom_ribbon needs to use to build the data. You can do
ggplot(df, aes(value)) + geom_ribbon(data=densitysplit(df$value, df$gender), aes(x, ymin=ymin, ymax=ymax, fill=top, group=group)) + geom_density() + geom_density(data=subset(df, gender=='male'), aes(value), colour="blue") + geom_density(data=subset(df, gender=='female'), aes(value), colour="red")

Mrflick
source share