[R] Drape 3D scatterplot

Duncan Murdoch murdoch.duncan at gmail.com
Mon Mar 26 20:37:13 CEST 2012

On 26/03/2012 1:34 PM, John Benning wrote:
> Hi,
> *Warning: R newb here. *
> I've got a 3D scatterplot (
> http://www.fleetwoodswoodworks.com/scatterPlot.jpg) which I want to drape
> in color, with the draped plane corresponding to the mean z values on the
> plot (similar to: http://www.fleetwoodswoodworks.com/draped3Dscatterplot.jpg).
> Does anyone know what package will allow me to do this? I used *
> scatterplot3d()* for my first plot. I've tried doing a *persp()* plot and
> draping that using *drape.plot()* in the *fields *package, but that colors
> the facets without differentiating between the four points (corners) that
> make up the facet...if that makes sense (see
> http://www.fleetwoodswoodworks.com/color3D.jpg). Draping *wireframe()* from
> the *lattice *package does the same thing.

You should be able to get what you want with the rgl package.  Some 
people have reported seg faults after the latest CRAN update; if you get 
those and can tell what the problem is, please tell me.  If you can't 
tell what's happening, you could try the previous release.

I don't know the calculations that go into the "draped plane", but this 
will give a somewhat similar looking picture:

x <- 1:5
y <- 1:4
z <- outer(x, y, function(x, y) x+y+rnorm(20))
low <- min(z)
high <- max(z)
col <- round(1+100*(z-low)/(high-low))

# Show the surface
persp3d(x,y,z, col=heat.colors(101)[col])

# now show some fake data
x1 <- outer(x, y, function(x, y) x)
y1 <- outer(x, y, function(x, y) y)
for (i in 1:5)
   points3d(x1,y1,z+rnorm(20, sd=0.2))

You can use alpha=0.5 (or some other value) in the persp3d call to get a 
partially transparent surface, or some other function to set the colors.

Duncan Murdoch

More information about the R-help mailing list