rmatrixplotheatmapgplots

How to plot different and independent color gradient legend in heatmap.2 (R)


I obtain a matrix from calculation where the values are between 0 and 4. I use the function heatmap.2 to plot.

The idea is to have a color code and with different gradient in function of the cell value:

from 0 to 0.99: gradient of red

from 1 to 1.99: gradient of yellow

from 2 to 2.99: gradient of green

from 3 to 4: gradient of grey

As you can see, the gradients are not independent, the colors smoothly change from a range to another (by example, I got orange from red to yellow). How to obtain 4 different and independent gradient? Mean, from 0 to 0.99 only red gradient and from 1 to 1.99 only yellow gradient etc

Here the code I used (I create a random matrix for the example):

library(gplots)

# create a matrix of random values from [0, 4]
random.matrix  <- matrix(runif(100, min = 0, max = 4), nrow = 10)

quantile.range <- quantile(random.matrix, probs = seq(0, 1))
palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
color.palette  <- colorRampPalette(c("red", "yellow", "green", "grey"))(length(palette.breaks)-1)
heatmap.2(
  random.matrix,
  dendrogram = "none",
  scale      = "none",
  trace      = "none",
  symbreaks=FALSE,
  symkey=FALSE, 
  key        = TRUE,
  key.title = 'class information',
  key.xlab = 'class number',
  keysize = 2,
  #( "bottom.margin", "left.margin", "top.margin", "right.margin" )
  key.par=list(mar=c(3,3.5,2,0)),
  labRow     = NA,
  labCol     = NA,
  col    = color.palette,
  breaks = palette.breaks,
)
legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))

Here the plot:

enter image description here


Solution

  • I found this way:

    # gplots contains the heatmap.2 function
    library(gplots)
    
    # create a matrix of random values from [0, 4]
    random.matrix  <- matrix(runif(100, min = 0, max = 4), nrow = 10)
    
    quantile.range <- quantile(random.matrix, probs = seq(0, 1))
    palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
    
    colfunc1 <- colorRampPalette(c("white", "red"))
    colfunc2 <- colorRampPalette(c("white", "yellow"))
    colfunc3 <- colorRampPalette(c("white", "green"))
    colfunc4 <- colorRampPalette(c("grey", "black"))
    
    color.palette  <- colorRampPalette(c(colfunc1(4), colfunc2(4), colfunc3(4), colfunc4(3)))(length(palette.breaks)-1)
    
    heatmap.2(
      random.matrix,
      dendrogram = "none",
      scale      = "none",
      trace      = "none",
      symbreaks=FALSE,
      symkey=FALSE, 
      key        = TRUE,
      key.title = 'class information',
      key.xlab = 'class number',
      keysize = 2,
      #( "bottom.margin", "left.margin", "top.margin", "right.margin" )
      key.par=list(mar=c(3,3.5,2,0)),
      labRow     = NA,
      labCol     = NA,
      col    = color.palette,
      breaks = palette.breaks,
    )
    legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))
    

    enter image description here