Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: using rgl to generate 3d rotatable plots that can be viewed in a web browser?

Tags:

In the world of the R statistics package, rgl allows me to generate 3d plots that I can rotate with my mouse. Is there a way I can export these plots in a portable format, load them in a web browser or other third party tool and rotate them there? I Am especially interested in the web browser solution since this will allow me to share the plots on an internal wiki.

If rgl does not allow this, are there other libraries or strategies that would allow me to accomplish this?

like image 209
Setjmp Avatar asked Oct 05 '11 15:10

Setjmp


People also ask

How do you plot a 3D graph in R?

Creating 3D Plots in R Programming – persp() Function 3D plot in R Language is used to add title, change viewing direction, and add color and shade to the plot. The persp() function which is used to create 3D surfaces in perspective view. This function will draw perspective plots of a surface over the x–y plane.

How do you rotate a 3D plot in R?

To create a 3-D Scatterplot, use plot3d() function (from rgl package) and pass in a data frame where the first three columns represent x, y, and z coordinates, or pass in three vectors representing the x, y, and z coordinates. You can click on the plot and drag it to rotate it.


1 Answers

You could try the vrmlgen package. It will produce 3d VRML files that can be displayed with a browser plugin; you can find a plugin at VRML Plugin and Browser Detector.

Once you've installed a plugin, try this:

require(vrmlgen) example(bar3d) 

NB: the example code didn't automatically open in a browser for me (RStudio, Win7, Chrome) because the path got mangled. You might need to use:

require(stringr) browseURL(str_replace_all(file.path(outdir, 'barplot.html'), fixed('\\'), '/')) 

If you don't want to install a VRML plugin, you could use X3DOM instead. You'll need a converter, but your users should be able to view them with just a (modern) browser. You might have to modify the following code to get the paths right:

setwd(outdir) aopt <- 'C:/PROGRA~1/INSTAN~1/bin/aopt' # Path to conversion program vrml <- 'barplot.wrl' x3dom <- 'barx.html' command <- paste(aopt, '-i', vrml, '-N', x3dom) system(command) # LOG   Avalon   Init: 47/616, V2.0.0 build: R-21023 Jan 12 2011 # LOG   Avalon   Read url # LOG   Avalon   Read time: 0.074000 # ============================================ # Call: writeHTML with 1 param  # Write raw-data to barx.html as text/html # WARNING   Avalon   Run NodeNameSpace "scene" destructor and _nodeCount == 3 # WARNING   Avalon   Try to remove nodes from parents # WARNING   Avalon   PopupText without component, cannot unregister # WARNING   Avalon   Avalon::exitSystem() call and node/obj left: 0/3331 browseURL(file.path(outdir, 'barx.html')) setwd(curdir) 
like image 78
pete Avatar answered Dec 20 '22 02:12

pete