Pheatmap Color for Specific Value

不羁的心 提交于 2019-12-22 11:37:17

问题


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!


回答1:


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

Should set the breaks to for the specified ranges you mentioned in the question and the 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))

For purpose of showing an example using rnorm.within function I made the following data-set:

   #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;



来源:https://stackoverflow.com/questions/41783245/pheatmap-color-for-specific-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!