Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

r - How to create a dendrogram with colored branches?

I would like to create a dendrogram in R which has colored branches, like the one shown below. enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...