8

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!

Jason
  • 123
  • 1
  • 1
  • 7

4 Answers4

15

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

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 4
    So if your memory was correct 2.5 years ago, then this is the third time you answered the same question: http://stackoverflow.com/questions/6344081/r-plotting-one-ecdf-on-top-of-another-in-different-colors?rq=1 – IRTFM Dec 16 '13 at 01:05
  • ==:-) You just made my day. I may need a tutorial about searching here because I looked for an old answer but didn't find it rightaway. Mind you, 2 1/2 years ago I only said I knew I had _done_ it before, not answered. And today's answer is all base R, no Hmisc, so there. ;-) – Dirk Eddelbuettel Dec 16 '13 at 01:08
  • 1
    Search on: user:143305 ecdf .... and you will find others from earlier as well although the one I'm thinking of was with `Hmisc::Ecdf`. (Sometimes it's just easier doing it again.) – IRTFM Dec 16 '13 at 01:11
  • Something odd there Dirk, looks like clipping got turned off somewhere as you produced that figure. – Gavin Simpson Dec 16 '13 at 16:59
  • That is AFAIK a rather old bug in R. – Dirk Eddelbuettel Dec 16 '13 at 17:03
8

The package latticeExtra provides the function 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

Oscar Perpiñán
  • 4,491
  • 17
  • 28
6

Here is an approach using ggplot2 (using the ecdf objects from [Dirk's answer])(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

Community
  • 1
  • 1
mnel
  • 113,303
  • 27
  • 265
  • 254
0

A simpler way is to use ggplot and have the variable that you want to plot as a factor. In the example below, I have Portfolio as a factor and plotting the distribution of Interest Rates by Portfolio.

# select a palette
myPal <- c( 'royalblue4', 'lightsteelblue1', 'sienna1')

# plot the Interest Rate distribution of each portfolio
# make an ecdf of each category in Portfolio which is a factor
g2 <- ggplot(mortgage, aes(x = Interest_Rate, color = Portfolio)) + 
      scale_color_manual(values = myPal) +
      stat_ecdf(lwd = 1.25, geom = "line")
      
g2

You can also set geom = "step", geom = "point" and adjust the line width lwd in the stat_ecdf() function. This gives you a nice plot with the legend.

enter image description here

Bryan Butler
  • 1,750
  • 1
  • 19
  • 19