Label and color leaf dendrogram (phylogeny) in R using ape package

自作多情 提交于 2019-12-03 09:55:51

Have a look at ?"plot.phylo":

library("ape")
plot(as.phylo(hc), tip.color=colorCodes[substr(rownames(sample), 1, 1)], type="fan")

One more solution to the question is to use the new circlize_dendrogram function which combined the two packages: circlize and dendextend. You will first need to install them:

install.packages("circlize")
devtools::install_github('talgalili/dendextend')

Here is the code to run:

# YOUR CODE
sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("A",25), rep("B",25), rep("C",25), rep("D",25))

## make unique rownames (equal rownames are not allowed)
rownames(sample) <- make.unique(groupCodes)

colorCodes <- c(A="red", B="green", C="blue", D="yellow")


## perform clustering
distSamples <- dist(sample)
hc <- hclust(distSamples)

#--------------
# NEW CODE

dend <- as.dendrogram(hc )
library(dendextend)
labels_colors(dend) <- colorCodes 
# plot(dend)
dend <- color_branches(dend, k=4)

# plot the radial plot
par(mar = rep(0,4))
# circlize_dendrogram(dend, dend_track_height = 0.8) 
circlize_dendrogram(dend) 

And here is the resulting plot:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!