pheatmap是一个非常受欢迎的绘制热图的R包。ComplexHeatmap包即是受之启发而来。你可以发现
为了使庞大并且“陈旧”的(对不起,我不应该这么说)pheatmap用户群能够迅速并且无痛的迁移至ComplexHeatmap,从2.5.2版本开始,我在ComplexHeatmap包中加入了一个
注意如下五个
-
kmeans_k :在pheatmap::pheatmap() 中,如果这个参数被设定,输入矩阵会进行k均值聚类,然后每个cluster使用其均值向量表示。最终的热图是k个均值向量的热图。此操作改变了原始矩阵的大小,而且每个cluster的大小信息丢失了,直接解读均值向量可能会造成对数据的误解。我不赞成此操作,因此我没有支持这个参数。在ComplexHeatmap中,row_km 和column_km 参数可能是一个更好的选择。 -
filename :如果这个参数被设定,热图直接保存至指定的文件中。我认为这只是画蛇添足(没有贬低pheatmap的意思,只是最近在给小孩讲成语故事,然后想在这里使用一下)的一步,ComplexHeatmap::pheatmap() 不支持此参数。 -
width :filename 的宽度。 -
height :filename 的长度。 -
silent : 是否打印信息。
在
1 2 3 | pheatmap::pheatmap(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100) ) |
在
1 2 3 | ComplexHeatmap::pheatmap(mat, color = rev(brewer.pal(n = 7, name = "RdYlBu")) ) |
例子
我们首先创建一个随机数据,这个来自于pheatmap包中提供的例子(https://rdrr.io/cran/pheatmap/man/pheatmap.html).
1 2 3 4 5 6 | test = matrix(rnorm(200), 20, 10) test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2 test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4 colnames(test) = paste("Test", 1:10, sep = "") rownames(test) = paste("Gene", 1:20, sep = "") |
我们载入ComplexHeatmap包,然后执行
1 2 3 | library(ComplexHeatmap) # 注意这是ComplexHeatmap::pheatmap pheatmap(test) |
在
下一个例子是在热图中加入annotation。以下代码是在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | annotation_col = data.frame( CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5 ) rownames(annotation_col) = paste("Test", 1:10, sep = "") annotation_row = data.frame( GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))) ) rownames(annotation_row) = paste("Gene", 1:20, sep = "") ann_colors = list( Time = c("white", "firebrick"), CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"), GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E") ) pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors) |
看起来和
1 2 3 4 5 6 | pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors, row_split = annotation_row$GeneClass, column_split = annotation_col$CellType) |
1 2 3 4 5 | p1 = pheatmap(test, name = "mat1") p2 = rowAnnotation(foo = anno_barplot(1:nrow(test))) p3 = pheatmap(test, name = "mat2", col = c("navy", "white", "firebrick3")) p1 + p2 + p3 |
ComplexHeatmap支持将一个热图导出为一个shiny app,这也同样适用于
1 2 | ht = pheatmap(...) ht_shiny(ht) # 强烈建议试一试 |
还有一件重要的小事是,因为
1 2 3 4 | for(...) { p = pheatmap(...) draw(p) } |
最后我想说的事,这篇文章的主旨并不是鼓励用户直接使用
从pheatmap到ComplexHeatmap的翻译
在“阅读原文”中,你可以找到一个表格,其中详细的列出了如何将
比较
这一小节我比较了相同参数下
1 | compare_pheatmap(test) |
其实等同于:
1 2 | pheatmap::pheatmap(test) ComplexHeatmap::pheatmap(test) |
在往下阅读之前,我先告诉你结论:
只提供一个矩阵:
1 | compare_pheatmap(test) |
对列进行z-score归一化,行聚类距离使用相关性距离:
1 2 3 | compare_pheatmap(test, scale = "row", clustering_distance_rows = "correlation") |
设定颜色:
1 2 | compare_pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50)) |
不对行聚类:
1 2 | compare_pheatmap(test, cluster_row = FALSE) |
不显示legend:
1 2 | compare_pheatmap(test, legend = FALSE) |
在矩阵格子上显示数值:
1 2 | compare_pheatmap(test, display_numbers = TRUE) |
对矩阵格子上的数值进行格式化:
1 2 3 | compare_pheatmap(test, display_numbers = TRUE, number_format = "%.1e") |
自定义矩阵格子上的文字:
1 2 3 | compare_pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))) |
定义legend上的label:
1 2 3 4 | compare_pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1")) |
热图的标题:
1 2 3 4 | compare_pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap") |
添加列的annotation:
1 2 3 4 5 6 7 8 9 10 11 12 13 | annotation_col = data.frame( CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5 ) rownames(annotation_col) = paste("Test", 1:10, sep = "") annotation_row = data.frame( GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))) ) rownames(annotation_row) = paste("Gene", 1:20, sep = "") compare_pheatmap(test, annotation_col = annotation_col) |
不绘制annotation的legend:
1 2 3 | compare_pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE) |
同时添加行和列的annotation:
1 2 3 | compare_pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row) |
调整列名的旋转:
1 2 3 4 | compare_pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45") |
调整列名的旋转至水平方向:
1 2 3 | compare_pheatmap(test, annotation_col = annotation_col, angle_col = "0") |
控制annotation的颜色:
1 2 3 4 5 6 7 8 9 10 | ann_colors = list( Time = c("white", "firebrick"), CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"), GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E") ) compare_pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title") |
同时控制行和列annotation的颜色:
1 2 3 4 | compare_pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors) |
只提供部分annotation的颜色,未提供颜色的annotation使用随机颜色:
1 2 3 | compare_pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) |
将热图分为两部分,我建议直接使用
1 2 3 4 | compare_pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14)) |
使用
1 2 3 4 5 | compare_pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), cutree_col = 2) |
自定义行名:
1 2 3 4 5 6 | labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b") compare_pheatmap(test, annotation_col = annotation_col, labels_row = labels_row) |
自定义聚类的距离:
1 2 3 4 5 | drows = dist(test, method = "minkowski") dcols = dist(t(test), method = "minkowski") compare_pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols) |
对聚类的回调处理:
1 2 3 4 | library(dendsort) callback = function(hc, ...){dendsort(hc)} compare_pheatmap(test, clustering_callback = callback) |
超详细的热图绘制教程(5000余字),真正的保姆级教程
聚类热图怎么按自己的意愿调整分支的顺序?
获取pheatmap聚类后和标准化后的结果
往期精品(点击图片直达文字对应教程)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
后台回复“生信宝典福利第一波”或点击阅读原文获取教程合集