Dear resident R geniuses,
I would like to colour the branches of cluster in a dendrogram where the leaves are not labelled.
I found the following script here on 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)
I have tried adapting it to my data as follows without success.
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)
I suspect n is causing the problem but I am not sure what I am suppose to put instead of n. As dissertation deadlines are looming I would be most grateful for any advice. Thanks, -Elizabeth
You need to set the edgePar
elements of the dendrogram object.
In the help for ?dendrapply
there is an example to set the colours of the node labels. By changing just one line to point to "edgePar"
and setting col
, you are almost there:
attr(n, "edgePar") <- c(a$nodePar, list(col = mycols[i], lab.font= i%%3))
The full modified example:
## a smallish simple dendrogram
dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave"))
## toy example to set colored leaf labels :
local({
colLab <<- function(n) {
if(is.leaf(n)) {
a <- attributes(n)
i <<- i+1
attr(n, "edgePar") <-
c(a$nodePar, list(col = mycols[i], lab.font= i%%3))
}
n
}
mycols <- grDevices::rainbow(attr(dhc21,"members"))
i <- 0
})
dL <- dendrapply(dhc21, colLab)
plot(dL) ## --> colored labels
You can read all about doing this by careful study of ?dendrapply
and ?as.dendrogram
来源:https://stackoverflow.com/questions/10571266/colouring-branches-in-a-dendrogram-in-r