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

相关推荐
我是苏苏13 分钟前
python开发:爬虫示例——GET和POST请求处理
开发语言·爬虫·python
leluckys24 分钟前
swift 专题三 swift 规范一
开发语言·ios·swift
Xiao Xiangζั͡ޓއއ40 分钟前
程序诗篇里的灵动笔触:指针绘就数据的梦幻蓝图<5>
c语言·开发语言·程序人生·学习方法·改行学it
二十雨辰1 小时前
[Java]函数式编程
java·开发语言
西岭千秋雪_2 小时前
ReentrantReadWriteLock源码分析
java·开发语言·jvm
云空2 小时前
《DeepSeek R1:7b 写一个python程序调用摄像头获取视频并显示》
开发语言·python·音视频
清同趣科研3 小时前
扩增子分析|零模型2——基于βNTI的微生物随机性和确定性装配过程(箱线图和柱状图R中实现)
开发语言·r语言
fc&&fl3 小时前
R语言速通
开发语言·学习·r语言
neeef_se3 小时前
Java实习生面试题汇总
java·开发语言
关关钧3 小时前
【R语言】写入数据
开发语言·r语言