简体   繁体   中英

How to color branches in cluster dendrogram?

I will appreciate it so much if anyone of you show me how to color the main branches on the Fan clusters.

Please use the following example:

library(ape)
library(cluster) 
data(mtcars)
plot(as.phylo(hclust(dist(mtcars))),type="fan")

You will need to be more specific about what you mean by "color the main branches" but this may give you some ideas:

 phyl <-as.phylo(hclust(dist(mtcars))) 
 plot(phyl,type="fan", edge.col=c("black", "green")[1+(phyl$edge.length >40) ])

The odd numbered edges are the radial arms in a fan plot so this mildly ugly (or perhaps devilishly clever?) hack colors only the arms with length greater than 40:

phyl <-as.phylo(hclust(dist(mtcars)))
plot(phyl,type="fan", edge.col=c("black", "black", "green")[
                                        c(TRUE, FALSE) + 1 + (phyl$edge.length >40) ])

If you want to color the main branches to indicate which class that sample belongs to, then you might find the function ColorDendrogram in the R package sparcl useful (can be downloaded from here ). Here's some sample code:

library(sparcl)

# Create a fake two sample dataset
set.seed(1)
x <- matrix(rnorm(100*20),ncol=20)
y <- c(rep(1,50),rep(2,50))
x[y==1,] <- x[y==1,]+2

# Perform hierarchical clustering
hc <- hclust(dist(x),method="complete")

# Plot
ColorDendrogram(hc,y=y,main="My Simulated Data",branchlength=3)

This will generate a dendrogram where the leaves are colored according to which of the two samples they came from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM