[R] Exporting interactive 3D plots with axes and labels

Duncan Murdoch murdoch.duncan at gmail.com
Fri May 13 02:21:52 CEST 2011


On 12/05/2011 4:47 PM, van Rijn, Peter wrote:
> Hi,
>
> I have a question about exporting interactive 3D plots. I use the following code to plot a contour of a trivariate normal distribution:
>
> library(mvtnorm)
> library(rgl)
> library(misc3d)
>
> n=25
> x=seq(-3,3,length=n)
> X=cbind(rep(x,each=n**2),rep(rep(x,each=n),n),rep(x,n**2))
> p=array(dmvnorm(X,sigma=diag(3)*0.5+0.5),c(n,n,n))
>
> contour3d(p,x,x,x,level=mean(p))
> lim=c(-3,3)
> decorate3d(xlim=lim,ylim=lim,zlim=lim,xlab="Variable X",ylab="Variable Y",zlab="Variable Z")
>
> 1.      I know thanks to Luke Tierney (www.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf) how to export the 3D plot without the axes and labels with:
>
> dummy=function(...){
>              contour3d(p,x,x,x,level=mean(p),scale = FALSE,...)
> }
>
> conts=dummy()
>
> saveTrianglesAsOFF<- function(scene, filename = "scene.OFF") {
>              scene<- misc3d:::colorScene(scene)
>              triangles<- misc3d:::canonicalizeAndMergeScene(scene, "color", "color2",
>              "alpha", "col.mesh",
>              "fill", "smooth")
>              ve<- misc3d:::t2ve(triangles)
>              f<- file(filename, open = "w")
>              on.exit(close(f))
>              write("OFF", f)
>              write(c(ncol(ve$vb), ncol(ve$ib), 3 * ncol(ve$ib)), f, 3)
>              write(ve$vb, f, 3)
>              write(rbind(3, ve$ib - 1), f, 4)
>              invisible(NULL)
> }
>
> saveTrianglesAsOFF(conts,"plot.OFF")
>
> However, I would like to have the axes and labels in the export as well. Simply adding the decorate3d to the dummy function does not work,
> nor does creating a separate dummy. (I do know how to combine multiple exports in Meshlab).
>
> Does anyone know how to do this in some way or the other, or is this simply not possible?

It's not currently possible, but it's on my wish list.

What you're doing is grabbing the triangles before they get sent to rgl. 
  I suppose you could try to do the same for all the axes and labels, 
but a better solution (which is what is on the wish list) is to be able 
to query an rgl scene and get back complete information (or at least 
geometric information) on every object in it.  Then you wouldn't care 
how the triangles were produced, you'd just have what you need to draw 
them again.

This isn't theoretically difficult, but it's a lot of tedious 
programming, and I just haven't needed it enough to put my nose to the 
grindstone and do it.

Duncan Murdoch



More information about the R-help mailing list