[R] rgl: plot3d and ellipse3d

Duncan Murdoch murdoch at stats.uwo.ca
Wed Sep 17 17:28:05 CEST 2008


On 9/17/2008 9:40 AM, Michael Friendly wrote:
> Yihui Xie wrote:
>> Hi Michael,
>>
>> You need to specify both 'box' and 'axes' to FALSE to avoid the box
>> lines (if you don't specify the latter one, there will still be axes
>> lines).
>>
>>
>>   
> Hi Yihui
> I tried several variations, none of which would simply add the ellipse 
> and nothing else to the original
> plot of points:
> 
> plot3d(trees, type="s", size=0.5, col="blue", cex=2)
> 
> # how to avoid the extra box?

As Yihui said, use axes=FALSE, box=FALSE in the call above.  However, 
there's another possibility:  remove the box after adding it.  For 
example, if you save the result of the plot3d call, you'll see the ids 
of the various bits and pieces:

 > myplot <- plot3d(trees, type="s", size=0.5, col="blue", cex=2)
 > myplot
      data      axes box.lines      xlab      ylab      zlab
        28         1        29        30        31        32

Now you can remove the ones you don't like:

 > rgl.pop(id=1)
 > rgl.pop(id=29)

You can also use rgl.ids() to print the list of currently removable 
shapes, but it doesn't know the purpose of each, so is less informative:

 > myplot <- plot3d(trees, type="s", size=0.5, col="blue", cex=2)
 > myplot
      data      axes box.lines      xlab      ylab      zlab
        33         1        34        35        36        37
 > rgl.ids()
   id    type
1 33 spheres
2 34   lines
3 35    text
4 36    text
5 37    text

(It also misses the axes, because of the strange way rgl handles them.)

Duncan Murdoch

> # -- this doesn't remove the extra box, but keeps the points & axis labels
> plot3d( ellipse3d(cov, centre=mu, level=0.68, box=FALSE), col="pink", 
> alpha=0.2,  add = TRUE, axes=FALSE)
> 
> # -- this removes the original axes, the points & labels axes x, y, z
> plot3d( ellipse3d(cov, centre=mu, level=0.68, box=FALSE, col="pink", 
> alpha=0.2,  add = TRUE, axes=FALSE))
> 
> -Michael
>>
>> On Wed, Sep 17, 2008 at 8:34 PM, Michael Friendly <friendly at yorku.ca> wrote:
>>   
>>> Hi
>>> I'm trying to make a 3d plot showing a point cloud, the corresponding data
>>> ellipse
>>> and the principal axes of the ellipse as vectors.
>>>
>>> library(rgl)
>>> data(trees)
>>> cov <- cov(trees)
>>> mu <- mean(trees)
>>>
>>> plot3d(trees, type="s", size=0.5, col="blue", cex=2)
>>>
>>> In this step, an extra box is added.  I've tried using box=FALSE, but it has
>>> no effect.
>>> # how to avoid the extra box?
>>> plot3d( ellipse3d(cov, centre=mu, level=0.68), col="pink", alpha=0.2,  add =
>>> TRUE)
>>>
>>> Here's what I've tried to plot the principal axes in variable space, using
>>> the result of prcomp().
>>> But I've got something wrong, because, although they are at right angles,
>>> they don't
>>> align with the ellipse.
>>>
>>> PC <- princomp(trees)
>>> sdev <- PC$sdev         # component standard deviations
>>> sd <- sqrt(diag(cov))   # variable standard deviations
>>>
>>> # vectors in variable space of principal components
>>> vec <- matrix(mu,3,3, byrow=TRUE) + diag(sd) %*% PC$loadings
>>>
>>> for (j in 1:3) {
>>>  mat <- rbind(mu, vec[j,])
>>>  segments3d(mat, col="red")
>>> }
>>>
>>> Can someone help?
>>>     
>



More information about the R-help mailing list