[R] Adding 3D points
David Brahm
brahm at alum.mit.edu
Thu Aug 26 21:02:10 CEST 2004
<dhirm001 at student.ucr.edu> wrote:
> I would like to add individual points and lines to a persp() plot that I
> generated with the geoR package.
See example (2) in ?persp. You must define this function:
trans3d <- function(x,y,z, pmat) {
tr <- cbind(x,y,z,1) %*% pmat
list(x = tr[,1]/tr[,4], y= tr[,2]/tr[,4])
}
Then you must assign the result of your persp() call to "pmat", e.g.:
R> x <- y <- seq(-10, 10, length = 50)
R> f <- function(x, y) {r <- sqrt(x^2+y^2); 10 * ifelse(r==0, 1, sin(r)/r)}
R> z <- outer(x, y, f)
R> pmat <- persp(x, y, z, theta=30, phi=30, expand=.5, col="lightblue",
+ xlab="X", ylab="Y", zlab="Z", ticktype="detailed")
And then you can add points and lines:
R> points(trans3d(0,0,f(0,0),pmat))
R> z2 <- sapply(1:length(x),function(n)f(x[n],y[n]))
R> lines(trans3d(x,y,z2,pmat),col="red",lwd=2)
R> lines(trans3d(c(-10,10,10,-10,-10),c(-10,-10,10,10,-10),c(2,2,8,8,2), pmat),
+ col="blue")
All hail to Ben Bolker, who kindly taught me this in March 2002.
--
-- David Brahm (brahm at alum.mit.edu)
More information about the R-help
mailing list