2

I am very new to R and recently I have been playing around with the pheatmap library to generate, well, heatmaps. My problem is that I want to color my heatmap in a specific way. I'll describe below:

  1. Values < 1 should be a color ramp (e.g. dark blue to light blue)
  2. A value exactly equal to 1 should be dark grey
  3. Values > 1 should be a color ramp (e.g. dark red to light red)

I have played around with the breaks parameter and the color parameter with various palettes but I can't seem to nail a good solution. the closest I've come is something like this:

pheatmap(mtx, 
     color =  c('#4444FF','#F4FF4F','#FF4444'), 
     breaks = c(0,1,2,3), 
     legend_breaks = c(0,1,2) )

But this doesn't allow for visualization of the ranges, i.e. 0.1 should look a different shade than 0.9 even though they should both be blue. Can anyone provide suggestions or advice? I did look at This ticket and consider changing 1 to NA, but it's a bit too complex for me. Not to mention I'd have to turn off clustering for pheatmap which is not something I wish to do. Thanks!

M--
  • 25,431
  • 8
  • 61
  • 93
Dave
  • 329
  • 1
  • 5
  • 16
  • In case someone ran into this question with slightly different needs, refer to this question: [Heatmap with customized color scale bar for values below and above thresholds](https://stackoverflow.com/questions/44398360/heatmap-with-customized-color-scale-bar-for-values-below-and-above-thresholds). – M-- Jun 12 '17 at 18:41

1 Answers1

2

You cannot set the color for a specific value but you can go for a very small range like [0.999,1.001]

You should set the breaks for the ranges you mentioned in the question and then assign the colors accordingly;

library(pheatmap)
bk1 <- c(seq(-2,0.9,by=0.1),0.999)
bk1 <- c(1.001,seq(1.1,3,by=0.1))
bk <- c(bk1,bk2)  #combine the break limits for purpose of graphing

my_palette <- c(colorRampPalette(colors = c("darkblue", "lightblue"))(n = length(bk1)-1),
              "gray38", "gray38",
              c(colorRampPalette(colors = c("darkred", "tomato1"))(n = length(bk2)-1)))

pheatmap(df, color = my_palette, breaks = bk, scale = "none", 
             cluster_rows = F, cluster_cols = F, margin = c(5,5))

I made an example dataset using rnorm.within:

   #V1 is random between -2 and 3 
   #V2 is equal to 1
   #V3 is random between 0 and 3

   df <- cbind(rnorm.within(1000, -2, 3),rep(1,1000), rnorm.within(1000,0,3))

And this will be the heatmap for the data created above;

M--
  • 25,431
  • 8
  • 61
  • 93