2013-12-16 175 views

回答

5

封装latticeExtra提供功能ecdfplot

library(lattice) 
library(latticeExtra) 

set.seed(42) 
vals <- data.frame(r1=rnorm(100)*0.5, 
        r2=rnorm(100), 
        r3=rnorm(100)*2) 

ecdfplot(~ r1 + r2 + r3, data=vals, auto.key=list(space='right') 

ecdfplot with legend

5

这里有一种方法(对于他们三个人,工作4以同样的方式):

set.seed(42) 
ecdf1 <- ecdf(rnorm(100)*0.5) 
ecdf2 <- ecdf(rnorm(100)*1.0) 
ecdf3 <- ecdf(rnorm(100)*2.0) 
plot(ecdf3, verticals=TRUE, do.points=FALSE) 
plot(ecdf2, verticals=TRUE, do.points=FALSE, add=TRUE, col='brown') 
plot(ecdf1, verticals=TRUE, do.points=FALSE, add=TRUE, col='orange') 

注意,我用的是第三个拥有范围最广的事实,并用它来初始化帆布。否则你需要ylim=c(...)

enter image description here

+4

所以,如果你的内存是正确的2.5年前,那么这就是你的回答同样的问题第三次:http://stackoverflow.com/questions/6344081/r-plotting-one -ecdf-on-top-of-another-in-different-colors?rq = 1 –

+0

== :-)你刚刚度过了我的一天。我可能需要一个关于在这里搜索的教程,因为我寻找一个旧的答案,但没有发现它立马。请注意,2年半前,我只说过我知道我以前做过,没有回答。今天的答案是所有基地R,没有Hmisc,所以那里。 ;-) –

+1

Search on:user:143305 ecdf ....你可以从早些时候找到其他人,尽管我正在考虑使用的是'Hmisc :: Ecdf'。 (有时再简单一点就行了。) –

2

下面是使用ggplot2性的方法(使用ECDF对象从[德克的答案])(https://stackoverflow.com/a/20601807/1385941

library(ggplot2) 
# create a data set containing the range you wish to use 
d <- data.frame(x = c(-6,6)) 
# create a list of calls to `stat_function` with the colours you wish to use 

ll <- Map(f = stat_function, colour = c('red', 'green', 'blue'), 
      fun = list(ecdf1, ecdf2, ecdf3), geom = 'step') 


ggplot(data = d, aes(x = x)) + ll 

enter image description here

相关问题