complexheatmap学习—单个热图绘制

作者: drlee_fc74 | 来源:发表于2019-05-10 17:07 被阅读18次

学习指南只要是对于作者提供的学习指南。另外学习的包一定要是github上的。如果是bioconductor上的则会有些参数不支持。

总体设计

  • 绘制热图的包有很多。其实比较好用是的包有pheatmap包。一般的热图绘制只能只能绘制热图本身。并不能在热图旁边绘制别的图。为了能够添加其他的图,因此开发了complexheatmap包。

  • complexheatmap包主要是可以通过不同的对于热图各个部分(上下左右)的注释来扩展热图的功能。

    image.png
  • 该包主要可以使用到函数包括

  1. Heatmap: 绘制单个的热图。这个图本身就包括所有的组成

  2. HeatmapList:绘制一系列的热图和注释

  3. HeatmapAnnotation:对热图进行注释,可以和热图一起时候,也可以单独的使用

单个热图

通过Heatmap我们就可以形成单个热图。

产生随机数据

library(ComplexHeatmap)
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.1.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.
## ========================================
set.seed(123)
nr1 = 4; nr2 = 8; nr3 = 6; nr = nr1 + nr2 + nr3
nc1 = 6; nc2 = 8; nc3 = 10; nc = nc1 + nc2 + nc3
mat = cbind(rbind(matrix(rnorm(nr1*nc1, mean = 1,   sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc1, mean = 0,   sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc1, mean = 0,   sd = 0.5), nr = nr3)),
    rbind(matrix(rnorm(nr1*nc2, mean = 0,   sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc2, mean = 1,   sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc2, mean = 0,   sd = 0.5), nr = nr3)),
    rbind(matrix(rnorm(nr1*nc3, mean = 0.5, sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc3, mean = 0.5, sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc3, mean = 1,   sd = 0.5), nr = nr3))
   )
mat = mat[sample(nr, nr), sample(nc, nc)] # random shuffle rows and columns
rownames(mat) = paste0("row", seq_len(nr))
colnames(mat) = paste0("column", seq_len(nc))
mat[1:5,1:5]
##        column1     column2     column3    column4    column5
## row1 0.8517620  0.75823530 -0.32552445  0.4490584  2.6205200
## row2 1.1183375  0.38998256  0.90882972  0.1391978  1.2814948
## row3 0.5235772  0.72207943  1.08419194  0.9248215 -0.4248522
## row4 0.2550648  1.05534136  0.58403269 -0.1976372 -0.3203530
## row5 0.8783874 -0.01427338  0.02729558 -0.5261114  0.8127096

最基本的显示

通过Heatmap函数我们可以对矩阵进行可视化。最基本的热图和其他热图绘制工具绘制出来的是一样的。 PS:如果我们把绘图结果放到了一个变量里面,我们可以通过draw函数来把结果绘制出来

ht <- Heatmap(mat)
draw(ht)
image.png

颜色的改变

连续性变量的颜色注释

  • 在热图当中如果需要改变颜色的话,一定要使用circlize::colorRamp2()来对颜色进行赋值。这个函数接受两个参数:颜色分割的位置以及相应分割点上的颜色。最后我们的Heatmap当中通过col来制定颜色

PScolorRamp2默认的使用的配色方案是LAB的。如果想使用RGB的则可以通过其中的space参数进行修改。

library(circlize)
## ========================================
## circlize version 0.4.6
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: http://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.
## ========================================
col_fun <- colorRamp2(c(-2, 0, 2), c("navy", "white", "firebrick3"))
Heatmap(mat, col = col_fun)
image.png

这样设定的好处在于:

  1. 即使出现了异常值,在制定的颜色当中,异常值也只是显示最大值而不是说是把整体的颜色分布给破坏了。我们可以比较一下pheatmapHeatmap的结果
mat2 = mat
mat2[1, 1] = 100000
Heatmap(mat2, name = "mat", col = col_fun)
image.png
pheatmap::pheatmap(mat2)
image.png
  1. 另外一个好处是,如果我们要比较不同组数据的热图,这样设置后,他们的数值对应的颜色不会因为整体分布的原因发生改变
p1 <- Heatmap(mat, name = "mat", col = col_fun, column_title = "mat")
p2 <- Heatmap(mat/4, name = "mat", col = col_fun, column_title = "mat/4")
p3 <- Heatmap(abs(mat), name = "mat", col = col_fun, column_title = "abs(mat)")
p1 + p2 + p3
## Warning: Heatmap/annotation names are duplicated: mat
## Warning: Heatmap/annotation names are duplicated: mat, mat
image.png

分类变量的颜色注释

如果要绘制分类变量的热图的话,则需要把每个分类的数字进行一定的赋值。

discrete_mat = matrix(sample(1:4, 100, replace = TRUE), 10, 10)
col1 = structure(1:4, names = c("1", "2", "3", "4")) # black, red, green, blue
mycol <- colorRamp2(breaks = col1, colors = c("red", "blue", "green", "black"))
h1 <- Heatmap(discrete_mat, col = col1)
h2 <- Heatmap(discrete_mat, name = "mat", col = mycol)
h1 + h2
image.png

缺失值的可视化

如果数据当中含有缺失值,如果我们不想去掉想要可视化的话,可以通过na_col来指定颜色

mat_with_na = mat
na_index = sample(c(TRUE, FALSE), nrow(mat)*ncol(mat), replace = TRUE, prob = c(1, 9))
mat_with_na[na_index] = NA
Heatmap(mat_with_na, name = "mat", na_col = "black")
image.png

边框的可视化

  • 通过border,来对整个热图添加边框。这个参数可以接受逻辑值或者具体的颜色
  • 每个格子的边框的变化,我们可以通过rect_gp来进行设置。这个参数可以通过gpar来设置其颜色以及线条宽度的变化。
h1 <- Heatmap(mat, border = T)
h2 <- Heatmap(mat, border = "red")
h3 <- Heatmap(mat, rect_gp = gpar(col = "white", lwd = 2))
h1 + h2 + h3
image.png

标题

通过上图,我们可以使用对热图的四周都可以进行标题注释。

  1. 通过column_title以及row_title设置标题

  2. 通过column_title_side以及row_title_side来设置标题位置

  3. 通过column_title_gp以及row_title_gp设置标题的格式。通过gpar函数来进行设置。

  4. 通过row_title_rot以及column_title_rot设置标题的旋转角度

Heatmap(mat, column_title = "column title", column_title_side = "bottom", column_title_gp = gpar(fontsize = 20, fontface = "bold"), row_title_rot = 0, row_title = "row title", row_title_gp = gpar(col = "red"))
image.png

聚类

基本设置

无监督的聚类属于热图的可视化的一个重要组成部分。

  1. cluster_rows/columns来设置是否进行聚类

  2. show_column/row_dend设置是否显示聚类树(会发生聚类)

  3. column/row_dend_side设置聚类图绘制的位置

  4. column/row_dend_height设置聚类树的高度

Heatmap(mat, cluster_rows = F, show_column_dend = T,
        column_dend_side = "bottom",  column_dend_height = unit(2, "cm"))
image.png

聚类的方法选择

分类聚类只要包括两步:计算距离矩阵以及应用聚类。一般来说计算距离的方式包括pearson, spearman以及kendall。这个计算方式是通过1 - - cor(x, y, method)来实现的。在函数当中则是通过clustering_distance_rows/columns来进行实现的。

Heatmap(mat, name = "mat", clustering_distance_rows = "pearson",
    column_title = "pre-defined distance method (1 - pearson)")
image.png

聚类树的自定义

我们可以对聚类树进行自定义,主要是可以

  1. column/row_dend_gp设置聚类图的自定义

  2. cluster_rows 分开设置不同的颜色

library(dendextend)
## 
## ---------------------
## Welcome to dendextend version 1.10.0
## Type citation('dendextend') for how to cite the package.
## 
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
## 
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## Or contact: <tal.galili@gmail.com>
## 
##  To suppress this message use:  suppressPackageStartupMessages(library(dendextend))
## ---------------------
## 
## Attaching package: 'dendextend'
## The following object is masked from 'package:stats':
## 
##     cutree
row_dend = as.dendrogram(hclust(dist(mat)))
row_dend = color_branches(row_dend, k = 2) # `color_branches()` returns a dendrogram object
Heatmap(mat, name = "mat", cluster_rows = row_dend)
image.png
Heatmap(mat, name = "mat", cluster_rows = row_dend, row_dend_gp = gpar(col = "red"))
image.png

基于聚类结果进行排序

我们可以通过column/row_dend_reorder来对聚类的结果进行重新排序

m2 = matrix(1:100, nr = 10, byrow = TRUE)
h1 <- Heatmap(m2, name = "mat", row_dend_reorder = FALSE, column_title = "no reordering")
h2 <- Heatmap(m2, name = "mat", row_dend_reorder = TRUE, column_title = "apply reordering")
h1 + h2
## Warning: Heatmap/annotation names are duplicated: mat
image.png

设置热图的观测值的顺序

一般情况下,热图当中各个观测值的顺序是基于聚类的分组来进行排列的。有时候我们想要自己排序顺序。这个时候就可以自定义去顺序。通过row_order/column_order可以来定义其排序。 PS:当我们自定义顺序之后,聚类的顺序就随之关闭了。

Heatmap(mat, name = "mat", row_order = sort(rownames(mat)), 
    column_order = sort(colnames(mat)))
image.png

观测值的自定义

默认情况下对于列名和行名都是显示的。我们可以对其进行自定义

  1. show_row/colums_names设置是否显示列名和行名

  2. column/row_names_side设置列名和行名的位置

  3. column/row_names_gp设置列名和行名的自定义

  4. column/row_names_centered设置名称是否中心化

  5. column/row_names_rot设置名称的角度

  6. column/row_labels设置重新定义名称

  7. column_names_max_heightrow_names_max_width设置列名和行名的最大空间。默认的都是6cm。如果名称太长可以通过这个来设置。

row_labels = structure(paste0(letters[1:24], 1:24), names = paste0("row", 1:24))
Heatmap(mat, show_row_names = T, column_names_side = "top",
        row_names_gp = gpar(fontsize = 20, col = c(rep("red", 10), rep("blue", 8))), column_names_centered = TRUE, column_names_rot = 45, row_labels = row_labels[rownames(mat)])
image.png

热图的分割

热图的分割主要包括多种方式

  1. 按照k-means方法来分割。可以通过column/row_km参数进行实现。

  2. 按照固定的分类来进行分割,可以通过row/column_split参数来实现 另外可以通过row_gap来设置分割的距离。

###k-means方式
Heatmap(mat, name = "mat", row_km = 2, column_km = 3)
image.png
### 特定的分组
Heatmap(mat, name = "mat", row_split = data.frame(rep(c("A", "B"), 9), rep(c("C", "D"), each = 9)), column_split = factor(rep(c("C", "D"), 12)))
image.png
Heatmap(mat, name = "mat", row_km = 3, row_gap = unit(5, "mm"))
image.png

相关文章

网友评论

    本文标题:complexheatmap学习—单个热图绘制

    本文链接:https://www.haomeiwen.com/subject/mlzaoqtx.html