[R] persp(): add second plane

Ben Bolker ben at zoo.ufl.edu
Thu Mar 14 22:15:36 CET 2002


  Not directly, but with a little bit of ingenuity you can hack one.

  It's completely undocumented, but persp() returns a 4x4 matrix that
allows you to compute the perspective transform for any other set of
(x,y,z) coordinates, and you can then use points/lines/whatever to
superimpose these on the plot.  (Hidden line removal is a harder problem.)

  Here is some example code (the key is the trans3d() function) that adds
a point, a line, and a plane (of sorts) to one of the persp() example
plots.

  This is essentially how the points3d(), plane3d(), xyz.convert()
functions returned by a call to the scatterplot3d() function (found in its
own library on CRAN) work ...


## matrix multiply c(3dv,1) by transformation matrix:
## plot v[0]/v[3], v[1]/v[3]

x <- seq(-10, 10, length = 50)
y <- x
f <- function(x, y) {
    r <- sqrt(x^2 + y^2)
    10 * sin(r)/r
}
z <- outer(x, y, f)
z[is.na(z)] <- 1
par(bg = "white")

trans3d <- function(x,y,z,pmat) {
  tmat <- t((cbind(x,y,z,1)%*% pmat))
  list(x=tmat[1,]/tmat[4,],y=tmat[2,]/tmat[4,])
}

pmat <- persp(x, y, z, theta = 30, phi = 30, expand = 0.5,
              col = "lightblue", xlab = "X", ylab = "Y", zlab = "Z",
              ticktype="detailed")
m <- 1e-5
points(trans3d(m,m,f(m,m),pmat),pch=16)
z2 <- sapply(1:length(x),function(n)f(x[n],y[n]))
lines(trans3d(x,y,z2,pmat),col="red",lwd=2)
lines(trans3d(c(-10,10,10,-10,-10),
              c(-10,-10,10,10,-10),
              c(2,2,8,8,2),pmat),col="blue")

On Thu, 14 Mar 2002, Nina Lieske wrote:

> Dear R-users,
>
> is it possible to add a second plane to the persp()-plot? I couldn't find
> any hint on that in the news archive...
> I'm using R.1.4.1 for Windows.
>
> Thanks in advance,
> Nina
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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