# [R] What does persp() return?

Ben Bolker ben at zoo.ufl.edu
Thu Aug 1 22:38:31 CEST 2002

```  I seem to recall this coming up before (I'm on a slow link so will not
check the mailing list archives), but here is a bit of hackery for adding
points and lines (etc.) to a persp() plot.  Of course, it doesn't do
hidden line removal ...

## 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, 1 Aug 2002, David Brahm wrote:

> I want to plot some 3D points on top of the grid produced by persp().
>
> On 2/22/01, Paul Murrell <paul at stat.auckland.ac.nz> wrote in R-help:
> > In S-Plus, persp() returns a value that can be used to transform 3D
> > locations to 2D, but this sort of thing is not (yet) available in R.
>
> But persp() does return something (in R-1.5.1): a 4x4 matrix which in the C
> code is called the "viewing transformation matrix", VT.  Can VT be used to plot
> additional points?  If not, is such a thing in the works?
>
> Paul also suggested Uwe Ligges's "scatterplot3d" package, which is very nice
> but seems better at producing points than grids.
>
> Thanks!
>

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

```