我有一个名为 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))
这给了我:
如您所见,颜色从绿色到黄色再到棕色沿 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/