继上一篇文章之后(r 中的标签和彩色叶树状图 https://stackoverflow.com/questions/18802519/label-and-color-leaf-dendrogram-in-r)我有一个后续问题。
我的问题与提到的帖子类似,但我想知道是否可以使用 ape 来完成(例如,plot(as.phylo(fit), type="fan", labelCol)
因为它有更多类型的系统发育。
提到的帖子问题是:
代码示例是:
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)
## function to set label color
labelCol <- function(x) {
if (is.leaf(x)) {
## fetch label
label <- attr(x, "label")
code <- substr(label, 1, 1)
## use the following line to reset the label to one letter code
# attr(x, "label") <- code
attr(x, "nodePar") <- list(lab.col=colorCodes[code])
}
return(x)
}
## apply labelCol on all nodes of the dendrogram
d <- dendrapply(as.dendrogram(hc), labelCol)
plot(d)