# function for perspective plot persp2 <- function(x,y,z1, ncolors=10, theta=-40, phi=35, ltheta = 120, lphi=0, shade=0.4, colSwitch=2, labVec=c('','',''),...) { # this is adapted by Ian Taylor from R code in demo(persp) # please share, adapt, & improve # colSwitch: 1 for b/w, 2 for heat colors # this is not intended to be general, rather to meet my current wishes # rescale likelihood so axis labels are simple and rename input vectors to make a gray sides to the plot z <- rbind(0, cbind(0, z1, 0), 0) # add zero values at the margins of the grid x <- c(min(x) - 1e-10, x, max(x) + 1e-10) # add values to x and y corresponding to those zeros y <- c(min(y) - 1e-10, y, max(y) + 1e-10) # compute a matrix of the average height of a cell between four grid point corners zi <- (z1[-1, -1] + z1[-1, -ncol(z1)] + z1[-nrow(z1), -1] + z1[-nrow(z1), -ncol(z1)])/4 # make a matrix of color vectors fcol <- matrix("gray", nr = nrow(z) - 1, nc = ncol(z) - 1)# make initial matrix of gray # fill in inside of matrix with colors values based on the height of zi if(colSwitch==1){ fcol[c(-1, -nrow(fcol)), c(-1, -ncol(fcol))] <- gray(0:ncolors/ncolors)[floor(ncolors*zi/max(zi))+1] }else{ fcol[c(-1, -nrow(fcol)), c(-1, -ncol(fcol))] <- heat.colors(ncolors+1)[floor(ncolors*zi/max(zi))+1] } # make the perspective plot res <- persp(x, y, z, theta=theta, phi=phi, col = fcol, scale=T, expand=0.6, ltheta=ltheta, lphi=lphi, shade=shade, border = NA, ticktype="detailed", xlab=labVec[1], ylab=labVec[2], zlab=labVec[3],...) } # lame example (the code to plot the morphs needs too much cleaning up at this point) x <- 1:50 y <- 1:70 z <- matrix((expand.grid(x,y)$Var1-20)^2 + (expand.grid(x,y)$Var2 - 30)^2,50,70) res <- persp2(x, y, z, labVec=c('x label','y label','z label')) lines(trans3d(x, rep(min(y),50),500+500*runif(50), res))