2016-07-26 32 views
0

我有一个data.frame,我想绘制edcf行。大约有96个pos ecf线和96个ec ecf线。我想将pos线黑色和neg线红色。我还想增加透明度或平均线,以免看起来杂乱无章。也许只包括传说中的pos和neg。着色ggplot2的基于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