R の image() で出力したカラーマップの横に反例として値と色の対応を示したい
GNU R で、image() を使って3次元データをカラーマップとして可視化するとき、その横に色と値の対応を示したいですよね。R はそのような機能を用意してくれているのかな?
少し探したけど発見できなかったので、以下のようにしています。もっと良い方法があったら教えてください。
# plotting colormap xs <- 1:100 ys <- 1:100 zs <- sapply(xs, function (x) { sapply(ys, function (y) { sin(x/(2*pi)) + cos(y/(2*pi)) }) }) z.range <- c(min(zs), max(zs)) default.par <- par(no.readonly=TRUE) layout(matrix(c(1, 2), 2, 2, byrow=TRUE), c(1, 0.12)) par(mar=c(default.par$mar[1], default.par$mar[2], default.par$mar[3], 1)) image(xs, ys, zs) # plotting legend xs.c <- 1 ys.c <- min(zs) + (max(zs) - min(zs)) * 1:100 / 100 zs.c <- matrix(ys.c, 1, 100) par(mar=c(default.par$mar[1], 0, default.par$mar[3], default.par$mar[4] + 1)) image(xs.c, ys.c, zs.c, axes=FALSE, xlab=NA, ylab=NA) axis(4) box() par(default.par)
結果