rlevelplot

3 different colour regions in levelplot


Trying to plot this matrix so that negative values are red, 0 is blue and positive values are green.

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

Solution

  • Here is one more generic way using boolean comparisons:

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