[R] density ellipses?

Martyn Plummer plummer at iarc.fr
Wed Mar 22 18:59:23 CET 2000

On 22-Mar-00 Kaspar Pflugshaupt wrote:
> Hello,
> has anybody written a function to plot density ellipses (95%, 99% or
> anything) in a scatterplot? I found nothing in any package, nor in the list
> archives. 
> There does seem to be a contributed package "ellipse" for S-Plus (on
> S-Archive), but it does a lot more than what I would need. Still, if anybody
> ported it to R, I'd be grateful for a link. I'm a bit afraid to try the port
> myself, since the routines do some magic with postscript preambles that I
> don't understand.
> Or is there a simple way to implement it myself? The axis directions could
> be extracted from princomp, but how would one plot the ellipses?
> Thanks for any tips
> Kaspar

You can calculate a 2D kernel density estimate on a regular grid using
the bkde2D() function (package Kernsmooth) and then plot highest density
regions with contour(). An example giving a 90% region:

x <- rnorm(1000)
y <- (x+rnorm(1000))/sqrt(2)
z <- bkde2D(cbind(x,y), bandwidth=c(0.4,0.4))
zsort <- sort(z$fhat)
p <- cumsum(zsort)/sum(zsort)
contour(z$x1,z$x2,z$fhat, levels=min(zsort[p>=0.10]))

You need to play with the bandwidth.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list