# an obvious choice:
# Dr. R's favorite New Zealand Volcano!
data( volcano)
M<- nrow( volcano)
N<- ncol( volcano)
x<- seq( 0,1,,M)
y<- seq( 0,1,,N)
pm<- drape.plot( x,y,volcano, col=terrain.colors(128))
# use different range for color scale and persp plot
# setting of border omits the mesh lines
drape.plot( x,y,volcano, col=topo.colors(128),zlim=c(0,300),
zlim2=c( 120,200), border=NA)
# note tranparent color for facets outside the zlim2 range
#The projection has been saved in pm
# add a point marking the summit
zsummit <- max( volcano)
ix<- row( volcano)[volcano==zsummit]
iy <- col( volcano)[volcano==zsummit]
uv <- trans3d( x[ix], y[iy],zsummit,pm)
points( uv, col="magenta", pch="+", cex=2)
# overlay volcano wireframe with gradient in x direction.
dz<- (
volcano[1:(M-1), 1:(N-1)] - volcano[2:(M), 1:(N-1)] +
volcano[1:(M-1), 2:(N)] - volcano[2:(M), 2:(N)]
)/2
# convert dz to a color scale:
zlim<- range( c( dz), na.rm=TRUE)
zcol<-drape.color( dz, zlim =zlim, col = viridis(64) )$color.index
# with these colors
persp( volcano, col=zcol, theta=30, phi=20,
border=NA,expand=.3 )
# add legend using image.plot function
image.plot( zlim=zlim, legend.only =TRUE, horizontal =TRUE,
col= viridis(64))
Run the code above in your browser using DataLab