当前位置: 首页 > article >正文

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


http://www.kler.cn/a/533934.html

相关文章:

  • 【21天学习AI底层概念】day14 (kaggle新手入门教程)random forests
  • Eureka加密 及Gateway搭建 - 基于SpringBoot不同版本配置方式
  • 【工欲善其事】利用 DeepSeek 实现复杂 Git 操作:从原项目剥离出子版本树并同步到新的代码库中
  • 想品客老师的第十一天:模块化开发
  • 前端 | JavaScript中的reduce方法
  • 手写单例模式
  • Noise Conditional Score Network
  • 玩转goroutine:Golang中对goroutine的理解
  • 多用户同时RDP登入Win10
  • 大型三甲医院算力网络架构的深度剖析与关键技术探索
  • JAVA 二维列表的基础操作与异常
  • python实现多路视频,多窗口播放功能
  • LeetCode:647.回文子串
  • java进阶专栏的学习指南
  • HTML5 教程之标签(3)
  • 2025春招,深度思考MyBatis面试题
  • 修剪二叉搜索树(力扣669)
  • 2025最新软件测试面试大全
  • 实现一个 LRU 风格的缓存类
  • java进阶1——JVM
  • 通过C/C++编程语言实现“数据结构”课程中的链表
  • Leetcode—159. 至多包含两个不同字符的最长子串【中等】Plus
  • ip属地是手机号还是手机位置?一文理清
  • 车载以太网__传输层
  • SpringBoot+SpringDataJPA项目中使用EntityManager执行复杂SQL
  • RabbitMQ中的过期时间