[R] shading an area in a edf

Ray Brownrigg Ray.Brownrigg at mcs.vuw.ac.nz
Tue Jul 8 23:29:48 CEST 2008


On Tue, 08 Jul 2008, squall44 wrote:
> Hi,
>
> I've got the following edf:
>
> ***
>
> x = c(1.6,1.8,2.4,2.7,2.9,3.3,3.4,3.4,4,5.2)
> F2.5 <- ecdf(x)
> plot(F2.5,
>      verticals= TRUE,
>      do.p = TRUE,
>      lwd=3,
>      ylab = "",
>      xlab = "",
>      xlim = c(1,5.5))
> abline(h= (0:5)*0.2)
>
> #mean
>
> abline(v=mean(x), lwd=2)
> mtext(text=expression(bar(x) == 3.07), side=1, adj=0.462, padj=3, cex=1)
>
> ***
>
> Now I would like to shade the two areas:
>
> http://www.nabble.com/file/p18334136/ecdf.gif
>
> Is it possible to do this without too much effort?
> Thanks for any suggestion
>
> Tobias

How about this:

x = c(1.6,1.8,2.4,2.7,2.9,3.3,3.4,3.4,4,5.2)
F2.5 <- ecdf(x)
plot(F2.5,
     verticals= TRUE,
     do.p = TRUE,
     lwd=3,
     ylab = "",
     xlab = "",
     xlim = c(1,5.5))
abline(h= (0:5)*0.2)
abline(v=mean(x), lwd=2)
mtext(text=expression(bar(x) == 3.07), side=1, adj=0.462, padj=3, cex=1)

# end of your stuff, now to fill the polygons
mx <- mean(x)
x1 <- x[x < mx]
x1a <- rep(c(x1, mx), each=2)
y1 <- c(0, F2.5(rep(x1, each=2)), 0)
polygon(x1a, y1, col=2)
x2 <- x[x >= mx]
x2a <- rep(c(mx, x2), each=2)
y2 <- c(1, F2.5(rep(c(x1[length(x1)], x2[-length(x2)]), each=2)), 1)
polygon(x2a, y2, col=2)

This seems to work even if the mean coincides with one of the x values.

HTH
Ray Brownrigg



More information about the R-help mailing list