R levelplot中的3个不同颜色区域

R levelplot中的3个不同颜色区域,r,levelplot,R,Levelplot,尝试绘制此矩阵,使负值为红色,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") pane

尝试绘制此矩阵,使负值为红色,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(...)
})

m
m这里有一种使用布尔比较的通用方法:

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(...)
})
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(...)
})