I'm trying to make an ecdf graph (Empirical cumulative distribution function) with a different colored plot for each subject ('A', 'B' or 'C' in this example). In this example, the X axis describes the RT (response time), and the Y axis describes the cumulative proportion of rt observations. Using ggplot2 and ecdf function, I managed to plot each subject's ecdf plot with a different discrete color for each of them. The problem starts when I want to color the subject's plot continuously based on a totally different variable, here called 'color_factor', which is different for each subject and is continuous.

Here is my simplified example:

set.seed(125)
dat <- data.frame(
  subject = c(rep(c("A"), 10), rep(c("B"), 10), rep(c("C"), 10)),
  color_factor = c(rep(0.3, 10), rep(0.6,10), rep(0.9,10)),
  rt = sample(1:50, 30, replace =T)
)

dat <- arrange(dat,color_factor,rt)
dat.ecdf <- ddply(dat, .(color_factor), transform, ecdf=ecdf(rt)(rt) )
p <- ggplot( dat.ecdf, aes(rt, ecdf, colour = subject)) + geom_line()
p2 <- ggplot( dat.ecdf, aes(rt, ecdf, colour = color_factor)) + geom_line()

the initial data looks like this:

Plot p works great and looks like this:

But when I try to color the plots using the color_factor variable, it draws only one plot for all subjects and colors it not as intended.

What I intend to do is that the graph will look like graph p, except for the plots colors, which will be, for example colored as such: subject A- light blue, subject B- blue, and subject C- dark blue, corresponding to each subject's color_factor variable.

Anyone has any ideas what I can do? Any help would be greatly appreciated!

Thanks very much,

Yuval

2

There are 2 answers

0
Yuval Harris On BEST ANSWER

Here is the answer that does exactly what I wanted, provided by @Lime:

p <- ggplot( dat.ecdf, aes(rt, ecdf, group = subject, colour = color_factor)) + geom_line()

This colors each subject's plot appropriate to his 'color_factor' value:

1
Duck On

Try any of these options:

library(plyr)
library(ggplot2)
#Data
set.seed(125)
dat <- data.frame(
  subject = c(rep(c("A"), 10), rep(c("B"), 10), rep(c("C"), 10)),
  color_factor = c(rep(0.3, 10), rep(0.6,10), rep(0.9,10)),
  rt = sample(1:50, 30, replace =T)
)
#Transform
dat <- arrange(dat,color_factor,rt)
dat.ecdf <- ddply(dat, .(color_factor), transform, ecdf=ecdf(rt)(rt) )
#Plot 1
ggplot( dat.ecdf, aes(rt, ecdf, colour = subject,group=1)) + geom_line()+
  scale_color_manual(values = c('lightblue','blue','darkblue'))

Output:

enter image description here

Or this:

#Plot 2
ggplot( dat.ecdf, aes(rt, ecdf, colour = factor(color_factor),group=subject)) + geom_line()+
  scale_color_manual(values = c('lightblue','blue','darkblue'))+
  labs(color='Factor')

Output:

enter image description here

Or this:

#Plot 3
ggplot( dat.ecdf, aes(rt, ecdf, colour = subject,group=subject)) + geom_line()+
  scale_color_manual(values = c('lightblue','blue','darkblue'))+
  labs(color='Subject')

Output:

enter image description here