r - 如何在 R persp 函数中沿 z 轴定位配色方案?

标签 r colors

我有一个名为 ht2 的矩阵。我使用 persp 函数生成 3D View 。

ht2 <- matrix(1, 29, 36)
ht2[4:26,4:33] <- 0
ht2[6:10,6:31] <- 3
ht2[13:17,6:31] <- 3
ht2[20:24,6:31] <- 3

persp(ht2, expand=0.03, theta=25, phi=25, shade=0.75, col=terrain.colors(999,alpha=1))

这给了我: Current Plot

如您所见,颜色从绿色到黄色再到棕色沿 y 轴变化。但是,我宁愿沿 z 轴更改它。

我正在寻找任何简单的方法来做到这一点。

最佳答案

我在这个网站上找到了一个可能的解决方案:

https://stat.ethz.ch/pipermail/r-help/2003-July/036151.html

  levelpersp <- function(x, y, z, colors=topo.colors, ...) {
  ## getting the value of the midpoint
  zz <- (z[-1,-1] + z[-1,-ncol(z)] + z[-nrow(z),-1] + z[-nrow(z),-ncol(z)])/4
  ## calculating the breaks
  breaks <- hist(zz, plot=FALSE)$breaks
  ## cutting up zz
  cols <- colors(length(breaks)-1)
  zzz <- cut(zz, breaks=breaks, labels=cols)
  ## plotting
  persp(x, y, z, col=as.character(zzz), ...)
  ## return breaks and colors for the legend
  list(breaks=breaks, colors=cols)
  }


## Example
x <- seq(-10, 10, length=60)
y <- x
f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
z <- outer(x, y, f)
levelpersp(x, y, z, theta = 30, phi = 30, expand = 0.5)

有人可能会建议一种在原始问题中实现这一点的方法。

关于r - 如何在 R persp 函数中沿 z 轴定位配色方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33517644/

相关文章:

r - r中的矩阵重排和重组

r - 在 R 中安装旧包时出错

r - 如何使用 Roxygen 正确记录来自不同包的泛型的 S3 方法?

javascript - 如何获得纯色背景色和半透明叠加色的有效结果(6位十六进制值)?

将颜色值从浮点 0..1 转换为字节 0..255

regex - 将包含不同日期格式的数据框列转换为 Date 对象

r - 为什么 R 中的 as.integer 使值递减?

javascript - Canvas 中圆弧的不同 fillStyle 颜色

algorithm - 如何将 RGB 代码转换为 8 个简单间隔(可能?)

python - 在 Python 中按颜色搜索图像