我有以下代码:
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
.
为了显示两者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