42

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")

enter image description here

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")

enter image description here

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:

  1. Ideally a simple fix to the not working

    ggplot(x,aes(x=X,color=A)) + geom_step(aes(y=..y.. * ..count..),stat="ecdf")
    
  2. A more complicated way to use stat_ecdf with counts.

  3. Last resort would be to use the cumsum approach, since it gives worse (binned) results.
0

3 Answers 3

29

This will not solve directly problem with grouping of lines but it will be workaround.

You can add three calls to stat_bin() where you subset your data according to A levels.

ggplot(x,aes(x=X,color=A)) +
  stat_bin(data=subset(x,A=="a"),aes(y=cumsum(..count..)),geom="step")+
  stat_bin(data=subset(x,A=="b"),aes(y=cumsum(..count..)),geom="step")+
  stat_bin(data=subset(x,A=="c"),aes(y=cumsum(..count..)),geom="step")

enter image description here

UPDATE - solution using geom_step()

Another possibility is to multiply values of ..y.. with number of observations in each level. To get this number of observations at this moment only way I found is to precalculate them before plotting and add them to original data frame. I named this column len. Then in geom_step() inside aes() you should define that you will use variable len=len and then define y values as y=..y.. * len.

set.seed(123)
x <- data.frame(A=replicate(200,sample(c("a","b","c"),1)),X=rnorm(200))
library(plyr)
df <- ddply(x,.(A),transform,len=length(X))
ggplot(df,aes(x=X,color=A)) + geom_step(aes(len=len,y=..y.. * len),stat="ecdf") 

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

While this works, it doesn't scale. The motivation of this question is to get more maintainable/robust code.
12

You can apply row_number over the groups, and utilize that as the Y aesthetic in a geom_step or other geometry. You'll just have to sort by X, or the values will appear as they do in the data frame, unordered.

ggplot(x %>% 
         group_by(A) %>% 
         arrange(X) %>% 
         mutate(rn = row_number())) + 
  geom_step(aes(x=X, y=rn, color=A))

Cumulative count by group

2 Comments

Alternatively, can use mutate(rn = rank(X, ties = "max")) if you want to avoid sorting the data (although I prefer row_number()).
The advantage to @jdoubleyou answer is that one need not hard-code values of A!
2

How about using ave() to get cumulative sum by group?

ggplot(x[order(x$X),], aes(x = X, y = ave(A == A, A, FUN = cumsum), col = A)) + geom_step()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.