0

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

Yuval Harris
  • 53
  • 1
  • 5
  • 1
    something like this? `p2 <- ggplot( dat.ecdf, aes(rt, ecdf, group = subject, colour = color_factor)) + geom_line()` – Lime Oct 10 '20 at 16:44
  • Thanks very much @Lime that's perfect! Exactly what I wanted and surprisingly simple as well :) – Yuval Harris Oct 11 '20 at 18:36

2 Answers2

0

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

Duck
  • 39,058
  • 13
  • 42
  • 84
  • Thanks so much for you time and effort @Duck! The 2nd option is the closest to what I meant, but I actually didn't mention that it's important for me that the colors should be of a continuous scale type that can work also for more subjects with other color_factor values. That's why @Lime 's answer is more what I intended. – Yuval Harris Oct 11 '20 at 18:40
  • @YuvalHarris So you are right. You could check this also to know how to proceed https://stackoverflow.com/help/someone-answers – Duck Oct 11 '20 at 18:45
0

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:

Yuval Harris
  • 53
  • 1
  • 5