Colouring branches in a dendrogram in R

泪湿孤枕 提交于 2019-12-03 20:29:50

问题


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


回答1:


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

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