2. 单个热图
单个热图是最常用的数据可视化方法。尽管ComplexHeatmap包的“亮点”是它可以并行地可视化热图列表,但是,作为热图列表的基本单元,对单个热图进行良好配置仍然是非常重要的。
首先,让我们生成一个随机矩阵,其中有三组在列和三组在行:
set.seed(123)#设种子为了随机可重复 nr1 = 4; nr2 = 8; nr3 = 6; nr = nr1 + nr2 + nr3 nc1 = 6; nc2 = 8; nc3 = 10; nc = nc1 + nc2 + nc3 #cbind按行合并 rbind按列合并 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)] # 随机打乱行和列 rownames(mat) = paste0("row", seq_len(nr)) colnames(mat) = paste0("column", seq_len(nc)) head(mat)
> head(mat) column1 column2 column3 column4 column5 column6 column7 row1 0.90474160 -0.35229823 0.5016096 1.26769942 0.8251229 0.16215217 -0.2869867 row2 0.90882972 0.79157121 1.0726316 0.01299521 0.1391978 0.46833693 1.2814948 row3 0.28074668 0.02987497 0.7052595 1.21514235 0.1747267 0.20949120 -0.6423579 row4 0.02729558 0.75810969 0.5333504 -0.49637424 -0.5261114 0.56724357 0.8127096 row5 -0.32552445 1.03264652 1.1249573 0.66695147 0.4490584 1.04236865 2.6205200 row6 0.58403269 -0.47373731 0.5452483 0.86824798 -0.1976372 -0.03565404 -0.3203530
下面的命令包含Heatmap()函数的最小参数,该函数将矩阵显示为带有默认设置的热图。默认的颜色模式是“蓝-白-红”,它被映射到矩阵中的最小-平均-最大值。
BiocManager::install("ComplexHeatmap") library(ComplexHeatmap) Heatmap(mat)
singleheatmap_1
2.1 颜色
用户应该使用circle::colorRamp2()函数来生成Heatmap()中的颜色映射函数。
在下面的例子中,对-2到2之间的值进行线性插值得到相应的颜色,大于2的值都映射为红色,小于-2的值都映射为绿色。
library(circlize) col_fun = colorRamp2(c(-2, 0, 2), c("green", "white", "red")) Heatmap(mat, name = "mat", col = col_fun)
singleheatmap_2
colorRamp2()使多个热图中的颜色具有可比性,如果它们是用同一个颜色映射函数设置的。在以下三个热图中,相同的颜色总是对应相同的值。
Heatmap(mat, name = "mat", col = col_fun, column_title = "mat") Heatmap(mat/4, name = "mat", col = col_fun, column_title = "mat/4") Heatmap(abs(mat), name = "mat", col = col_fun, column_title = "abs(mat)")
singleheatmap_3
如果矩阵是连续的,你也可以简单地提供一个颜色向量。但是这种方法对离群值是不可靠的,因为映射从矩阵中的最小值开始,以最大值结束。以下颜色映射设置与colorRamp2(seq(min(mat), max(mat), length = 10), rev(rainbow(10))相同。
Heatmap(mat, name = "mat", col = rev(rainbow(10)), column_title = "set a color vector for a continuous matrix")
singleheatmap_4
如果矩阵包含离散值(数字或字符),颜色应指定为命名向量,使离散值映射到颜色成为可能。注意现在图例是由颜色映射向量生成的。
下面为离散数字矩阵设置颜色(不需要将其转换为字符矩阵)。
discrete_mat = matrix(sample(1:4, 100, replace = TRUE), 10, 10) colors = structure(1:4, names = c("1", "2", "3", "4")) # black, red, green, blue Heatmap(discrete_mat, name = "mat", col = colors, column_title = "a discrete numeric matrix")
singleheatmap_5
或者字符矩阵:
discrete_mat = matrix(sample(letters[1:4], 100, replace = TRUE), 10, 10) colors = structure(1:4, names = letters[1:4]) Heatmap(discrete_mat, name = "mat", col = colors, column_title = "a discrete character matrix")
singleheatmap_6
矩阵中允许有NA。可以通过na_col参数来控制NA的颜色(默认为灰色)。包含NA的矩阵可以通过Heatmap()进行聚类。注意图例中没有NA的值。
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 #构建含NA的矩阵 Heatmap(mat_with_na, name = "mat", na_col = "black", column_title = "a matrix with NA values")
singleheatmap_7
颜色空间对于插值颜色非常重要。默认情况下,颜色是在LAB color space中线性插值的,但是可以在colorRamp2()函数中选择颜色空间。
f1 = colorRamp2(seq(min(mat), max(mat), length = 3), c("blue", "#EEEEEE", "red")) f2 = colorRamp2(seq(min(mat), max(mat), length = 3), c("blue", "#EEEEEE", "red"), space = "RGB") Heatmap(mat, name = "mat1", col = f1, column_title = "LAB color space") Heatmap(mat, name = "mat2", col = f2, column_title = "RGB color space")
singleheatmap_8
热图边界的颜色可以通过border/border_gp和rect_gp参数设置。border/border_gp控制热图主体的全局边界,rect_gp控制热图中的网格/单元格的边界。
border的值可以是逻辑的(TRUE对应黑色)或一个颜色字符(例如红色)。这
rect_gp是一个gpar对象,这意味着你只能通过grid::gpar()来设置它。由于填充的颜色已经由热图颜色映射控制,所以您只能在gpar()中设置col参数来控制热图网格的边界。
Heatmap(mat, name = "mat", border = FALSE, column_title = "set heatmap borders")
singleheatmap_11
Heatmap(mat, name = "mat", rect_gp = gpar(col = "white", lwd = 2), column_title = "set cell borders")#lty线型, lwd线宽
singleheatmap_12
Heatmap(mat, name = "mat", rect_gp = gpar(type = "none"), column_title = "nothing is drawn in the heatmap body")
singleheatmap_13
2.2 标题
Heatmap(mat, name = "mat", column_title = "I am a column title", row_title = "I am a row title")
singleheatmap_14
Heatmap(mat, name = "mat", column_title = "I am a column title at the bottom", column_title_side = "bottom")
singleheatmap_15
Heatmap(mat, name = "mat", column_title = "I am a big column title",
column_title_gp = gpar(fontsize = 20, fontface = "bold"))
标题的旋转可以通过row_title_rot和column_title_rot设置,但只允许水平和垂直旋转。
Heatmap(mat, name = "mat", row_title = "row title", row_title_rot = 0)
singleheatmap_17
可以通过设置row_title_gp和column_title_gp中的填充参数来设置标题的背景颜色。如row_title_gp控制文本的颜色,所以border用来控制背景边框的颜色。
Heatmap(mat, name = "mat", column_title = "I am a column title", column_title_gp = gpar(fill = "red", col = "white", border = "blue"))
singleheatmap_19