- ComplexHeatmap學習筆記①Introduction to ComplexHeatmap package
- ComplexHeatmap學習筆記②Making A Single Heatmap
Making A List of Heatmaps 制作一組熱圖
A list of heatmaps可以優(yōu)化多個數(shù)據(jù)源之間對應關系的可視化糯累。在這個vignette中忙灼,我們將討論making a list of heatmaps 的配置呛每,您可以在[Examples]vignette 以及在ComplexHeatmap paper的[supplementaries]中看到更多真實的例子仅颇。
Heatmap concatenation 合并多個熱圖
你可以從左到右排列多個熱圖贱案。實際上寇仓,一個單獨的熱圖只是長度為1的heatmap list的特殊情況厚棵。
heatmap()
實際上是一類單個heatmap的構造函數(shù)。如果要組合多個heatmap裳朋,用戶可以通過+
運算符將一個heatmap附加到另一個heatmap病线。
library(ComplexHeatmap)
mat1 = matrix(rnorm(80, 2), 8, 10)
mat1 = rbind(mat1, matrix(rnorm(40, -2), 4, 10))
rownames(mat1) = paste0("R", 1:12)
colnames(mat1) = paste0("C", 1:10)
mat2 = matrix(rnorm(60, 2), 6, 10)
mat2 = rbind(mat2, matrix(rnorm(60, -2), 6, 10))
rownames(mat2) = paste0("R", 1:12)
colnames(mat2) = paste0("C", 1:10)
ht1 = Heatmap(mat1, name = "ht1")
ht2 = Heatmap(mat2, name = "ht2")
class(ht1)
## [1] "Heatmap"
## attr(,"package")
## [1] "ComplexHeatmap"
class(ht2)
## [1] "Heatmap"
## attr(,"package")
## [1] "ComplexHeatmap"
ht1 + ht2
在默認模式下,第二個heatmap中的樹形圖將被刪除鲤嫡,行順序也將與第一個相同.
兩個heatmap相加的返回值是一個heatmaplist
對象送挑。直接調用ht_list
對象將調用draw()
方法的默認設置。通過顯式調用draw()
方法暖眼,您可以擁有更多的控件惕耕,例如圖例和標題。
ht_list = ht1 + ht2
class(ht_list)
## [1] "HeatmapList"
## attr(,"package")
## [1] "ComplexHeatmap"
您可以在heatmap list中附加任意數(shù)量的heatmap诫肠。還可以將heatmap list 附加到heatmap list司澎。
ht1 + ht1 + ht1
ht1 + ht_list
ht_list + ht1
ht_list + ht_list
NULL
可以被賦值給heatmaplist中欺缘。當用戶想通過for
循環(huán)構造一個heatmap list時,它將是很方便的挤安。
ht_list = NULL
for(s in sth) {
ht_list = ht_list + Heatmap(...)
}
Titles 標題
A heatmap list還具有獨立于熱圖標題的標題谚殊。--單個heatmap有單獨的標題,組合起來的heatmap list也可以弄個heatmap list標題
ht1 = Heatmap(mat1, name = "ht1", row_title = "Heatmap 1", column_title = "Heatmap 1")
ht2 = Heatmap(mat2, name = "ht2", row_title = "Heatmap 2", column_title = "Heatmap 2")
ht_list = ht1 + ht2
draw(ht_list, row_title = "Two heatmaps, row title", row_title_gp = gpar(col = "red"),
column_title = "Two heatmaps, column title", column_title_side = "bottom")
Gaps between heatmaps 熱圖間的間隔
熱圖間的間隔可以通過 gap
參數(shù)配合unit
對象來設置.
draw(ht_list, gap = unit(1, "cm"))
draw(ht_list + ht_list, gap = unit(c(3, 6, 9, 0), "mm"))
## Warning in .local(object, ...): Heatmap/row annotation names are duplicated: ht1, ht2
Size of heatmaps 熱圖的大小設置
一些(不是所有)熱圖的寬度可以設置為固定的寬度蛤铜。
ht1 = Heatmap(mat1, name = "ht1", column_title = "Heatmap 1")
ht2 = Heatmap(mat2, name = "ht2", column_title = "Heatmap 2", width = unit(5, "cm"))
ht1 + ht2
or寬度可以設置為相對值嫩絮。 Please not in this case, width
for all heatmaps should be set (relative width and fixed width can be mixed)
ht1 = Heatmap(mat1, name = "ht1", column_title = "Heatmap 1", width = 2)
ht2 = Heatmap(mat2, name = "ht2", column_title = "Heatmap 2", width = 1)
ht1 + ht2
Auto adjustment 自動調整
如果繪制了多個熱圖,則會有一些自動調整围肥。應該有一個主熱圖剿干,默認情況下是第一個熱圖。在剩余熱圖中的一些設置將會被主熱圖的設置所修改 The adjustment are:
- 剩余熱圖的行聚類被移除.
- 剩余熱圖的行標題被移除.
- 如果主熱圖被按行分隔穆刻,其他剩余熱圖也將被拆分置尔,拆分樣式和主熱圖一樣.
主熱圖可以通過“main_heatmap”參數(shù)指定。它的值可以是 a numeric index或熱圖的名稱(當然氢伟,在創(chuàng)建“熱圖”對象時需要設置熱圖名稱).
ht1 = Heatmap(mat1, name = "ht1", column_title = "Heatmap 1", km = 2)
ht2 = Heatmap(mat2, name = "ht2", column_title = "Heatmap 2")
ht1 + ht2
# note we changed the order of `ht1` and `ht2`
draw(ht2 + ht1)
# here although `ht1` is the second heatmap, we specify `ht1` to be
# the main heatmap by explicitely setting `main_heatmap` argument
draw(ht2 + ht1, main_heatmap = "ht1")
#用main_heatmap參數(shù)設定主熱圖
如果主熱圖中沒有行聚類榜轿,則所有其他熱圖也沒有行聚類
ht1 = Heatmap(mat1, name = "ht1", column_title = "Heatmap 1", cluster_rows = FALSE)
ht2 = Heatmap(mat2, name = "ht2", column_title = "Heatmap 2")
ht1 + ht2
Change graphic parameters simultaneously 同時更改圖形參數(shù)
ht_global_opt()
can set graphic parameters for dimension names and titles as global settings.ht_global_opt()
可以為維名稱和標題設置圖形參數(shù),將其設置為全局設置。
ht_global_opt(heatmap_row_names_gp = gpar(fontface = "italic"),
heatmap_column_names_gp = gpar(fontsize = 14))
ht1 = Heatmap(mat1, name = "ht1", column_title = "Heatmap 1")
ht2 = Heatmap(mat2, name = "ht2", column_title = "Heatmap 2")
ht1 + ht2
ht_global_opt(RESET = TRUE)
以下是“ht_global_opt()”支持的全局設置。通過這個函數(shù)闷畸,您還可以控制圖例的設置.
names(ht_global_opt())
## [1] "heatmap_row_names_gp" "heatmap_column_names_gp"
## [3] "heatmap_row_title_gp" "heatmap_column_title_gp"
## [5] "heatmap_legend_title_gp" "heatmap_legend_title_position"
## [7] "heatmap_legend_labels_gp" "heatmap_legend_grid_height"
## [9] "heatmap_legend_grid_width" "heatmap_legend_grid_border"
## [11] "annotation_legend_title_gp" "annotation_legend_title_position"
## [13] "annotation_legend_labels_gp" "annotation_legend_grid_height"
## [15] "annotation_legend_grid_width" "annotation_legend_grid_border"
## [17] "fast_hclust"
Retrieve orders and dendrograms 檢索順序和樹狀圖
row_order
, column_order
, row_dend
and column_dend
可以被用作從熱圖中檢索相應的信息. 用法可以直接從下方的例子中學習:
ht_list = ht1 + ht2
row_order(ht_list)
## [[1]]
## [1] 8 3 4 1 5 7 2 6 9 11 10 12
column_order(ht_list)
## $ht1
## [1] 5 1 3 2 7 9 6 10 8 4
##
## $ht2
## [1] 9 4 6 7 8 1 5 10 3 2
row_dend(ht_list)
## [[1]]
## 'dendrogram' with 2 branches and 12 members total, at height 16.14288
column_dend(ht_list)
## $ht1
## 'dendrogram' with 2 branches and 10 members total, at height 8.069474
##
## $ht2
## 'dendrogram' with 2 branches and 10 members total, at height 6.883646
如果尚未繪制“ht_list”茶敏,那么如果矩陣很大,調用這四個函數(shù)會有點慢。但是,如果已經(jīng)繪制了“ht_list”,這意味著已經(jīng)將聚類應用于矩陣助析,那么檢索這些信息將非常快椅您。
ht_list = draw(ht1 + ht2)
row_order(ht_list)
column_order(ht_list)
row_dend(ht_list)
column_dend(ht_list)
Heatmap list with row annotations 帶有行注釋的熱圖列表(Heatmap list)
行注釋可以添加到 heatmap list中, 查看 [Heatmap Annotation] 獲取更多的解釋.
Modify row orders/clustering in main heatmap 修改主熱圖中的行順序/聚類
從版本1.11.1, 主熱圖的行順序/聚類設置可以直接在"draw()"函數(shù)中設置外冀。這使得切換主熱圖非常方便,無需單獨修改熱圖中的設置掀泳。實際上雪隧,在'draw()'中指定的設置將覆蓋主熱圖中的相應設置。
split = rep(c("a", "b"), each = 6)
ht_list = Heatmap(mat1, name = "mat1", cluster_rows = FALSE, column_title = "mat1") +
Heatmap(mat2, name = "mat2", cluster_rows = FALSE, column_title = "mat2")
draw(ht_list, main_heatmap = "mat1", split = split)
draw(ht_list, main_heatmap = "mat2", km = 2, cluster_rows = TRUE)
draw(ht_list, cluster_rows = TRUE, main_heatmap = "mat1", show_row_dend =TRUE)
draw(ht_list, cluster_rows = TRUE, main_heatmap = "mat2", show_row_dend =TRUE)
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] dendextend_1.9.0 dendsort_0.3.3 cluster_2.0.7-1 IRanges_2.16.0
## [5] S4Vectors_0.20.0 BiocGenerics_0.28.0 HilbertCurve_1.12.0 circlize_0.4.4
## [9] 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 GetoptLong_0.1.7 stringr_1.3.1
## [25] RCurl_1.95-4.11 munsell_0.5.0 compiler_3.5.1 pkgconfig_2.0.2
## [29] shape_1.4.4 nnet_7.3-12 tidyselect_0.2.5 gridExtra_2.3
## [33] tibble_1.4.2 GenomeInfoDbData_1.2.0 viridisLite_0.3.0 crayon_1.3.4
## [37] dplyr_0.7.7 MASS_7.3-51 bitops_1.0-6 gtable_0.2.0
## [41] magrittr_1.5 scales_1.0.0 stringi_1.2.4 XVector_0.22.0
## [45] viridis_0.5.1 flexmix_2.3-14 bindrcpp_0.2.2 robustbase_0.93-3
## [49] fastcluster_1.1.25 HilbertVis_1.40.0 rjson_0.2.20 RColorBrewer_1.1-2
## [53] tools_3.5.1 fpc_2.1-11.1 glue_1.3.0 trimcluster_0.1-2.1
## [57] DEoptimR_1.0-8 purrr_0.2.5 colorspace_1.3-2 GenomicRanges_1.34.0
## [61] prabclus_2.2-6 bindr_0.1.1 modeltools_0.2-22