具有自定义颜色比例尺的热图,适用于阈值上下的值 [英] Heatmap with customized color scale bar for values below and above thresholds

查看:374
本文介绍了具有自定义颜色比例尺的热图,适用于阈值上下的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中使用带有绿色,黑色和红色的pheatmap并在图例中使用从-2到2的范围来制作一个热图,这是我使用的代码:

I would like to make an heatmap in R using pheatmap with the colors green, black and red and using a range in the legend from -2 to 2, here is the code that I used:

library(pheatmap)
my_palette <- colorRampPalette(c("green", "black", "red"))(n = 201)
colors = c(seq(as.numeric(-2),-0.01,length=100), 0, 
seq(0.01,as.numeric(2),length=100))

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

问题是我希望绿色的值小于-2,红色的值大于2,而对于我的解决方案,这些值是白色,您能帮我吗?

The problem is that I would like the values less than -2 in green an greater than 2 in red while with my solution these values are white, could you help me?

推荐答案

您需要将断点设置为-2和+2,并在两者之间进行渐变.您还需要将颜色设置为小于最小期望值和大于最大期望值.看下面;

You need to set the break-point to be at -2 and +2 and make a gradient in between. You also need to set the color for less than minimum and greater than maximum desired values. Look below;

library(pheatmap)

colors <- c(min(mFilt_annot_sort_matrix),seq(-2,2,by=0.01),max(mFilt_annot_sort_matrix))

my_palette <- c("green",colorRampPalette(colors = c("green", "black", "red"))
                                                   (n = length(colors)-3), "red")


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

使用普通数据的示例;

使用 rnorm.within >函数,我进行了以下数据设置:

Example using a normal data;

Using rnorm.within function I make the following data-set:

#V1 is random between -4 and 4 
#V2 is less than -2
#V3 is greater than 2

df <- data.frame(cbind(rnorm.within(1000, -4, 4)
,rnorm.within(1000,-4,-3), rnorm.within(1000,3,4))) 

并应用上述过程(相同的间隔和颜色托盘)来制作热图,将会得到:

and applying the procedure above (same breaks and color pallet) for making a heatmap will get:

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



   

这篇关于具有自定义颜色比例尺的热图,适用于阈值上下的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆