How to plot multiple ECDF's on one plot in different colors in R

Jason picture Jason · Dec 16, 2013 · Viewed 16.8k times · Source

I am trying to plot 4 ecdf functions on one plot but can't seem to figure out the proper syntax.

If I have 4 functions "A, B, C, D" what would be the proper syntax in R to get them to be plotted on the same chart with different colors. Thanks!

Answer

Dirk Eddelbuettel picture Dirk Eddelbuettel · Dec 16, 2013

Here is one way (for three of them, works for four the same way):

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')

Note that I am using the fact that the third has the widest range, and use that to initialize the canvas. Else you need ylim=c(...).

enter image description here