首页 > 解决方案 > 如何在 ComplexHeatmap 的连续色标中创建颜色中断?

问题描述

我想使用 ComplexHeatmap 包创建一个热图,其中我有一个连续的颜色比例到某个阈值,并且对于高于阈值的值有一个不同的颜色。我尝试用最大值替换阈值以上的值。这给了我想要的热图结果。但是,在这种情况下,我的色标图例不再准确。有没有办法为复杂的热图创建一个颜色中断,比如为 heatmap.2 做的“breaks()”?此外,我可以在我的图例中包含 na_col 值吗?

为了添加 NA 颜色,我尝试添加一个额外的图例:

draw(myHeatmap)
NA_leg<-Legend(at = c(1), title = "", legend_gp = gpar(fill = "grey"), labels = c("NA"))
draw(NA_leg, x = unit(20, "cm"), y = unit(10, "cm"))

但是,在缩放或导出图像时,它不会保持在正确的位置。有没有办法固定相对于热图的位置?

标签: rheatmap

解决方案


我检查了一下,我在 (-)0.8 和 (-)0.8001 之间没有任何值,并使用 Heatmap() 的 col_fun 参数更改了我的颜色。

col_fun<-colorRamp2(c(-0.8001,-0.8,0,0.8,0.8001), c("darkblue","skyblue","white", "brown1", "darkred"))

Heatmap(matrix, show_row_dend = F, show_column_dend = F, na_col = "grey", cluster_rows = F, cluster_columns = F,
               show_row_names = F, show_column_names = F, cluster_row_slices = F, cluster_column_slices = F,
               heatmap_legend_param = list(title = "legend", at = c(-1,-.5, 0, 0.5, 1), 
                                           labels = c("<-0.8","" ,"0","", ">0.8")), column_title_gp = gpar(fontsize = 10), row_title_gp = gpar(fontsize = 10), col = col_fun)

对于在图例中包含 NA 的问题,我在绘图函数中使用了“annotation_legend_list”:

myHeatmap<-Heatmap(matrix, col = col_fun, show_heatmap_legend = F, show_row_dend = F, show_column_dend = F, na_col = "grey", cluster_rows = F, cluster_columns = F,
               show_row_names = F, show_column_names = F, cluster_row_slices = F, cluster_column_slices = F,
                column_title_gp = gpar(fontsize = 10), row_title_gp = gpar(fontsize = 10))

#create legend
main_leg<-Legend(at = c(-1,-.5, 0, 0.5, 1), col_fun =  col_fun, title = "my_title",
                   labels = c(expression("" <= -0.8),"" ,"0","", expression("">=0.8)), title_position = "leftcenter-rot")
NA_leg<-Legend(at = c(1), title = " ", legend_gp = gpar(fill = "grey"), labels = c("NA"), title_position = "leftcenter-rot")

draw(myHeatmap, column_title = "my Title", annotation_legend_list = list(main_leg, NA_leg))

推荐阅读