[R] ecdf plots, lines, and y values
Martin Maechler
maechler at stat.math.ethz.ch
Fri Jun 4 10:30:29 CEST 2004
>>>>> "UweL" == Uwe Ligges <ligges at statistik.uni-dortmund.de>
>>>>> on Thu, 03 Jun 2004 21:36:48 +0200 writes:
UweL> Monica Palaseanu-Lovejoy wrote:
>> Hi,
>>
>> I have a question for the group, perhaps someone can help me
>> figure this out. I've already looked in the help files and they were
>> no help to me.
>>
>> I have a vector of values and I am plotting an ecdf graph.
>>
>> 1. How can i draw a continuous line through the ecdf points? (lines
>> and type for the plot with an ecdf object does not work)
UweL> Well, the help page ?plot.ecdf and ?plot.stepfun is pretty clear and
UweL> tells us to use verticals = TRUE.
UweL> From the help page:
UweL> F10 <- ecdf(rnorm(10))
UweL> plot(F10, verticals = TRUE, do.p = FALSE)
Thank you Uwe,
but maybe Monica doesn't want to really plot the ecdf, but
rather a "continuized" {polyline} version of it ?
That's a bit less obvious to do and can be achieved by noting
that the (x,y) values are the (x,y) vectors in F10's
environment and that plot() or lines() on a list uses the list's
$x and $y components, e.g.,
F10 <- ecdf(rnorm(10))
plot(F10, verticals = TRUE, do.p = FALSE)
lines(as.list(environment(F10)), col = "blue")
But even more nicely:
with(environment(F10), lines(x,y, col = "blue"))
This last trick can also be applied for the following question:
>> 2. Supposing I have this line drawn. I can add a vertical line of a
>> known x value which intersects the graph. How can I determine the
>> y value of the graph point that is intersected by the abline?
## Construct a "linear interpolator" using approxfun:
polyF10 <- with(environment(F10), approxfun(x,y))
## 'test' it graphically, using curve():
plot(F10, verticals = TRUE, do.p = FALSE)
curve(polyF10(x), add = TRUE, col="red")
## now use it, e.g. for two abscissa values simultaneously
x0 <- c(0, 0.5)
y0 <- polyF10(x0)
x.min <- with(environment(F10), min(x))
## or rather simply
x.min <- par("usr")[1]
segments(x0, 0, x0,y0, lty=3)
segments(x.min,y0, x0,y0, lty=3)
This seems a pretty neat example of making use of the function
objects returned from ecdf() and approxfun().
Regards, Martin Maechler
More information about the R-help
mailing list