【发布时间】:2013-08-22 12:05:14
【问题描述】:
有一些关于在 ggplot 中绘制累积密度的帖子。我目前正在使用来自Easier way to plot the cumulative frequency distribution in ggplot? 的公认答案来绘制我的累积计数。但是这个解决方案需要预先计算值。
我在这里寻找一个纯粹的 ggplot 解决方案。让我们展示一下我到目前为止所拥有的:
x <- data.frame(A=replicate(200,sample(c("a","b","c"),1)),X=rnorm(200))
ggplot 的stat_ecdf
我可以使用 ggplot 的 stat_ecdf,但它只绘制累积密度:
ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y..),stat="ecdf")
我想做以下类似的事情,但它不起作用:
ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y.. * ..count..),stat="ecdf")
cumsum 和 stat_bin
我发现了一个关于使用cumsum 和stat_bin 的想法:
ggplot(x,aes(x=X,color=A)) + stat_bin(aes(y=cumsum(..count..)),geom="step")
但如您所见,下一个颜色不是从y=0 开始,而是上一个颜色结束的位置。
我的要求
从最好到最坏我想要的:
-
理想情况下是对不工作的简单修复
ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y.. * ..count..),stat="ecdf") 将
stat_ecdf与计数一起使用的更复杂的方法。- 最后的手段是使用
cumsum方法,因为它会产生更差的(分箱)结果。
【问题讨论】: