[R] persp plot + plotting grid lines

David Winsemius dwinsemius at comcast.net
Sun Mar 15 12:22:59 CET 2009


This wiki page has the answer. Draw the plot first to establish the  
coordinate system and create the viewing transformation matrix. Draw  
the grid lines with lines(trans3d()), and then put a:

par(new=T) ... and then redraw the plot over the gridlines.

<http://wiki.r-project.org/rwiki/doku.php?id=tips:graphics- 
base:add_grid>

################
x <- seq(-10, 10, length= 30)
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
op <- par(bg = "white")
  persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
       ltheta = 120, shade = 0.75, ticktype = "detailed",
       xlab = "X", ylab = "Y", zlab = "Sinc( r )"
) -> res

for (ix in seq(-10,10, by=5)) lines (trans3d(x=ix, y=seq(-10,10,  
by=5), z= -10, pmat = res),
                                              col = "red", lty="dotted")
for (iy in seq(-10,10, by=5)) lines (trans3d(x=seq(-10,10, by=5),  
y=iy, z= -10, pmat = res),
                                              col = "red", lty="dotted")
for (ix in seq(-10,10, by=5)) lines (trans3d(x=ix, y=10, z=  
seq(-10,10, by=5), pmat = res),
                                              col = "red", lty="dotted")
for (iz in seq(-10,10, by=5)) lines (trans3d(x=seq(-10,10, by=5),  
y=10, z= iz, pmat = res),
                                              col = "red", lty="dotted")
  for (iy in seq(-10,10, by=5)) {lines (trans3d(x=-10, y=iy, z=  
seq(-10,10, by=5), pmat = res),
                                               col = "red",  
lty="dotted")}
  for (iz in seq(-10,10, by=5)) {lines (trans3d(x=-10, y=seq(-10,10,  
by=5), z= iz, pmat = res),
                                               col = "red",  
lty="dotted")}
  par(new=T)
  persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
        ltheta = 120, shade = 0.75, ticktype = "detailed",
        xlab = "X", ylab = "Y", zlab = "Sinc( r )")
par(op)
-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT

On Mar 14, 2009, at 12:02 PM, Pedro Mardones wrote:

> Dear all;
> Does anyone know how to add grid lines to a persp plot? I've tried
> using lines(trans3d..) but the lines of course are superimposed into
> the actual 3d surface and what I need is something like the plot shown
> in the following link:
> http://thermal.gg.utah.edu/tutorials/matlab/matlab_tutorial.html
> I'll appreciate any ideas
> Thanks
> PM




More information about the R-help mailing list