为什么`cumsum`在ggplot中的组或构面中不起作用?

col*_*ang 5 r ggplot2

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")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如您所见,cumsum跨小组和多方面工作。我想知道为什么会这样吗?显然..count..是在组内完成的,为什么cumsum不应用于..count..?ggplot是否在内部将所有内容..count..整合到一个向量中然后应用于cumsum它?

如何在不进行预处理的情况下(例如使用)正确解决它plyr

而且我不介意geom不是step,只要图形是累积图就可以line,甚至可以bar

Pet*_*erK 1

以下是我如何用一行代码(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)
Run Code Online (Sandbox Code Playgroud)