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

在R的ComplexHeatmap中为分块热图注释配置不同调色板及相关问题

主问题:为分切片热图的指定注释分配切片专属调色板

需要将矩阵mat生成的热图划分为3个切片,为每个切片的foo注释分配对应调色板(切片1用绿色系、切片2红色系、切片3黄色系)。尝试使用decorate_annotation方法未成功,相关代码如下:

library(ComplexHeatmap)
set.seed(123)
mat = matrix(runif(100, 0, 10), 10)
rownames(mat) = paste0("R", 1:10)
colnames(mat) = paste0("C", 1:10)

library(circlize)
col_funa = colorRamp2(c(-3, 0, 3), c("pink", "white", "purple"))
col_funb = colorRamp2(c(-3, 0, 3), c("yellow", "white", "green"))

ha = HeatmapAnnotation(
foo = cbind(a = rnorm(10), b = rnorm(10)), 
foo2 = cbind(a = rnorm(10), b = rnorm(10)),
col = list(foo = col_funa, foo2 = col_funb)
)

Heatmap(mat, name = "mat", top_annotation = ha, 
    column_dend_height = unit(2, "cm"), 
    row_dend_width = unit(2, "cm"), 
    column_split = c(rep(1, 3),rep(2, 3),rep(3, 4)))

后续尝试的代码:

col_fun1 = colorRamp2(c(-3, 0, 3), c("blue", "white", "red"))
col_fun2 = colorRamp2(c(-3, 0, 3), c("pink", "white", "purple"))
col_fun3 = colorRamp2(c(-3, 0, 3), c("yellow", "white", "green"))
col_funL <- list(col_fun1, col_fun2, col_fun3)

for(i in 1:3) {
    decorate_annotation("foo", slice = i, {
        grid.rect(gp = gpar(fill = col_funL[[i]], col = NA))
    })
}

跟进问题1:大数据量使用layer_fun报错

适配大数据量时改用layer_fun出现报错,代码及错误信息如下:

hta = Heatmap(rbind(value), rect_gp = gpar(type = "none"),
    layer_fun = function(j, i, x, y, w, h, fill) {
        if(pindex(rbind(foo), i, j) == "a") {
            grid.rect(x, y, w, h, gp = gpar(fill = col_fun_a(pindex(rbind(value), i, j)), col = NA))
        } else if(pindex(rbind(foo), i, j) == "b") {
            grid.rect(x, y, w, h, gp = gpar(fill = col_fun_b(pindex(rbind(value), i, j)), col = NA))
        } else {
            grid.rect(x, y, w, h, gp = gpar(fill = col_fun_c(pindex(rbind(value), i, j)), col = NA))
        }
    }, show_heatmap_legend = FALSE)

错误信息:

Error in if (pindex(rbind(foo), i, j) == "a") { : 
  the condition has length > 1

跟进问题2:垂直拼接热图保持列顺序不变

使用%v%垂直拼接注释热图与主体热图时,希望保持主体热图列顺序不变,询问是否可实现及方法。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 21:12:09