我有以下代码:

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

最佳答案

为了同时显示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库选择最合适的颜色编码。可能最好使用顺序调色板以避免过度着色。

07-24 22:21