R语言 | 使用 ComplexHeatmap 绘制热图,分区并给对角线分区加黑边框

目的:画热图,分区,给对角线分区添加黑色边框

建议直接看0和4。

0. 准备数据

复制代码
# 安装并加载必要的包
#install.packages("ComplexHeatmap")  # 如果尚未安装
library(ComplexHeatmap)

# 使用 iris 数据集 #data(iris)

# 选择数值列(去掉物种列)
data0 <- iris
rownames(data0)=paste0(iris$Species, 1:nrow(data0))

# data0 <- mtcars 分类效果不好


# 随机抽取30个
set.seed(42)
dat=data0[sample(nrow(data0), 30), 1:4]
#dat=data0

# 计算余弦距离
#install.packages("proxy")      # 如果尚未安装
library("proxy")
distance_matrix <- as.matrix(dist(dat, method = "cosine"))
# 如果不想安装新包,也可以使用默认的欧氏距离:
#distance_matrix <- as.matrix(dist(iris_data, method = "euclidean"))

# 使用相似性绘图 simi=1-dist
similarity=1-distance_matrix

dim(similarity)
[1] 30 30

1. Heatmap 全部分块加黑框

复制代码
library(circlize)
col_fun = colorRamp2(c(-2, 0, 2), c("green", "white", "red"))
col_fun(seq(-3, 3))

Heatmap(similarity, name = "mat", #col = col_fun,
        row_km = 3, column_km = 3,
        )
# 每个分块绘制黑边框
# When the heatmap is split, layer_fun is applied in every slice.
Heatmap(similarity, name = "mat", #col = col_fun,
        row_km = 3, column_km = 3,
        layer_fun = function(j, i, x, y, width, height, fill) {
            # 全部分块都加黑框
            v = pindex(similarity, i, j)
            #grid.text(sprintf("%.1f", v), x, y, gp = gpar(fontsize = 10))
            str(v)
            grid.rect(gp = gpar(lwd = 2, fill = "transparent"))
            if(sum(v > 0)/length(v) > 0.75) {
            }
        })

2. 为对角线分块添加黑边框

复制代码
Heatmap(similarity, name = "mat",
        #col = c("white", "yellow", "red3"),
        #col = col_fun,
        col =  colorRamp2(c(0.5, 0.75, 1), 
                          c("white", "yellow", "red3")),
        row_km = 3, column_km = 3,
        layer_fun = function(j, i, x, y, width, height, fill, slice_r, slice_c) {
          v = pindex(similarity, i, j)
          #grid.text(sprintf("%.1f", v), x, y, gp = gpar(fontsize = 10))
          if(slice_r == slice_c) {
            grid.rect(gp = gpar(lwd = 4, fill = "transparent", col="black"))
          }
        })

3. 添加列注释

还有一个与 pheatmap 包同名的函数:

复制代码
annotation_col = data.frame(
  type = data0$Species,
  row.names = rownames(data0)
)[rownames(dat), ,drop=F]
# set colors
ann_colors = list(
  #type = c('setosa'="#ed553b", 'versicolor'="#99b433", 'virginica'="orange")
  type = c('setosa'="violetred1", 
           'versicolor'="turquoise2", 
           'virginica'="blueviolet")
)
# "#ed553b", "#99b433"
#violetred1,turquoise2,

pheatmap(similarity,
         name = "Cosine\nsimilarity",
         main="xx", 
         border_color = NA,
         clustering_method = "ward.D2",
         annotation_col = annotation_col, #set anno for column
         annotation_colors = ann_colors, #set colors
         
         #col = c("white", "yellow", "red3"),
         #col = col_fun,
         col =  colorRamp2(c(0.8, 0.9, 1), 
                  c("white", "yellow", "red3")),
         row_km = 3, column_km = 3,
         layer_fun = function(j, i, x, y, width, height, fill, slice_r, slice_c) {
           v = pindex(similarity, i, j)
           #grid.text(sprintf("%.1f", v), x, y, gp = gpar(fontsize = 10))
           if(slice_r == slice_c) {
             grid.rect(gp = gpar(lwd = 4, fill = "transparent", col="black"))
           }
         })

Bug:

有一个问题:不同次执行,图竟然是不同的,不仅仅是分类的排列顺序问题,而是分类本身也不同了。搜了一下, 竟然受到随机数种子的影响?!固定的数据,固定的参数,每次聚类为什么还要受到随机数影响?不理解!难道非监督的聚类还要人工判断对不对?

比如,对以上最后一个聚类函数,设置不同的随机数种子,结果分别是:

复制代码
# set.seed(45) #这个随机数竟然影响分类位置?!比如修改随机数种子,图分别为
pheatmap(similarity,
         name = "Cosine\nsimilarity",
         main="xx", 
         border_color = NA,
         clustering_method = "ward.D2",
         annotation_col = annotation_col, #set anno for column
         annotation_colors = ann_colors, #set colors
         
         #col = c("white", "yellow", "red3"),
         #col = col_fun,
         col =  colorRamp2(c(0.8, 0.9, 1), 
                  c("white", "yellow", "red3")),
         row_km = 3, column_km = 3,
         layer_fun = function(j, i, x, y, width, height, fill, slice_r, slice_c) {
           v = pindex(similarity, i, j)
           #grid.text(sprintf("%.1f", v), x, y, gp = gpar(fontsize = 10))
           if(slice_r == slice_c) {
             grid.rect(gp = gpar(lwd = 4, fill = "transparent", col="black"))
           }
         })

原因:使用kmeans聚类,确实是随机数确定初始中心的。不使用kmeans聚类,就不会受到随机数的影响。

4. 层次聚类,对结果分群

  • 原来:row_km = 3, column_km = 3, #kmeans确实是种子确定初始中心,结果会随随机数而变化

  • 现在:cutree_row=3, cutree_cols=3, #层次聚类是稳定的

    pheatmap(similarity,
    name = "Cosine\nsimilarity",
    main="Hierarchical cluster",
    border_color = NA,
    clustering_method = "ward.D2",
    annotation_col = annotation_col, #set anno for column
    annotation_colors = ann_colors, #set colors

    复制代码
           #col = c("white", "yellow", "red3"),
           #col = col_fun,
           col =  colorRamp2(c(0.8, 0.9, 1), 
                             c("white", "yellow", "red3")),
           #row_km = 3, column_km = 3, #kmeans确实是种子确定初始中心
           cutree_row=3, cutree_cols=3, #层次聚类是稳定的
           layer_fun = function(j, i, x, y, width, height, fill, slice_r, slice_c) {
             v = pindex(similarity, i, j)
             #grid.text(sprintf("%.1f", v), x, y, gp = gpar(fontsize = 10))
             if(slice_r == slice_c) {
               grid.rect(gp = gpar(lwd = 4, fill = "transparent", col="black"))
             }
           })

Ref

相关推荐
爬虫程序猿1 小时前
用 Python 给京东商品详情做“全身 CT”——可量产、可扩展的爬虫实战
开发语言·爬虫·python
徐同保3 小时前
tailwindcss暗色主题切换
开发语言·前端·javascript
蓝纹绿茶3 小时前
bash:**:pip:***python: 错误的解释器: 没有那个文件或目录
开发语言·python·pip
云知谷3 小时前
【经典书籍】C++ Primer 第15章类虚函数与多态 “友元、异常和其他高级特性” 精华讲解
c语言·开发语言·c++·软件工程·团队开发
START_GAME4 小时前
深度学习Diffusers:用 DiffusionPipeline 实现图像生成
开发语言·python·深度学习
不爱编程的小九九4 小时前
小九源码-springboot088-宾馆客房管理系统
java·开发语言·spring boot
Evand J5 小时前
【MATLAB例程】到达角度定位(AOA),平面环境多锚点定位(自适应基站数量),动态轨迹使用EKF滤波优化。附代码下载链接
开发语言·matlab·平面·滤波·aoa·到达角度
细节控菜鸡5 小时前
【2025最新】ArcGIS for JS 实现随着时间变化而变化的热力图
开发语言·javascript·arcgis
Pluto_CSND5 小时前
Java实现gRPC双向流通信
java·开发语言·单元测试
Tiger Z6 小时前
《R for Data Science (2e)》免费中文翻译 (第11章) --- Communication(1)
r语言·数据科学·中文翻译