[R] 3-D scatter plot laid over Surface plot

David Brahm brahm at alum.mit.edu
Tue Oct 22 17:00:45 CEST 2002

Peter Kraft <pkraft at zoology.uq.edu.au> wrote:
> I have created a Fitness surface (persp())...  Now I would like to
> superimpose a set (XYZ) of existing points ... onto this surface.

Uwe Ligges <ligges at statistik.uni-dortmund.de> has already pointed out Ben
Bolker's <ben at zoo.ufl.edu> excellent R-help message of 3/14/02, which I merely
summarize here.  Save your persp() invisible output as "pmat":
  R> pmat <- persp(...)

Define the handy function "trans3d":
  R> trans3d <- function(x,y,z, pmat) {
  R>   tmat <- t(cbind(x,y,z,1) %*% pmat)
  R>   list(x=tmat[1,]/tmat[4,], y=tmat[2,]/tmat[4,])
  R> }

Now you can convert 3D coordinates to 2D coordinates for use with points, etc:
  R> points(trans3d(myx, myy, myz, pmat))
                              -- David Brahm (brahm at alum.mit.edu)
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list