You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ComplexHeatmap自定义图例值不生效,为何显示自动生成的极值?

问题

手动设置ComplexHeatmap的颜色映射断点(colorRamp2(c(-11,-5,2), c("blue", "white", "red")))后,热图图例仍自动显示数据的最小值、中位数和最大值,自定义的数值未被展示。

原因分析

ComplexHeatmap默认会根据输入数据的实际数值范围自动生成图例刻度标签,不会直接沿用colorRamp2中定义的断点。即使指定了颜色映射区间,图例组件仍会基于数据分布自动调整显示刻度,导致自定义断点不生效。

解决方案

在heatmap_legend_param参数中手动指定at(刻度位置)和labels(刻度文本),强制图例显示自定义数值。修改后的代码如下:

col_fun = colorRamp2(c(-11,-5,2), c("blue", "white", "red"))

Heatmap <- Heatmap(t(as.matrix(df)),
                   name="Abundance (log2)",
                   left_annotation = ha,
                   show_column_dend = FALSE,
                   show_row_dend = FALSE,
                   column_names_gp = grid::gpar(fontsize = 8),
                   row_names_gp = grid::gpar(fontsize = 8),
                   col=col_fun,
                   heatmap_legend_param = list(
                     title = "Abundance (log2)",
                     legend_height = unit(1.5, "cm"),
                     title_position = "topcenter",
                     title_gp = gpar(fontsize = 6),
                     labels_gp = gpar(fontsize = 6),
                     direction = "horizontal",
                     labels_rot = 0,
                     at = c(-11, -5, 2),  # 指定显示的刻度位置
                     labels = c("-11", "-5", "2")  # 对应刻度的文本
                   )
)
补充说明
  • at的取值必须和colorRamp2中定义的断点完全一致,才能保证图例颜色刻度与颜色映射对应。
  • 如果需要显示更多中间刻度,可在at和labels中添加对应数值,比如at = c(-11, -8, -5, -2, 2),确保颜色映射覆盖这些数值即可。

内容的提问来源于stack exchange,提问作者user2300940

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 07:15:51