R: Plotting one ECDF on top of another in differen

2020-07-06 07:42发布

I have a couple of cumulative empirical density functions which I would like to plot on top of each other in order to illustrate differences in the two curves. As was pointed out in a previous question, the function to draw the ECDF is simply plot(Ecdf()) And as I read the fine manual page, I determined that I can plot multiple ECDFs on top of each other using something like the following:

require( Hmisc )
set.seed(3)
g <- c(rep(1, 20), rep(2, 20))  
Ecdf(c( rnorm(20), rnorm(20)), group=g)

However my curves sometimes overlap a bit and can be hard to tell which is which, just like the example above which produces this graph:

enter image description here

I would really like to make the color of these two CDFs different. I can't figure out how to do that, however. Any tips?

标签: r plot ecdf
3条回答
劳资没心,怎么记你
2楼-- · 2020-07-06 07:54

If memory serves, I have done this in the past. As I recall, you needed to trick it as Ecdf() is so darn paramterised. I think in help(ecdf) it hints that it is just a plot of stepfunctions, so you could estimate two or more ecdfs, plot one and then annotate via lines().

Edit Turns out it is as easy as

  R> Ecdf(c(rnorm(20), rnorm(20)), group=g, col=c('blue', 'orange'))

as the help page clearly states the col= argument. But I have also found some scriptlets where I used plot.stepfun() explicitly.

查看更多
beautiful°
3楼-- · 2020-07-06 07:59

Without using Ecdf (doesn't look like Hmisc is available):

set.seed(3)
mat <- cbind(rnorm(20), rnorm(20))
matplot(apply(mat, 2, sort), seq(20)/20, type='s')
查看更多
叼着烟拽天下
4楼-- · 2020-07-06 08:06

You can add each curve one at a time (each with its own style), e.g.

Ecdf(rnorm(20), lwd = 2)
Ecdf(rnorm(20),add = TRUE, col = 'red', lty = 1)
查看更多
登录 后发表回答