ComplexHeatmap复杂热图绘制学习——6.热图装饰

热图装饰

在制作热图后,每个热图组件的绘图区域仍然保留,因此如何回到原始位置进而添加更多图形呢?看如下示例。
首先生成一个几乎包含所有类型的热图组件的图。list_components()列出热图/注释组件的名称(或视口的名称)。

set.seed(123)
mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)

ha_column1 = HeatmapAnnotation(points = anno_points(rnorm(10)), 
    annotation_name_side = "left")
ht1 = Heatmap(mat, name = "ht1", km = 2, column_title = "Heatmap 1", 
    top_annotation = ha_column1, row_names_side = "left")

ha_column2 = HeatmapAnnotation(type = c(rep("a", 5), rep("b", 5)),
    col = list(type = c("a" = "red", "b" = "blue")))
ht2 = Heatmap(mat, name = "ht2", row_title = "Heatmap 2", column_title = "Heatmap 2",
    bottom_annotation = ha_column2, column_km = 2)

ht_list = ht1 + ht2 + 
    rowAnnotation(bar = anno_barplot(rowMeans(mat), width = unit(2, "cm")))
draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list")
list_components()
##  [1] "ROOT"                           "global"                        
##  [3] "global_layout"                  "global-heatmaplist"            
##  [5] "main_heatmap_list"              "heatmap_ht1"                   
##  [7] "ht1_heatmap_body_wrap"          "ht1_heatmap_body_1_1"          
##  [9] "ht1_heatmap_body_2_1"           "ht1_column_title_1"            
## [11] "ht1_row_title_1"                "ht1_row_title_2"               
## [13] "ht1_dend_row_1"                 "ht1_dend_row_2"                
## [15] "ht1_dend_column_1"              "ht1_row_names_1"               
## [17] "ht1_row_names_2"                "ht1_column_names_1"            
## [19] "annotation_points_1"            "heatmap_ht2"                   
## [21] "ht2_heatmap_body_wrap"          "ht2_heatmap_body_1_1"          
## [23] "ht2_heatmap_body_1_2"           "ht2_heatmap_body_2_1"          
## [25] "ht2_heatmap_body_2_2"           "ht2_column_title_1"            
## [27] "ht2_dend_column_1"              "ht2_dend_column_2"             
## [29] "ht2_column_names_1"             "ht2_column_names_2"            
## [31] "annotation_type_1"              "annotation_type_2"             
## [33] "heatmap_heatmap_annotation_2"   "annotation_bar_1"              
## [35] "annotation_bar_2"               "global-column_title_top"       
## [37] "global_column_title"            "global-row_title_left"         
## [39] "global_row_title"               "global-heatmap_legend_right"   
## [41] "heatmap_legend"                 "global-annotation_legend_right"
## [43] "annotation_legend"
image

基本上可以通过decorate_*() 函数重新访问上图中的红色区域。

6.1 装饰函数

由于可以通过list_components()获取视窗名称,因此实际上可以通过seekViewport()直接进入视窗。为了摆脱复杂的视窗名称,这些decorate_*()函数提供了一种更友好的方法。

ComplexHeatmap包中有以下装饰函数:

  • decorate_heatmap_body()
  • decorate_annotation()
  • decorate_dend()
  • decorate_title()
  • decorate_dimnames()
  • decorate_row_names(),等同于decorate_dimnames(..., which = "row")
  • decorate_column_names(),等同于decorate_dimnames(..., which = "column")
  • decorate_row_dend(),等同于decorate_dend(..., which = "row")
  • decorate_column_dend(),等同于decorate_dend(..., which = "column")
  • decorate_row_title(),等同于decorate_title(..., which = "row")
  • decorate_column_title(),等同于decorate_title(..., which = "column")

其中,decorate_heatmap_body()decorate_annotation()经常使用。

对于所有这些功能,它们需要一个热图或注释名称、行/列切片的索引(如果热图被拆分)以及定义如何添加图形的代码块。查看以下示例。

ht_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", 
    heatmap_legend_side = "right", annotation_legend_side = "left")

decorate_heatmap_body("ht1", {
    grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc")
    grid.lines(c(0.5, 0.5), c(0, 1), gp = gpar(lty = 2, lwd = 2))
}, slice = 2)

decorate_column_dend("ht1", {
    tree = column_dend(ht_list)$ht1[[1]]
    ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)]

    first_index = function(l) which(l)[1]
    last_index = function(l) { x = which(l); x[length(x)] }
    x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1
    x2 = c(last_index(ind == 1), last_index(ind == 2))
    grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left",
        default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA))
})

decorate_row_names("ht1", {
    grid.rect(gp = gpar(fill = "#FF000040"))
}, slice = 2)

decorate_row_title("ht1", {
    grid.rect(gp = gpar(fill = "#00FF0040"))
}, slice = 1)

decorate_annotation("points", {
    grid.lines(c(0, 1), unit(c(0, 0), "native"), gp = gpar(col = "red"))
})
image

对于由anno_points()anno_barplot()所创建的注解,anno_boxplot()可以在装饰代码中使用“原生”单元。

6.2 例子

6.2.1 单列热图的条形图

在前面章节中,我们介绍了添加条形图作为单列热图的注释。在这种情况下,热图包含离散值,其中条形图显示每个级别的频率。在以下示例中,我们展示了使用条形图作为注释但用于连续热图的另一种场景。

想象一下,我们正在分析一组基因组区域(例如差异甲基化区域,DMR),并且我们有一个单列热图,显示与假设基因的重叠(通过基因覆盖的 DMR 的分数来衡量,值介于 0 和 1 之间,例如,0.5 的值意味着该 DMR 的 50% 与基因重叠)。如果我们将 DMR 的宽度表示为w和分数记为p,在分数热图之上,我们想添加条形图来显示平均有多少 DMR 被基因覆盖。在这种情况下,我们需要计算由 DMR 的宽度加权的平均分数 ( ∑(w⋅p)/∑w ).
在下面的代码中,我们随机生成了一个分数向量并将其分成两组。我们首先使用anno_empty()在热图顶部分配空白绘图区域,然后使用decorate_annotation()将条形图添加到其中。

library(circlize)
# DMRs
bed = generateRandomBed(nr = 1000)
# fractions
frac = c(runif(400, min = 0.4, max = 1), runif(nrow(bed) - 400, min = 0.2, max = 0.6))
col_fun = colorRamp2(c(0, 1), c("white", "orange"))
# two groups
split = c(rep("group1", 400), rep("group2", nrow(bed) - 400))
# draw the fraction heatmap with an empty annotation
ht = Heatmap(frac, name = "fraction", col = col_fun, width = unit(2, "cm"),
    top_annotation = HeatmapAnnotation(barplot = anno_empty(height = unit(4, "cm"))))
ht = draw(ht, row_split = split)
# get the row indices in the two row-groups
ro = row_order(ht)
w = bed[, 3] - bed[, 2]
# the mean weighted fraction in the two groups
p = sapply(ro, function(index) {
    sum(w[index]*frac[index])/sum(w[index])
})
# add two bars of `p`
decorate_annotation("barplot", {
    pushViewport(viewport(xscale = c(0.5, 2.5), yscale = c(0, max(p)*1.1)))
    grid.rect(x = 1:2, y = 0, width = 0.8, height = p, just = "bottom",
        gp = gpar(fill = "orange"), default.units = "native")
    grid.yaxis()
    grid.text("mean fraction", x = unit(-1.5, "cm"),rot = 90, just = "bottom")
    popViewport()
})
image

6.2.2 行注释标题

行注释可以连接到热图列表。有时我们需要行注释的标题。通过装饰很容易实现它。

ht_list = Heatmap(matrix(rnorm(100), 10), name = "mat", show_column_dend = FALSE) +
    rowAnnotation(foo = anno_barplot(1:10, width = unit(4, "cm"))) +
    rowAnnotation(bar = anno_boxplot(matrix(rnorm(100), 10)), width = unit(4, "cm"))
draw(ht_list, padding = unit(c(2, 2, 10, 2), "mm")) # add space for titles
decorate_annotation("foo", { 
    grid.text("title for barplot", y = unit(1, "npc") + unit(2, "mm"), just = "bottom") 
})
decorate_annotation("bar", { 
    grid.text("title for boxplot", y = unit(1, "npc") + unit(2, "mm"), just = "bottom") 
})
decorate_heatmap_body("mat", {
    grid.text("title for the heatmap", y = unit(1, "npc") + unit(2, "mm"), just = "bottom")
})
image

你可能感兴趣的:(ComplexHeatmap复杂热图绘制学习——6.热图装饰)