[R] two perspective plots in in plot

Duncan Murdoch murdoch at stats.uwo.ca
Fri Feb 9 19:41:05 CET 2007


On 2/9/2007 1:11 PM, Greg Snow wrote:
> Probably the easiest way is to use the "wireframe" function in the
> lattice package.  The second example in the help shows 2 surfaces (you
> do need to combine the data into a single data frame).
> 
> If you really want to use the "persp" function, then you could create
> the first plot, then call "par(new=TRUE)" and then do the 2nd plot, but
> that would take a lot of thinking to get the axes and scales to line up
> properly and make it look good.

Another alternative is to use the persp3d function and surface3d 
functions in the rgl package.  It would be quite tricky to get persp to 
handle hidden surfaces properly, whereas rgl will just do it (as long as 
neither is transparent.  Transparency is hard.)

For example, after running example(persp) so that x, y, and z contain 
values that were just used in

persp(x, y, z, theta = 135, phi = 30, col = "green3", scale = FALSE,
       ltheta = -120, shade = 0.75, border = NA, box = FALSE)

you can run

  library(rgl)

  persp3d(x,y,z, col="green3", aspect="iso", axes=FALSE, box=FALSE, 
xlab="", ylab="", zlab="")

  persp3d(x,y,(z + mean(z))/2, col="red", add=TRUE)

and then rotate the surfaces to the desired viewing angle.

Duncan Murdoch



More information about the R-help mailing list