如何创建带有彩色分支的树状图? [英] How to create a dendrogram with colored branches?
本文介绍了如何创建带有彩色分支的树状图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在R中创建一个有色分支的树状图,如下图所示。
所以到目前为止,我使用以下命令来创建标准树状图:
d<-dist(as.matrix(data [,29] ))#查找距离矩阵
hc<-hclust(d)#应用分层聚类
plot(hc,labels = data [,1],main =,xlab =)#图树状图
我应该如何修改此代码以获得所需的结果?
提前感谢您的帮助。
解决方案
您可以使用
I would like to create a dendrogram in R which has colored branches, like the one shown below.
So far I used following commands to create a standard dendrogram:
d <- dist(as.matrix(data[,29])) # find distance matrix
hc <- hclust(d) # apply hirarchical clustering
plot(hc,labels=data[,1], main="", xlab="") # plot the dendrogram
How should I modify this code to obtain desired result ?
Thanks in advance for your help.
解决方案
You could use the dendextend package, aimed for tasks such as this:
# install the package:
if (!require('dendextend')) install.packages('dendextend'); library('dendextend')
## Example:
dend <- as.dendrogram(hclust(dist(USArrests), "ave"))
d1=color_branches(dend,k=5, col = c(3,1,1,4,1))
plot(d1) # selective coloring of branches :)
d2=color_branches(d1,k=5) # auto-coloring 5 clusters of branches.
plot(d2)
# More examples are in ?color_branches
You can see many examples in the presentations and vignettes of the package, in the "usage" section in the following URL: https://github.com/talgalili/dendextend
这篇关于如何创建带有彩色分支的树状图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文