- ComplexHeatmap學習筆記①Introduction to ComplexHeatmap package
- ComplexHeatmap學習筆記②Making A Single Heatmap
- ComplexHeatmap學習筆記③Making A List of Heatmaps
- ComplexHeatmap學習筆記④Heatmap Annotations
- ComplexHeatmap學習筆記⑤Heatmap and Annotation Legends
Heatmap Decoration 熱圖裝飾
熱圖/熱圖列表(heatmap/heatmap list)的每個組件都有一個名稱(唯一ID)泥栖。 您可以通過指定熱圖/注釋名稱向任何viewport添加圖形腾么。.
我們先生成一個幾乎包含所有類型的熱圖組件的圖形.
library(ComplexHeatmap)
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)))
ht1 = Heatmap(mat, name = "ht1", km = 2, row_title = "Heatmap 1", column_title = "Heatmap 1",
top_annotation = ha_column1)
ha_column2 = HeatmapAnnotation(df = data.frame(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)
The components (viewports) that have names are:
-
global
: 這個viewport控制所有figure. -
global_column_title
: 這個viewport 控制熱圖列表的列名. -
global_row_title
: 這個viewport 控制熱圖列表的行名. -
main_heatmap_list
: 這個viewport控制a list of heatmaps and row annotations. -
heatmap_@{heatmap_name}
: 這個viewport控制a single heatmap -
annotation_@{annotation_name}
: 這個viewport控制an annotation on columns. -
annotation_@{annotation_name}_@{i}
: for row annotations -
@{heatmap_name}_heatmap_body_@{i}
: the heatmap body. -
@{heatmap_name}_column_title
: column title for a single heatmap. -
@{heatmap_name}_row_title_@{i}
: since a heatmap body may be splitted into several parts.@{i}
is the index of the row slice. -
@{heatmap_name}_dend_row_@{i}
: dendrogram for ith row slice. -
@{heatmap_name}_dend_column
: dendrogram on columns -
@{heatmap_name}_row_names_@{i}
: the viewport which contains row names. -
@{heatmap_name}_column_names
: the viewport which contains column names. -
heatmap_legend
: the viewport which contains all heatmap legends. -
legend_@{heatmap_name}
: the viewport which contains a single heatmap legend. -
annotation_legend
: the viewport which contains all annotation legends. -
legend_@{annotation_name}
: the viewport which contains a single annotation legend.
注:上方部分用英文反而更容易讓人理解西采,所以保留原文英文炎辨。
decorate_* functions decorate_*類函數(shù)
基本上,您可以通過seekViewport()
來是定位這些組件殉疼,但是為了隱藏太低級別的細節(jié)乾胶,ComplexHeatmap包提供了decorate_*
系列函數(shù),可以很容易地將圖形添加到不同的組件中崖瞭。.
下面的代碼添加了注釋名狂巢,在熱圖中標記一個網格(grid ),并用兩個矩形將第一個列的聚類分開.
ht_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list",
heatmap_legend_side = "right", annotation_legend_side = "left")
decorate_annotation("points", {
grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5,
default.units = "npc", just = "right")
})
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
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("ht2", {
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"))
})
由anno_points()
, anno_barplot()
and anno_boxplot()
這些函數(shù)創(chuàng)建的注釋, “native” unit 可以被用在 decoration code中.
Add annotation names 添加注釋名
默認情況下唧领,注釋的名稱不會與熱圖注釋一起繪制。 原因是如果繪制了注釋名稱雌续,它們將位于其他熱圖組件的區(qū)域中斩个,這將使得熱圖布局的調整變得困難。 HeatmapAnnotation()
為添加注釋名稱提供了一個不那么完美的[solution]驯杜,但是受啥,因為你可以通過名稱定位到熱圖列表中的任何組件,實際上手動添加注釋名稱并不困難.
以下代碼在列注釋的兩側添加注釋名稱鸽心。 缺點是因為沒有為注釋名稱設計特定組件滚局,如果注釋名稱太長,它將超過圖形區(qū)域(但這個問題可以通過一些技巧來解決顽频,請參閱[** Examples **]vignette).
df = data.frame(type1 = c(rep("a", 5), rep("b", 5)),
type2 = c(rep("A", 3), rep("B", 7)))
ha = HeatmapAnnotation(df, col = list(type1 = c("a" = "red", "b" = "blue"),
type2 = c("A" = "green", "B" = "orange")))
Heatmap(mat, name = "ht", top_annotation = ha)
for(an in colnames(df)) {
decorate_annotation(an, {
# annotation names on the right
grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
# annotation names on the left
grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
})
}
Visualize distributions 可視化分布
使用熱圖裝飾(heatmap decorations)藤肢,實際上您可以基于熱圖設計新圖形。 以下是一個例子::
為了可視化在矩陣或列表中的列的分布糯景,有時我們使用boxplot or beanplot嘁圈。在這里省骂,我們還可以使用顏色映射到密度值,以可視化每列(或每個列表元素)中的分布最住。有時候它可以給你的數(shù)據(jù)展示出一個明晰可讀的圖像钞澳。
這個包有一個densityHeatmap()
函數(shù),其用法非常簡單:
matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10]
ha = HeatmapAnnotation(df = data.frame(anno = rep(c("A", "B"), each = 5)),
col = list(anno = c("A" = "green", "B" = "orange")),
points = anno_points(runif(10)))
densityHeatmap(matrix, anno = ha)
Session info
sessionInfo()
## R version 3.5.1 Patched (2018-07-24 r75008)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 2012 R2 x64 (build 9600)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=C LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats4 parallel grid stats graphics grDevices utils datasets methods
## [10] base
##
## other attached packages:
## [1] GetoptLong_0.1.7 dendextend_1.9.0 dendsort_0.3.3 cluster_2.0.7-1
## [5] IRanges_2.16.0 S4Vectors_0.20.0 BiocGenerics_0.28.0 HilbertCurve_1.12.0
## [9] circlize_0.4.4 ComplexHeatmap_1.20.0 knitr_1.20 markdown_0.8
##
## loaded via a namespace (and not attached):
## [1] mclust_5.4.1 Rcpp_0.12.19 mvtnorm_1.0-8 lattice_0.20-35
## [5] png_0.1-7 class_7.3-14 assertthat_0.2.0 mime_0.6
## [9] R6_2.3.0 GenomeInfoDb_1.18.0 plyr_1.8.4 evaluate_0.12
## [13] ggplot2_3.1.0 highr_0.7 pillar_1.3.0 GlobalOptions_0.1.0
## [17] zlibbioc_1.28.0 rlang_0.3.0.1 lazyeval_0.2.1 diptest_0.75-7
## [21] kernlab_0.9-27 whisker_0.3-2 stringr_1.3.1 RCurl_1.95-4.11
## [25] munsell_0.5.0 compiler_3.5.1 pkgconfig_2.0.2 shape_1.4.4
## [29] nnet_7.3-12 tidyselect_0.2.5 gridExtra_2.3 tibble_1.4.2
## [33] GenomeInfoDbData_1.2.0 viridisLite_0.3.0 crayon_1.3.4 dplyr_0.7.7
## [37] MASS_7.3-51 bitops_1.0-6 gtable_0.2.0 magrittr_1.5
## [41] scales_1.0.0 stringi_1.2.4 XVector_0.22.0 viridis_0.5.1
## [45] flexmix_2.3-14 bindrcpp_0.2.2 robustbase_0.93-3 fastcluster_1.1.25
## [49] HilbertVis_1.40.0 rjson_0.2.20 RColorBrewer_1.1-2 tools_3.5.1
## [53] fpc_2.1-11.1 glue_1.3.0 trimcluster_0.1-2.1 DEoptimR_1.0-8
## [57] purrr_0.2.5 colorspace_1.3-2 GenomicRanges_1.34.0 prabclus_2.2-6
## [61] bindr_0.1.1 modeltools_0.2-22