Q&A | 热图的单元格颜色和数字标签可以不一一对应吗?
有同学私信了一个问题:热图的单元格颜色与色阶条对应,单元格标签能不能用另一套数据呢?当然可以了。今天就来简单的回答一下。
22
数据准备
# 创建一个基因表达矩阵数据 以此数据为热图单元格颜色
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 2
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 3
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Sample", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
head(test)
> head(test)
Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7
Gene1 2.591946 0.94322447 1.1294228 -1.7029648 0.5565071 -1.4958268 1.4481296
Gene2 1.880310 -0.10931712 1.9482818 1.2651684 1.7914298 -0.7684170 0.8172005
Gene3 2.074005 -0.07037692 2.9084477 0.3603572 1.5614365 0.4084885 2.6206636
Gene4 2.741277 -0.48431909 1.2502742 -0.5836394 1.7814062 1.9001363 2.4463130
Gene5 2.753295 -0.13833633 1.6783939 -1.9940788 3.4599659 0.1100091 2.4218847
Gene6 1.737329 -0.06876564 0.8522292 1.9022098 1.4179401 1.1403868 2.4424648
Sample8 Sample9 Sample10
Gene1 1.1504492 1.883642 0.659804377
Gene2 0.1679410 2.925546 -0.453913733
Gene3 -0.5661093 2.648230 -0.694936825
Gene4 -1.0861182 1.849791 -0.006846303
Gene5 -0.6653028 3.040377 1.373052045
Gene6 0.7148484 2.292559 -0.635323077
## 创建一个矩阵,以此数据为单元格数值标签
test1 = matrix(rnorm(200,100), 20, 10)
colnames(test1) = paste("Sample", 1:10, sep = "")
rownames(test1) = paste("Gene", 1:20, sep = "")
绘制
#devtools::install_github("jokergoo/ComplexHeatmap")
library('ComplexHeatmap')
library(circlize)
# 色阶颜色
col_fun = colorRamp2(c(-2, 0, 2), c("#4878b5", "#fefec1", "#d83128"))
# 热图绘制
Heatmap(test, name = "test", col = col_fun,
row_km = 4, column_km = 2,
cluster_rows = T,
cluster_columns = T,
cell_fun = function(j, i, x, y, width, height, fill) { #数值标签添加
grid.text(sprintf("%.1f", test1[i, j]), x, y, gp = gpar(fontsize = 10))
})
木舟笔记2022年度VIP企划
关于木舟笔记2022年度VIP会员企划
详见:https://mp.weixin.qq.com/s/tOIXdM3iZ6sNI0fi_CG94w
往期
- 跟着Nature学作图 | 配对哑铃图+分组拟合曲线+分类变量热图
- (免费教程+代码领取)|跟着Cell学作图系列合集
- 跟着Nat Commun学作图 | 1.批量箱线图+散点+差异分析
- 跟着Nat Commun学作图 | 2.时间线图
- 跟着Nat Commun学作图 | 3.物种丰度堆积柱状图
- 跟着Nat Commun学作图 | 4.配对箱线图+差异分析