[R] rgl: ellipse3d with axes
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Sep 24 17:11:04 CEST 2008
On 24/09/2008 10:12 AM, Michael Friendly wrote:
> Duncan Murdoch wrote:
>> The normals component contains the surface normals. It is used to
>> help in rendering the surface, but isn't much use for your purposes.
>> Unfortunately, I'm not familiar enough with the internals of princomp
>> to tell you how to get the axes you want.
>>
>> Duncan Murdoch
>
> OK, then let me repharase it. The axes of the unit sphere are like
>
> axes <- matrix(
> c(0, 0, -1, 0, 0, 1,
> 0, -1, 0, 0, 1, 0,
> -1, 0, 0, 1, 0, 0), 6, 3, byrow=TRUE)
> taken in pairs. I'd like to transform these coordinates the same was as
> in ellipse3d() and add them to the plot.
That's easy, but it doesn't give you the principal axes of the ellipse.
Just use
axes %*% chol(cov)
If you start with a unit sphere, this will give you points on its
surface, but not the ones you want. For those you need the SVD or
eigenvectors. This looks like it does what you want:
axes <- matrix(
c(0, 0, 0, # added origin
0, 0, -1, 0, 0, 1,
0, -1, 0, 0, 1, 0,
-1, 0, 0, 1, 0, 0), 7, 3, byrow=TRUE)
axes <- axes[c(1,2,1,3,1,4,1,5,1,6,1,7),] # add the origin before each
cov <- cov(trees)
eigen <- eigen(cov)
shade3d(ellipse3d(cov, t=1, alpha=0.2, col='red'))
segments3d(axes %*% sqrt(diag(eigen$values)) %*% t(eigen$vectors))
Duncan Murdoch
More information about the R-help
mailing list