jokergoo / ComplexHeatmap

Make Complex Heatmaps
https://jokergoo.github.io/ComplexHeatmap-reference/book/
Other
1.31k stars 228 forks source link

Heatmap with split tiles #1178

Closed saketkc closed 5 months ago

saketkc commented 6 months ago

Hi @jokergoo, thank you for developing and maintaining the wonderful package!

Is it possible to split the individual tiles of ComplexHeatmap and have them take different colors? Something on the lines of this figure

Yunuuuu commented 5 months ago

You can use layer_fun or cell_fun https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html?q=layer#layer-fun

jokergoo commented 5 months ago

Thanks @Yunuuuu for the answer!

@saketkc I will give you an example:

library(circlize)
library(ComplexHeatmap)

m1 = matrix(runif(100), 10)
m2 = matrix(runif(100), 10)

col_fun1 = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
col_fun2 = colorRamp2(c(0, 0.5, 1), c("green", "white", "orange"))

# here the clustering is from `m1`
ht = Heatmap(m1, rect_gp = gpar(type = "none"), 
    cell_fun = function(j, i, x, y, w, h, f) {
        # draw graphics in each cell
        # upper triangle
        grid.polygon(unit.c(x - w*0.5, x - w*0.5, x + w*0.5),
                     unit.c(y - h*0.5, y + h*0.5, y + h*0.5),
                     gp = gpar(col = "black", fill = col_fun1(m1[i, j])))
        # lower triangle
        grid.polygon(unit.c(x - w*0.5, x + w*0.5, x + w*0.5),
                     unit.c(y - h*0.5, y - h*0.5, y + h*0.5),
                     gp = gpar(col = "black", fill = col_fun2(m2[i, j])))
    }, show_heatmap_legend = FALSE)

# you should also manually generate two legends
lgd = packLegend(
    Legend(title = "m1", col_fun = col_fun1),
    Legend(title = "m2", col_fun = col_fun2)
)

draw(ht, heatmap_legend_list = lgd)
image
saketkc commented 5 months ago

Wonderful, thanks @jokergoo and @Yunuuuu - exactly what I was looking for!