2016-07-26 1 views
0

У меня есть data.frame, который я хотел бы нарисовать линии edcf. Существует приблизительно 96 строк pos ecdf и 96 отрицательных строк ecdf. Я хотел бы покрасить строки pos черными, а отрицательные - красными. Я также хотел бы добавить уровень прозрачности или среднюю строку, чтобы она не выглядела загроможденной. И, возможно, в легенду можно включить только pos и neg.раскраска ggplot2's stat_ecdf строк на основе разделителя образцов

Код:

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 выглядит примерно так:

> 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 

Производит:

enter image description here

+0

Вы можете закрыть этот вопрос, нажав на галочку рядом с моим ответом. – shayaa

ответ

1

В следующий раз пожалуйста, напишите reproducible example.

Вам просто нужно указать пользовательскую легенду, используя 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

Если вы хотите, полные имена переменных просто заменить соответствующий код с

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

Что касается последнего вопроса, то можно указать вручную легенды следующим образом. Я увеличил размер df, потому что вы столкнетесь с проблемой, которую вы указали со многими именами в названии.

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

Смежные вопросы