0

I have a data.frame which I'd like to draw edcf lines. There are approximately 96 pos ecdf lines and 96 neg ecdf lines. I'd like to color the pos lines black and the neg lines red. I'd also like to add a level of transparency, or an average line, so that it doesn't look cluttered. And perhaps only include pos and neg in the legend.

Code:

simplify <- function(x){
  temp = x[complete.cases(x),]
  df.m = reshape2::melt(temp, id.vars = NULL) 
  df.m$XIST = sapply(strsplit(as.character(df.m$variable), "_", fixed=TRUE), function(x) (x[1]))
  return(df.m)
}
temp = simplify(X_chr)
ggplot(temp, aes(value, colour=variable)) + stat_ecdf() + xlim(1,1000) + theme_bw()

Temp looks something like this:

> head(temp, 10)
   variable    value XIST
1    pos_A1  0.00000  pos
2    pos_A1  0.00000  pos
3    pos_A1  0.00000  pos
4    pos_A1  0.00000  pos
5    pos_A1  0.00000  pos
6    pos_A1 15.66911  pos
7    pos_A1  0.00000  pos
8    pos_A1  0.00000  pos
9    pos_A1  0.00000  pos
10   pos_A1  0.00000  pos

> tail(temp, 10)
       variable     value XIST
210999   neg_H9  0.000000  neg
211000   neg_H9  0.000000  neg
211001   neg_H9  0.000000  neg
211002   neg_H9  0.000000  neg
211003   neg_H9  0.000000  neg
211004   neg_H9  4.466276  neg
211005   neg_H9  0.000000  neg
211006   neg_H9  0.000000  neg
211007   neg_H9  0.000000  neg
211008   neg_H9 30.033764  neg

Produces:

enter image description here

user2117258
  • 515
  • 4
  • 18

1 Answers1

1

Next time please post a reproducible example.

You just need to specify a custom legend using scale_color_manual.

df <- reshape2::melt(replicate(10,rnorm(100)^2))
df$Var2 <- paste0(c(rep("pos", 500), 
                    rep("neg", 500)),
                  df$Var2)
ggplot(df, aes(x = value, colour=Var2)) + stat_ecdf() + 
  xlim(0,3) + theme_bw() + 
  scale_color_manual(label = stringr::str_sub(unique(df$Var2),1,3),
                     values = c(rep('red',5), rep("blue",5)))

enter image description here

If you want the full variable names just replace the relevant code with

  scale_color_manual(label = unique(df$Var2),
                     values = c(rep('red',5), rep("blue",5)))

As for your last question, you can specify manual legends as follows. I increased the size of the df because you will run into the problem you specified with many names in the title.

df <- reshape2::melt(replicate(100,rnorm(100)^2))
df$Var2 <- paste0(c(rep("pos", 500), 
                    rep("neg", 500)),
                  df$Var2)
ggplot(df, aes(x = value, group=Var2, 
               color = c(rep('red',5e3), rep("blue",5e3)))) +
         stat_ecdf() + 
  xlim(0,3) + theme_bw() + 
  scale_colour_manual("+ or -",
                      values = c("red", "blue"), 
                      labels = c("pos", "neg")) 

enter image description here

Community
  • 1
  • 1
shayaa
  • 2,787
  • 13
  • 19