超详细的R语言热图之complexheatmap系列5
我们继续学习complexheatmap
包的内容,这是本系列的第5篇文章。本系列内容非常多,将通过多篇推文逐渐介绍,欢迎大家关注我的公众号:医学和生信笔记。
本篇内容主要介绍热图的拼图的各种设置!希望大家都能学会,一个高大上的热图怎么少得了复杂的元素堆砌呢?
complexheatmap
系列第1篇:超详细的R语言热图之complexheatmap系列1
complexheatmap
系列第2篇:超详细的R语言热图之complexheatmap系列2
complexheatmap
系列第3篇:超详细的R语言热图之complexheatmap系列3
complexheatmap
系列第4篇:超详细的R语言热图之complexheatmap系列4
第四章 多个热图
ComplexHeatmap
包的一大特色就是可以同时绘制多个热图,本章主要介绍水平排列的热图,垂直排列的热图原理与此类似,会在后面简要介绍。水平排列的热图要求具有相同的行。单列热图有时在将可以显示的热图列表连接起来时很有用,例如,如果行是基因,则基因的类型(即是否编码蛋白质)可以表示为一列字符矩阵,而差异表达分析的p值或倍数变化可以表示为一列数字矩阵,并连接到主热图。
下面是一个小例子:
library(ComplexHeatmap)
## 载入需要的程辑包:grid
## ========================================
## ComplexHeatmap version 2.8.0
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
##
## If you use it in published research, please cite:
## Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional
## genomic data. Bioinformatics 2016.
##
## The new InteractiveComplexHeatmap package can directly export static
## complex heatmaps into an interactive Shiny app with zero effort. Have a try!
##
## This message can be suppressed by:
## suppressPackageStartupMessages(library(ComplexHeatmap))
## ========================================
# 首先生成三个矩阵,最后一个只有一列
set.seed(123)
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(runif(60, max = 3, min = 1), 6, 10)
mat2 = rbind(mat2, matrix(runif(60, max = 2, min = 0), 6, 10))
rownames(mat2) = paste0("R", 1:12)
colnames(mat2) = paste0("C", 1:10)
le = sample(letters[1:3], 12, replace = TRUE)
names(le) = paste0("R", 1:12)
ind = sample(12, 12)
mat1 = mat1[ind, ]
mat2 = mat2[ind, ]
le = le[ind]
ht1 = Heatmap(mat1, name = "rnorm")
ht2 = Heatmap(mat2, name = "runif")
ht3 = Heatmap(le, name = "letters")
ht1 + ht2 + ht3
4.1 标题
这里的标题是表示全局标题,也包括行标题和列标题,需要借助draw()
函数添加
library(circlize)
## ========================================
## circlize version 0.4.13
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
##
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
## in R. Bioinformatics 2014.
##
## This message can be suppressed by:
## suppressPackageStartupMessages(library(circlize))
## ========================================
col_rnorm = colorRamp2(c(-3, 0, 3), c("green", "white", "red"))
col_runif = colorRamp2(c(0, 3), c("white", "orange"))
col_letters = c("a" = "pink", "b" = "purple", "c" = "blue")
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm,
row_title = "Heatmap 1", column_title = "Heatmap 1")
ht2 = Heatmap(mat2, name = "runif", col = col_runif,
row_title = "Heatmap 2", column_title = "Heatmap 2")
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, row_title = "Three heatmaps, row title", row_title_gp = gpar(col = "red"),
column_title = "Three heatmaps, column title", column_title_gp = gpar(fontsize = 16))
4.2 大小
先在单个热图里设置好大小,然后再拼起来
ht2 = Heatmap(mat2, name = "runif", col = col_runif, width = unit(4, "cm"))
ht3 = Heatmap(le, name = "letters", col = col_letters, width = unit(5, "mm"))
ht1 + ht2 + ht3
也可以不使用unit()
函数
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, width = 6)
ht2 = Heatmap(mat2, name = "runif", col = col_runif, width = 4)
ht3 = Heatmap(le, name = "letters", col = col_letters, width = 1)
ht1 + ht2 + ht3
4.3 热图之间的间隔
ht_gap
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, ht_gap = unit(1, "cm"))
draw(ht_list, ht_gap = unit(c(3, 10), "mm"))
4.4 相对于主热图的自动调整
对于多个热图,总有一个是主要的热图,其他热图都要根据主热图进行调整,这些调整的地方主要包括:
主热图有行聚类,其余热图没有; 非主热图的行标题会被移除; 如果主热图按行分割,其余热图也会按行分割; 其余热图的高度和主热图保持一致
默认写在第一个的是主热图:
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht2 + ht1 + ht3 # ht2是主热图,所以ht1的聚类被忽略了
也可以通过main_heatmap
参数手动指定主热图:
ht_list = ht2 + ht1 + ht3
draw(ht_list, main_heatmap = "rnorm")
虽然除主热图外其余热图没有行聚类,但是依然可以用row_dend_side
和row_sub_title_side
调整聚类树的位置
ht_list = ht2 + ht1 + ht3
draw(ht_list, main_heatmap = "rnorm", row_dend_side = "right", row_sub_title_side = "left")
两个热图之间的行名没有了,可以通过auto_adjust = FALSE
参数调整
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, auto_adjust = F)
4.5 使用draw()
函数设置主热图
主要是设置行的参数,如果使用了draw()
函数设置一些参数,则单个热图里的这些设置会被覆盖,这些设置主要包括下面这些:
控制行的顺序:
cluster_rows clustering_distance_rows clustering_method_rows row_dend_width show_row_dend row_dend_reorder row_dend_gp row_order
控制行分割:
row_gap row_km row_km_repeats row_split
控制热图高度:
height heatmap_height
一个参数被覆盖的小例子:
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2, cluster_rows = FALSE)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, row_km = 1, row_split = le, cluster_rows = TRUE)
4.6 注释条的调整
对于多个热图来说,每个小热图的注释条高度是不一样的,但是使用ComplexHeatmap
包会自动调整,使其高度一致。
如下所示,ht2
的聚类树的高度就是被调整过的:
ha1 = HeatmapAnnotation(foo1 = 1:10, annotation_name_side = "left")
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, top_annotation = ha1)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht1 + ht2 + ht3
再来一个更加复杂的例子:
ha1 = HeatmapAnnotation(foo1 = 1:10, bar1 = anno_points(1:10),
annotation_name_side = "left")
ha2 = HeatmapAnnotation(bar2 = anno_barplot(1:10))
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, top_annotation = ha1)
ht2 = Heatmap(mat2, name = "runif", col = col_runif, top_annotation = ha2)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, ht_gap = unit(c(6, 2), "mm"))
4.7 注释条的排列
除了热图之间可以排列,热图和注释条之间也可以排列。
ha1 = rowAnnotation(foo = 1:12, bar = anno_barplot(1:12, width = unit(4, "cm")))
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2)
ht1 + ha1
改变彼此之间的间隔:
Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2) +
rowAnnotation(foo = 1:12) +
rowAnnotation(bar = anno_barplot(1:12, width = unit(4, "cm")))
4.8 只排列注释条
rowAnnotation(foo = 1:12) +
rowAnnotation(bar = anno_barplot(1:12, width = unit(4, "cm")))
如果只有一个HeatmapAnnotation
对象,必须借助NULL
才能画出来:
rowAnnotation(bar = anno_barplot(1:12, width = unit(4, "cm"))) + NULL
4.9 垂直排列
和水平排列一模一样的原理,但是要把+
换成%v%
mat1t = t(mat1)
mat2t = t(mat2)
ht1 = Heatmap(mat1t, name = "rnorm", col = col_rnorm, row_title = "rnorm")
ht2 = Heatmap(mat2t, name = "runif", col = col_runif, row_title = "runif")
ht3 = Heatmap(rbind(letters = le), name = "letters", col = col_letters)
ht_list = ht1 %v% ht2 %v% ht3
draw(ht_list)
一个稍微复杂的例子:
ht1 = Heatmap(mat1t, name = "rnorm", col = col_rnorm, row_km = 2,
left_annotation = rowAnnotation(foo1 = 1:10, bar1 = anno_barplot(1:10)))
ha = HeatmapAnnotation(foo = anno_barplot(1:12, height = unit(2, "cm"),
axis_param = list(side = "right")))
ht2 = Heatmap(mat2t, name = "runif", col = col_runif, row_km = 2,
left_annotation = rowAnnotation(foo2 = 1:10))
ht3 = Heatmap(rbind(letters = le), name = "letters", col = col_letters)
ht_list = ht1 %v% ha %v% ht2 %v% ht3
draw(ht_list, column_km = 2)
4.10 取子集
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm,
left_annotation = rowAnnotation(foo1 = 1:12, bar1 = anno_points(1:12)))
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ha = rowAnnotation(foo2 = anno_barplot(1:12), bar2 = 12:1)
ht_list = ht1 + ht2 + ha
names(ht_list)
## [1] "rnorm" "runif" "foo2" "bar2"
ht_list[1:6, c("rnorm", "bar2")] # 取子集
4.11 - 4.15(略)
sessionInfo()
## R version 4.1.0 (2021-05-18)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] circlize_0.4.13 ComplexHeatmap_2.8.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 cluster_2.1.2 knitr_1.33
## [4] magrittr_2.0.1 BiocGenerics_0.38.0 IRanges_2.26.0
## [7] doParallel_1.0.16 clue_0.3-59 colorspace_2.0-2
## [10] rjson_0.2.20 foreach_1.5.1 highr_0.9
## [13] stringr_1.4.0 tools_4.1.0 parallel_4.1.0
## [16] xfun_0.25 png_0.1-7 iterators_1.0.13
## [19] matrixStats_0.60.0 digest_0.6.27 crayon_1.4.1
## [22] RColorBrewer_1.1-2 S4Vectors_0.30.0 GlobalOptions_0.1.2
## [25] codetools_0.2-18 shape_1.4.6 evaluate_0.14
## [28] stringi_1.7.3 compiler_4.1.0 magick_2.7.3
## [31] stats4_4.1.0 Cairo_1.5-12.2 GetoptLong_1.0.5
以上就是本系列第5篇的内容,本系列内容较多,更多内容将逐步推送!
欢迎关注我的公众号:医学和生信笔记
医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!