Skip to content

3d scatter plot with drop line

1 message · davidr@rhotrading.com

#
Very nice!
I noticed that you wrote the function to drop points to any surface.
Is it possible to add the surface to the plot as a translucent surface,
so the points and drop lines still show?

David L. Reiner

-----Original Message-----
From: Robin Hankin [mailto:rksh at soc.soton.ac.uk] 
Sent: Thursday, November 18, 2004 3:39 AM
To: joel30000 at gmail.com
Cc: r-help at stat.math.ethz.ch
Subject: Fwd: Re: [R] 3d scatter plot with drop line



Hi

try this:




p3dpairs <- function(x,x1, 
xlim=NULL,ylim=NULL,zlim=NULL,col=par("col"), pch=par("pch"), 
cex=par("cex"), ...){
   if(is.matrix(x)){
     z <- x[,3]
     y <- x[,2]
     x <- x[,1]
   }

     if(is.matrix(x1)){
     z1 <- x1[,3]
     y1 <- x1[,2]
     x1 <- x1[,1]
   }

   if(missing(zlim)) {
     z.grid <- matrix(range(z),2,2)
   } else {
     z.grid <- matrix(zlim,2,2)
   }

   if(missing(xlim)){ xlim <- range(x) }
   if(missing(ylim)){ ylim <- range(y) }

   persp(xlim, ylim, z.grid, col = NA, border=NA, ...) -> res

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

   out <- trans3d(x,y,z,pm=res)
   out1 <- trans3d(x1,y1,z1,pm=res)
   points(out, col=col, pch=pch, cex=cex, ...)

   for(i in 1:length(out$x)){
     lines(c(out$x[i],out1$x[i]),c(out$y[i],out1$y[i]), col="gray", ...)
   }
   return(invisible(out))
}




then


a <- matrix(rnorm(60),20,3)
  b <- a
b[,3] <- 0
  p3dpairs(a,b)


gives you a good approximation to what you want

HTH

rksh
http://www.R-project.org/posting-guide.html
precaution)