I'm trying to replicate a conditional density plot as shown in the docs for stat_density, but I need to do it with ggplot and not qplot because I need to facet the plot. However, my data has unequal numbers of rows in each facet, and when I plot it, I get weird results. Here's an MRE:
test.df = data.frame(model=c(rep("A",100),rep("B",50),rep("C",20)),
as.factor(cnt=c(rbinom(100,1,0.5),rbinom(50,1,0.5),rbinom(20,1,0.5))),
xvar=c(seq(1,100),seq(1,50),seq(1,20)))
# This works fine, ignoring the model facets.
ggplot(test.df,aes(x=xvar,y=..count..,fill=cnt)) + geom_density(position="fill")
# Something's wrong with this one.
ggplot(test.df,aes(x=xvar,y=..count..,fill=cnt)) + geom_density(position="fill") +
facet_wrap(~model)
Here's an example of what it looks like ignoring the model (and it looks the same if you subset and plot each group individually):
vs with faceting:
Any thoughts on what I might be doing wrong here?
edit: this is with R 2.15.1 / Rstudio 0.97.314 under OSX 10.8.2, ggplot2 0.9.3.1