1
### Demos for persp() plots -- things not in example(persp)
2
### -------------------------
4
## is FALSE in demo() {using source()}:
5
## if(dev.interactive())
6
## Use a ``portable'' (;-) hack instead:
7
#is.dev.interactive <- eval(body(dev.interactive)[[3]])
10
## (1) The Obligatory Mathematical surface.
11
## Rotated sinc function.
13
x <- seq(-10, 10, length = 50)
15
rotsinc <- function(x,y)
17
sinc <- function(x) { y <- sin(x)/x ; y[is.na(y)] <- 1; y }
18
10 * sinc( sqrt(x^2+y^2) )
20
sinc.exp <- expression(z == Sinc(sqrt(x^2 + y^2)))
22
z <- outer(x, y, rotsinc)
26
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
27
title(sub=".")## work around persp+plotmath bug
28
title(main = sinc.exp)
31
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
32
ltheta = 120, shade = 0.75, ticktype = "detailed",
33
xlab = "X", ylab = "Y", zlab = "Z")
34
title(sub=".")## work around persp+plotmath bug
35
title(main = sinc.exp)
39
## (2) Visualizing a simple DEM model
42
z <- 2 * volcano # Exaggerate the relief
43
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
44
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
45
persp(x, y, z, theta = 120, phi = 15, scale = FALSE, axes = FALSE)
48
## (3) Now something more complex
49
## We border the surface, to make it more "slice like"
50
## and color the top and sides of the surface differently.
53
z <- rbind(z0, cbind(z0, z, z0), z0)
54
x <- c(min(x) - 1e-10, x, max(x) + 1e-10)
55
y <- c(min(y) - 1e-10, y, max(y) + 1e-10)
57
fill <- matrix("green3", nr = nrow(z)-1, nc = ncol(z)-1)
58
fill[ , i2 <- c(1,ncol(fill))] <- "gray"
59
fill[i1 <- c(1,nrow(fill)) , ] <- "gray"
62
persp(x, y, z, theta = 120, phi = 15, col = fill, scale = FALSE, axes = FALSE)
63
title(main = "Maunga Whau\nOne of 50 Volcanoes in the Auckland Region.",
68
persp(x, y, z, theta = 135, phi = 30, col = fill, scale = FALSE,
69
ltheta = -120, lphi = 15, shade = 0.65, axes = FALSE)
71
## Don't draw the grid lines : border = NA
72
persp(x, y, z, theta = 135, phi = 30, col = "green3", scale = FALSE,
73
ltheta = -120, shade = 0.75, border = NA, box = FALSE)
75
## `color gradient in the soil' :
76
fcol <- fill ; fcol[] <- terrain.colors(nrow(fcol))
77
persp(x, y, z, theta = 135, phi = 30, col = fcol, scale = FALSE,
78
ltheta = -120, shade = 0.3, border = NA, box = FALSE)
80
## `image like' colors on top :
82
zi <- volcano[ -1,-1] + volcano[ -1,-61] +
83
volcano[-87,-1] + volcano[-87,-61] ## / 4
85
terrain.colors(20)[cut(zi, quantile(zi, seq(0,1, len = 21)),
86
include.lowest = TRUE)]
87
persp(x, y, 2*z, theta = 110, phi = 40, col = fcol, scale = FALSE,
88
ltheta = -120, shade = 0.4, border = NA, box = FALSE)