There are some posts about plotting cumulative densities in ggplot. I'm currently using the accepted answer from Easier way to plot the cumulative frequency distribution in ggplot? for plotting my cumulative counts. But this solution involves pre-calculating the values beforehand.
Here I'm looking for a pure ggplot solution. Let's show what I have so far:
x <- data.frame(A=replicate(200,sample(c("a","b","c"),1)),X=rnorm(200))
ggplot's stat_ecdf
I can use ggplot's stat_ecdf
, but it only plots cumulative densities:
ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y..),stat="ecdf")
I'd like to do something like the following, but it doesn't work:
ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y.. * ..count..),stat="ecdf")
cumsum
and stat_bin
I found an idea about using cumsum
and stat_bin
:
ggplot(x,aes(x=X,color=A)) + stat_bin(aes(y=cumsum(..count..)),geom="step")
But as you can see, the next color doesn't start at y=0
, but where the last color ended.
What I ask for
What I'd like to have from best to worst:
Ideally a simple fix to the not working
ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y.. * ..count..),stat="ecdf")
A more complicated way to use
stat_ecdf
with counts.- Last resort would be to use the
cumsum
approach, since it gives worse (binned) results.