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

时间:2014-03-09 04:46:52

标签: r plot cluster-analysis heatmap

我有以下代码:

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])

其中产生如下图: enter image description here

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

目前我只能成功展示RowSideColors <{1}} ColSideColors

1 个答案:

答案 0 :(得分:6)

为了显示RowSideColorsColSideColors,您必须分别获取矩阵的行和列的群集分配。目前,对象“群集”包含仅与行对应的群集。

# 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库来选择最合适的颜色编码。可能是顺序调色板可能更好,以避免过度着色。