r语言 - 水平图中有 3 个不同的颜色区域



尝试绘制此矩阵,使负值为红色,0为蓝色,正值为绿色。

m <- matrix(c(0,0,-1,-2,0,0,1,0,1,3,3,3,-3,0,0,2),nrow = 4)
levelplot(m,at=c(-3,0,3),col.regions=c("red","blue","green"),xlab = xlab.a, ylab="", colorkey = FALSE, panel = function(...) {
panel.fill(col = "blue")
panel.levelplot(...)
})

以下是使用布尔比较的另一种通用方法:

levelplot((m>=0) + (m > 0),at=c(-0.1, 0:2),col.regions=c("red","blue","green"), xlab = "", ylab="", colorkey = FALSE, panel = function(...) {
panel.fill(col = "blue")
panel.levelplot(...)
})
m <- matrix(c(0,0,-1,-2,0,0,1,0,1,3,3,3,-3,0,0,2),nrow = 4)
levelplot(m,at=c(-3,-1,0,3),col.regions=c("red","blue","green"), xlab = "", ylab="", colorkey = FALSE, panel = function(...) {
panel.fill(col = "blue")
panel.levelplot(...)
})

最新更新