Why does cumsum not work inside groups or faces in ggplot?

Borrowing example from Accrual of total account in ggplot2

x <- data.frame(A=replicate(200,sample(c("a","b","c"),1)),X=rnorm(200))
ggplot(x,aes(x=X,color=A)) + stat_bin(aes(y=cumsum(..count..)),geom="step")

enter image description here

As you can see, it cumsumworks across groups and faces. I wonder why this is so? Clearly what ..count..is done inside groups, why cumsumnot apply to ..count..? Ggplot internally cat all ..count..in vector, then apply cumsumto it?

How to properly resolve it without preprocessing, for example. using plyr?

And I do not mind geomnot step, it can be lineor even baras long as the schedule is a cumulative plot.

+4
source share
1 answer

(ddply mutate):

df <- data.frame(x=rnorm(1000),kind=sample(c("a","b","c"),1000,replace=T),
         label=sample(1:5,1000,replace=T),attribute=sample(1:2,1000,replace=T))

dfx <- ddply(df,.(kind,label,attribute),mutate,cum=rank(x)/length(x))

ggplot(dfx,aes(x=x))+geom_line(aes(y=cum,color=kind))+facet_grid(label~attribute)
+1

All Articles