Skip to content
Prev 173781 / 398503 Next

persp plot + plotting grid lines

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)