如何在执行双聚类(行和列)后在heatmap.2上添加ColSideColors

 洗个小枣_312 发布于 2023-01-19 11:06

我有以下代码:

library(gplots)
library(RColorBrewer);

setwd("~/Desktop")
mydata <- mtcars
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x,method="euclidean")

d <- distfunc(mydata)
fit <- hclustfunc(d)
clusters <- cutree(fit, h=100)
nofclust.height <-  length(unique(as.vector(clusters)));

# Colorings
hmcols <- rev(redgreen(2750))
selcol <- colorRampPalette(brewer.pal(12,"Set3"))
selcol2 <- colorRampPalette(brewer.pal(9,"Set1"))
clustcol.height = selcol2(nofclust.height);

heatmap.2(as.matrix(mydata), 
           trace='none', 
           dendrogram='both', 
           key=F,
           Colv=T, 
           scale='row',
           hclust=hclustfunc, distfun=distfunc, col=hmcols,
           symbreak=T,
           margins=c(7,10), keysize=0.1,
           lwid=c(5,0.5,3), lhei=c(0.05,0.5),
           lmat=rbind(c(5,0,4),c(3,1,2)),
           labRow=rownames(mydata),
           #ColSideColors=clustcol.height[clusters],  # This line doesn't work
           RowSideColors=clustcol.height[clusters])

其中产生如下图: 在此输入图像描述

我想要做的是在行和列上执行聚类,并在树形图旁边显示聚类条(RowSideColors和ColSideColors).我怎样才能做到这一点?

目前我只是成功RowSideColors 而不是那个ColSideColors.

1 个回答
  • 为了显示两者RowSideColors,ColSideColors您必须分别获取矩阵的行和列的集群分配.目前,对象"群集"包含仅与行对应的群集.

    # set the custom distance and clustering functions, per your example
    hclustfunc <- function(x) hclust(x, method="complete")
    distfunc <- function(x) dist(x, method="euclidean")
    
    # perform clustering on rows and columns
    cl.row <- hclustfunc(distfunc(mydata))
    cl.col <- hclustfunc(distfunc(t(mydata)))
    
    # extract cluster assignments; i.e. k=8 (rows) k=5 (columns)
    gr.row <- cutree(cl.row, 8)
    gr.col <- cutree(cl.col, 5)
    
    # require(RColorBrewer)
    col1 <- brewer.pal(8, "Set1")
    col2 <- brewer.pal(5, "Pastel1")
    
    # require(gplots)    
    heatmap.2(as.matrix(mydata), hclustfun=hclustfunc, distfun=distfunc,   
              RowSideColors=col1[gr.row], ColSideColors=col2[gr.col])
    

    您可以使用和检查先验聚类.您也可以使用库来选择最合适的颜色编码.可能是顺序调色板可能更好,以避免过度着色.plot(cl.row)plot(cl.col)RColorBrewer

    2023-01-19 11:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有