以下代码创建1.树形图和2.带有树形图的热图
mydata <- mtcars hclustfunc <- function(x) hclust(x, method="complete") distfunc <- function(x) dist(x,method="euclidean") d <- distfunc(mydata) fit <- hclustfunc(d) #plot dendogram only plot(fit) groups <- cutree(fit, k=5) # Add rectangle in cluster rect.hclust(fit, k=5, border="red")
这产生了这个情节:
现在我想创建一个带有树状图的热图
# plot heat map with dendogram together. library("gplots") heatmap.2(as.matrix(mydata),dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc,distfun=distfunc);
目前它看起来像这样:
在最终的热图中有一种方法可以为每个簇添加红色矩形(即在左边的树形图上),就像第一个图一样?
我可以通过RowSideColors
在heatmap.2
函数中使用参数来建议替代方案:
heatmap.2(as.matrix(mydata),dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc, distfun=distfunc, RowSideColors=as.character(groups))
如果您想重新分配颜色:
# require(RColorBrewer) cols <- brewer.pal(max(groups), "Set1") heatmap.2(as.matrix(mydata),dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc, distfun=distfunc, RowSideColors=cols[groups])
第一个例子如下所示: