着色ggplot2的基于stat_ecdf行的样本分隔符

时间:2016-07-26 22:10:05

标签: r ggplot2 ecdf

我有一个data.frame,我想绘制edcf行。有大约96个pos ecdf线和96个neg ecdf线。我想把pos线黑色和neg线红色。我还希望添加一定程度的透明度或平均线,以使其看起来不会混乱。也许只包括传说中的pos和neg。

代码:

Get-ADComputer -Filter {OperatingSystem -Like "*windows*"} -Property * | Format-Table Name |  foreach {Echo "psexec \\$ cleanmgr /sagerun:1"}

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

制作:

enter image description here

1 个答案:

答案 0 :(得分: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