R: Using Rgl to Generate 3D Rotatable Plots That Can Be Viewed in a Web Browser

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

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)

How to export an interactive rgl 3D Plot to share or publish?

You can export the interactive plot to html using knitr. For the details see: including a interactive 3D figure with knitr.

Export a 3d plot from Octave to be shown interactively on a web browser

Octave itself creates 3d charts, but they are not interactive:

http://www.gnu.org/software/octave/doc/interpreter/Three_002dDimensional-Plotting.html

So you'll have to use another application and have octave feed data into 3d interactive charting software so that users can play with it on a webpage.

"Processing" tool for 3d interactive charting

software from (http://www.processing.org/) could help you. You will be responsible for exporting the raw data from octave into the format acceptable for the interactive 3d plots.

webMathematica 3.2 tool for 3d interactive charting

software from (http://www.wolfram.com/products/webmathematica/examples/examples.html) that you can embed into your web page that can generate interactive and dynamic 2d and 3d content for users to play with.

R plot gam 3D surface to show also actual response values

As @李哲源 pointed out in the comments, you shouldn't use plot here, because it's not flexible enough. Here's a version based on the referenced question Rough thin-plate spline fitting (thin-plate spline interpolation) in R with mgcv.

# First, get the fit
library(mgcv)
fit <- gam( carb ~ te(cyl, hp, k=c(3,4)), data = mtcars)

# Now expand it to a grid so that persp will work
steps <- 30
cyl <- with(mtcars, seq(min(cyl), max(cyl), length = steps) )

hp <- with(mtcars, seq(min(hp), max(hp), length = steps) )
newdat <- expand.grid(cyl = cyl, hp = hp)
carb <- matrix(predict(fit, newdat), steps, steps)

# Now plot it
p <- persp(cyl, hp, carb, theta = 45, col = "yellow")

# To add the points, you need the same 3d transformation
obs <- with(mtcars, trans3d(cyl, hp, carb, p))
pred <- with(mtcars, trans3d(cyl, hp, fitted(fit), p))
points(obs, col = "red", pch = 16)

# Add segments to show where the points are in 3d
segments(obs$x, obs$y, pred$x, pred$y)

That produces the following plot:

screen shot

You might not want to make predictions so far from the observed data. You can put NA values into carb to avoid that. This code does that:

exclude <- exclude.too.far(rep(cyl,steps), 
rep(hp, rep(steps, steps)),
mtcars$cyl,
mtcars$hp, 0.15) # 0.15 chosen by trial and error
carb[exclude] <- NA
p <- persp(cyl, hp, carb, theta = 45, col = "yellow")
obs <- with(mtcars, trans3d(cyl, hp, carb, p))
pred <- with(mtcars, trans3d(cyl, hp, fitted(fit), p))
points(obs, col = "red", pch = 16)
segments(obs$x, obs$y, pred$x, pred$y)

That produces this plot:

screen shot

Finally, you might want to use the rgl package to get a dynamic graph instead. After the same manipulations as above, use this code to do the plotting:

library(rgl)
persp3d(cyl, hp, carb, col="yellow", polygon_offset = 1)
surface3d(cyl, hp, carb, front = "lines", back = "lines")
with(mtcars, points3d(cyl, hp, carb, col = "red"))
with(mtcars, segments3d(rep(cyl, each = 2),
rep(hp, each = 2),
as.numeric(rbind(fitted(fit),
carb))))

Here's one possible view:

screen shot

You can use the mouse to rotate this one if you want to see it from a different angle. One other advantage is that points that should be hidden by the surface really are hidden; in persp, they'll plot on top even if they should be behind it.

Put a fixed title in an interactive 3D plot using rgl package, R

This cannot be done in rgl at present.



Related Topics



Leave a reply



Submit