亲爱的居民 R 天才们,
我想在没有标记叶子的树状图中为簇的分支着色。
我在 Stackoverflow 上找到了以下脚本:
clusDendro <- as.dendrogram(Clustering)
labelColors <- c("red", "blue", "darkgreen", "darkgrey", "purple")
## function to get colorlabels
colLab <- function(n) {
if(is.leaf(n)) {
a <- attributes(n)
# clusMember - a vector designating leaf grouping
# labelColors - a vector of colors for the above grouping
labCol <- labelColors[clusMember[which(names(clusMember) == a$label)]]
attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
}
n
}
## Graph
clusDendro <- dendrapply(clusDendro, colLab)
op <- par(mar = par("mar") + c(0,0,0,2))
plot(clusDendro,
main = "Major title",
horiz = T, type = "triangle", center = T)
par(op)
我已尝试将其按如下方式适应我的数据,但没有成功。
Gdis.UPGMA<-hclust(Gdis, method = "average", members=NULL)
k<-12
Gdiswo<-reorder.hclust(Gdis.UPGMA, Gdis, labels = FALSE)
cutg <- cutree(Gdiswo, k=k)
clusDendro <- as.dendrogram(Gdiswo)
labelColors <- c("red", "blue", "darkgreen", "darkgrey", "purple")
## function to get colorlabels
colLab <- function(n) {
if(is.leaf(n)) {
a <- attributes(n)
# cutg - a vector designating leaf grouping
# labelColors - a vector of colors for the above grouping
labCol <- labelColors[cutg[which(names(cutg) == a$label)]]
attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
}
n
}
## Graph
clusDendro <- dendrapply(clusDendro, colLab)
op <- par(mar = par("mar") + c(0,0,0,2))
plot(clusDendro,
main = "Major title",
horiz = T, type = "triangle", center = T)
par(op)
我怀疑是 n 引起了问题,但我不确定我应该放什么而不是 n。由于论文截止日期迫在眉睫,我将不胜感激任何建议。谢谢,-伊丽莎白