Skip to content
Prev 157062 / 398506 Next

rgl: ellipse3d with axes

Dear Michael,

Maybe this is irrelevant, since you appear to have a satisfactory solution
now, but here's an approach (from a figure that I drew in a recent book)
that computes the axes directly. This example is in 2D but I think that it
wouldn't be hard to generalize it:

------- snip --------

library(car)
library(MASS)

set.seed(12345)
Sigma <- matrix(c(1, .8, .8, 1), 2, 2)
Z <- mvrnorm(50, mu=c(0,0), Sigma=Sigma, empirical=TRUE)

eqscplot(Z, axes=FALSE, xlab="", ylab="")
abline(h=0, v=0)

ellipse(c(0,0), Sigma, radius=1, center.pch=FALSE, 
    col="black", segments=500)
    
E <- eigen(Sigma)
L <- E$vectors
lam <- sqrt(E$values)

lines(c(1, -1)*lam[1]*L[1,1], c(1, -1)*lam[1]*L[2,1], lwd=2)
lines(c(1, -1)*lam[2]*L[1,2], c(1, -1)*lam[2]*L[2,2], lwd=2)

------- snip --------

Some notes: eqscplot() from the MASS package does the analog of what Duncan
mentioned -- using equal units for both horizontal and vertical axes;
ellipse() from the car package draws the ellipse by transforming a circle,
but the axes are drawn directly using the eigenvalues and vectors of the
covariance (here, correlation) matrix.

Regards,
 John


------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox
On
http://www.R-project.org/posting-guide.html