具有自定义颜色比例尺的热图,用于低于和高于阈值的值

时间:2017-06-06 19:29:37

标签: r plot heatmap colorbar pheatmap

我想在R中使用pheatmap制作热图,颜色为绿色,黑色和红色,并使用-2到2的图例中的范围,这是我使用的代码:

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,而我的解决方案这些值是白色的,你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

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

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 功能,我制作了以下数据集:

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

并应用上述程序(相同的折断和颜色托盘)制作热图将获得:

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