【绘图】聚类热图展示数据细节

Last updated on September 27, 2025 pm

配置环境

1
2
3
mamba create -n complexheatmap bioconda::bioconductor-complexheatmap -y
conda install -n complexheatmap conda-forge::r-tidyverse conda-forge::r-irkernel conda-forge::r-circlize -y
conda run -n complexheatmap Rscript -e "IRkernel::installspec(name='complexheatmap', displayname='r-complexheatmap')"

使用示例

1
2
3
4
5
6
require(circlize)
require(ComplexHeatmap)
require(tidyverse)
require(RColorBrewer)
options(repr.plot.width=6, repr.plot.height=12)
RColorBrewer::display.brewer.all(type = "all")
1
2
data <- iris
data %>% head
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 定义连续变量的颜色映射
col_Petal.Width <- colorRamp2(
c(0.2, 1.2, 2),
c(
RColorBrewer::brewer.pal(9, 'YlGnBu')[5],
RColorBrewer::brewer.pal(9, 'YlOrRd')[1],
RColorBrewer::brewer.pal(9, 'YlOrRd')[7]
)
)
# 定义其余变量的颜色映射
col_ANO = list(
Species = c(
"setosa" = RColorBrewer::brewer.pal(9, 'Set1')[1],
"versicolor" = RColorBrewer::brewer.pal(9, 'Set1')[2],
"virginica" = RColorBrewer::brewer.pal(9, 'Set1')[3]
),
Petal.Width = col_Petal.Width
)
#定义注释信息
ha <- HeatmapAnnotation(
Petal.Width = data$Petal.Width,
Species = data$Species,
col = col_ANO,
show_annotation_name = TRUE
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mat <- as.matrix(data[1:4])
rownames(mat) <- NULL
mat_zs <- apply(t(mat), 1, scale)
colnames(mat_zs) <- colnames(mat)
mat <- t(mat_zs)

col_fun <- colorRamp2(
c(seq(from = -2, to = 2, length.out = 11)),
RColorBrewer::brewer.pal(11, 'BrBG')
)

options(repr.plot.width=12, repr.plot.height=2)
Heatmap(mat, name = 'Z-Score',
top_annotation = ha, column_order = order(data$Petal.Width),
col=col_fun)


【绘图】聚类热图展示数据细节
https://hexo.limour.top/cluster-heatmap-displays-data-details
Author
Limour
Posted on
August 11, 2022
Updated on
September 27, 2025
Licensed under