Search code examples
rcolorspheatmap

Coloring a specific cell in pheatmap graph


I'm trying to color arbitrary cells cells in a diagram created with the R package pheatmap.

Short example:

library (pheatmap)

dat <- as.data.frame (scale (mtcars))

pheatmap (dat,
          color = colorRampPalette (c ("white", "red")) (5),
          cluster_rows = FALSE,
          cluster_cols = FALSE,
)

results in this:

enter image description here

What I would like is to color specific cells in arbitrary colors, so I get something like this:

enter image description here

Any ideas how I could achieve that?


Solution

  • You can set to NA the cells that you need to color differently. Then, using the na_col option of pheatmap, you can set the color for these cells.

    library (pheatmap)
    dat <- as.data.frame(scale(mtcars))
    
    # Set to NA the cells 
    dat[, 10] <- NA
    dat[10, 6] <- NA
    dat[16, 4:6] <- NA
    
    pheatmap(dat,
              color = colorRampPalette (c ("white", "red")) (5),
              cluster_rows = FALSE,
              cluster_cols = FALSE,
              na_col = "blue"
    )
    

    enter image description here