グラフィックス参考実例集:鳥瞰図 persp
(グラフィックス参考実例集に戻る。Rのグラフィックスパラメータを参照する。)
persp() 関数は二次元曲面の鳥瞰図を描く。ヒストグラムと密度の推定 にも実例あり。
Function to generate a matrix of colors to be used in perspective plots (based on demo(persp) and the comments of U. Ligges
surf.colors <- function(x, col = terrain.colors(20)) { # First we drop the 'borders' and average the facet corners # we need (nx - 1)(ny - 1) facet colours! x.avg <- (x[-1, -1] + x[-1, -(ncol(x) - 1)] + x[-(nrow(x) -1), -1] + x[-(nrow(x) -1), -(ncol(x) - 1)]) / 4 # Now we construct the actual colours matrix colors = col[cut(x.avg, breaks = length(col), include.lowest = T)] return(colors) }
# Now lets look at an example of using it: # first lets build some random surface library(MASS) x <- cbind(rnorm(100), rnorm(100)) x <- kde2d(x[,1], x[,2], n = 100)$z # now lets plot it! par(bg = "gray") persp(x, col = surf.colors(x), phi = 30, theta = 225, box = F, border = NA, shade = .4)
persp(x, col = surf.colors(x, col = heat.colors(40)), phi = 30, theta = 225, box = F, border = NA, shade = .4)
persp(x, col = surf.colors(x, col = topo.colors(40)), phi = 30, theta = 225, box = F, border = NA, shade = .4)
persp(x, col = surf.colors(x, col = gray(seq(0, 1, len = 40))), phi = 30, theta = 225, box = F, border = NA, shade = .4)