2.3 聚类
聚类是热图可视化的关键组成部分。在ComplexHeatmap包中,分层聚类具有极大的灵活性。你可以通过以下方式来指定聚类:
一种预先定义的距离方法(例如:"euclidean" or "pearson")
一个距离函数
已经包含聚类的对象(hclust或dendrogram对象)
一个聚类函数
首先,对于聚类有一般的设置,例如是应用聚类还是显示树状图,树状图的侧面和树状图的高度。
Heatmap(mat, name = "mat", cluster_rows = FALSE) # 不进行行聚类
singleheatmap_20
Heatmap(mat, name = "mat", show_column_dend = FALSE) # 不显示列树状图
singleheatmap_21
Heatmap(mat, name = "mat", row_dend_side = "right", column_dend_side = "bottom") #树状图位置
singleheatmap_22
Heatmap(mat, name = "mat", column_dend_height = unit(4, "cm"), row_dend_width = unit(4, "cm")) #树状图高、宽
singleheatmap_23
2.3.1 距离方法
层次聚类分为两步:计算距离矩阵和应用聚类。有三种方法来指定聚类的距离度量:
指定距离作为一个预定义的选项。有效值是dist()函数和“pearson”、“spearman”和“kendall”中支持的方法。相关距离定义为1 - cor(x, y, method)。所有这些内置的距离方法都允许NA值。
自定义函数,计算与矩阵的距离。这个函数应该只包含一个参数。请注意在列上的聚类,矩阵会自动转置。
一个自定义的函数,计算到两个向量的距离。
Heatmap(mat, name = "mat", clustering_distance_rows = "pearson", column_title = "pre-defined distance method (1 - pearson)")
singleheatmap_24
Heatmap(mat, name = "mat", clustering_distance_rows = function(m) dist(m), column_title = "a function that calculates distance matrix")
singleheatmap_25
Heatmap(mat, name = "mat", clustering_distance_rows = function(x, y) 1 - cor(x, y), column_title = "a function that calculates pairwise distance")
singleheatmap_26
基于这些特征,我们可以利用两两距离对离群点进行鲁棒聚类。这里我们设置了颜色映射函数,因为我们不想让离群值影响颜色。
mat_with_outliers = mat for(i in 1:10) mat_with_outliers[i, i] = 1000 robust_dist = function(x, y) { qx = quantile(x, c(0.1, 0.9)) qy = quantile(y, c(0.1, 0.9)) l = x > qx[1] & x < qx[2] & y > qy[1] & y < qy[2] x = x[l] y = y[l] sqrt(sum((x - y)^2)) }
我们可以比较使用和不使用鲁棒距离方法两个热图:
Heatmap(mat_with_outliers, name = "mat", col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")), column_title = "dist") Heatmap(mat_with_outliers, name = "mat", col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")), clustering_distance_rows = robust_dist, clustering_distance_columns = robust_dist, column_title = "robust_dist")
singleheatmap_27
如果有合适的距离方法(如stringdist包中的方法),也可以对字符矩阵进行聚类。
mat_letters = matrix(sample(letters[1:4], 100, replace = TRUE), 10) # distance in the ASCII table dist_letters = function(x, y) { x = strtoi(charToRaw(paste(x, collapse = "")), base = 16) y = strtoi(charToRaw(paste(y, collapse = "")), base = 16) sqrt(sum((x - y)^2)) } Heatmap(mat_letters, name = "letters", col = structure(2:5, names = letters[1:4]), clustering_distance_rows = dist_letters, clustering_distance_columns = dist_letters, cell_fun = function(j, i, x, y, w, h, col) { # add text to each grid grid.text(mat_letters[i, j], x, y) })
singleheatmap_28
2.3.2 聚类方法
执行分层聚类的方法,可以通过clustering_method_rows和clustering_method_columns指定。可能的方法是hclust()函数中支持的方法。
Heatmap(mat, name = "mat", clustering_method_rows = "single")
singleheatmap_29
如果已经有一个聚类对象,则可以忽略距离设置,并将cluster_rows或cluster_columns设置为聚类对象或聚类函数。如果它是一个聚类函数,唯一的参数应该是矩阵,它应该返回一个hclust或dendrogram对象,或者一个可以转换为dendrogram类的对象。
在下面的例子中,我们通过预先计算的聚类对象或聚类函数来使用聚类包中的方法来执行聚类:
library(cluster) Heatmap(mat, name = "mat", cluster_rows = diana(mat), cluster_columns = agnes(t(mat)), column_title = "clustering objects")
singleheatmap_30
# 如果将cluster_columns设置为一个函数,则不需要转置矩阵 Heatmap(mat, name = "mat", cluster_rows = diana, cluster_columns = agnes, column_title = "clustering functions")
singleheatmap_32
使用下面的命令也是一样的:
# code only for demonstration Heatmap(mat, name = "mat", cluster_rows = function(m) as.dendrogram(diana(m)), cluster_columns = function(m) as.dendrogram(agnes(m)), column_title = "clutering functions")
2.3.3 渲染树状图
可以通过dendextend包来呈现树形图对象,使树形图更具个性化。
library(dendextend) 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)
singleheatmap_33
Heatmap(mat, name = "mat", cluster_rows = row_dend, row_dend_gp = gpar(col = "red"))
singleheatmap_34png
2.3.4 重排树状图
在Heatmap()函数中,树状图被重新排序,使差异较大的特征更加分离(参阅reorder. dendergram()的文档)。这里的差值(或者称为权重)是通过行来度量的,行表示是行树状图,列表示是列树状图。row_dend_reorder和column_dend_reorder控制是否应用树状图重新排序。重新排序可以通过设置row_dend_reorder = FALSE来关闭。
默认情况下,如果将cluster_rows/cluster_columns设置为逻辑值或聚类函数,则会打开树状图重新排序。如果将cluster_rows/cluster_columns设置为聚类对象,则关闭此选项。
比较下面两个热图:
m2 = matrix(1:100, nr = 10, byrow = TRUE) Heatmap(m2, name = "mat", row_dend_reorder = FALSE, column_title = "no reordering") Heatmap(m2, name = "mat", row_dend_reorder = TRUE, column_title = "apply reordering")
singleheatmap_35
还有许多其他方法可以对树状图进行重新排序,例如dendsort包。也可以根据数据矩阵生成行或列树状图,通过某些方法重新排序它,并将它分配回cluster_rows或cluster_columns。
比较下面两个重排树状图后的热图:
Heatmap(mat, name = "mat", column_title = "default reordering") library(dendsort) row_dend = dendsort(hclust(dist(mat))) col_dend = dendsort(hclust(dist(t(mat)))) Heatmap(mat, name = "mat", cluster_rows = row_dend, cluster_columns = col_dend, column_title = "reorder by dendsort")
2.4 设置行列顺序
聚类用于调整热图的行顺序和列顺序,但仍然可以通过row_order和column_order手动设置顺序。
#如果出错了,重新创建mat数据矩阵 Heatmap(mat, name = "mat", row_order = order(as.numeric(gsub("row", "", rownames(mat)))), column_order = order(as.numeric(gsub("column", "", colnames(mat)))), column_title = "reorder matrix")
singleheatmap_38
顺序可以是字符向量如果它们只是变换矩阵的行名或列名:
Heatmap(mat, name = "mat", row_order = sort(rownames(mat)), column_order = sort(colnames(mat)), column_title = "reorder matrix by row/column names")
singleheatmap_39
2.5 维度名称
默认情况下,行名和列名绘制在热图的右侧和底部。维度名称的侧面、可见性和图形参数设置如下:
Heatmap(mat, name = "mat", row_names_side = "left", row_dend_side = "right", column_names_side = "top", column_dend_side = "bottom")
singleheatmap_40
Heatmap(mat, name = "mat", show_row_names = FALSE)
singleheatmap_41
Heatmap(mat, name = "mat", row_names_gp = gpar(fontsize = 20))
singleheatmap_42.
Heatmap(mat, name = "mat", row_names_gp = gpar(col = c(rep("red", 10), rep("blue", 8))))
singleheatmap_43
Heatmap(mat, name = "mat", row_names_centered = TRUE, column_names_centered = TRUE)
singleheatmap_44
可以通过column_names_rot设置列名的旋转:
Heatmap(mat, name = "mat", column_names_rot = 45) Heatmap(mat, name = "mat", column_names_rot = 45, column_names_side = "top", column_dend_side = "bottom")
singleheatmap_45
如果行名或列名太长,可以使用row_names_max_width或column_names_max_height为它们设置最大空间。行名和列名的默认最大空间都是6厘米。在下面的代码中,max_text_width()是一个帮助函数,用于快速计算文本向量的最大宽度。
mat2 = mat rownames(mat2)[1] = paste(c(letters, LETTERS), collapse = "") Heatmap(mat2, name = "mat", row_title = "default row_names_max_width") Heatmap(mat2, name = "mat", row_title = "row_names_max_width as length of a*", row_names_max_width = max_text_width( rownames(mat2), gp = gpar(fontsize = 12) ))
singleheatmap_46
除了直接使用矩阵中的行/列名,还可以提供另一个对应于行或列的字符向量,并通过row_labels或column_labels设置。
对于基因表达分析,我们可以使用Ensembl ID作为基因ID,作为基因表达矩阵的行名。但是,Ensembl ID用于编制Ensembl数据库的索引,而不是用于人类阅读。相反,我们更愿意将gene symbols作为行名放在热图上,这样更容易阅读。为此,我们只需要将相应的gene symbols分配给row_labels,而不需要修改原始矩阵。
第二个优点是row_labels或column_labels允许重复标签,而矩阵中不允许重复的行名或列名。
下面给出了一个简单的例子,我们把字母作为行标签和列标签:
#使用一个命名向量来确保两者之间的对应关系 row_labels = structure(paste0(letters[1:24], 1:24), names = paste0("row", 1:24)) column_labels = structure(paste0(LETTERS[1:24], 1:24), names = paste0("column", 1:24)) row_labels
> row_labels row1 row2 row3 row4 row5 row6 row7 row8 "a1" "b2" "c3" "d4" "e5" "f6" "g7" "h8" row9 row10 row11 row12 row13 row14 row15 row16 "i9" "j10" "k11" "l12" "m13" "n14" "o15" "p16" row17 row18 row19 row20 row21 row22 row23 row24 "q17" "r18" "s19" "t20" "u21" "v22" "w23" "x24"
Heatmap(mat, name = "mat", row_labels = row_labels[rownames(mat)], column_labels = column_labels[colnames(mat)])
singleheatmap_47
第三个优点是可以在热图中使用数学表达式作为行名。