着色分支在R中的树状图

时间:2012-05-13 11:16:55

标签: r dendrogram

亲爱的居民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而不是n。随着论文截止日期的临近,我将非常感谢任何建议。 谢谢, -Elizabeth

2 个答案:

答案 0 :(得分:2)

您需要设置树形图对象的edgePar元素。

?dendrapply的帮助中,有一个设置节点标签颜色的示例。通过只更改一行指向"edgePar"并设置col,您几乎就在那里:

attr(n, "edgePar") <- c(a$nodePar, list(col = mycols[i], lab.font= i%%3))

完整的修改示例:

## 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

enter image description here


您可以通过仔细研究?dendrapply?as.dendrogram

来阅读所有相关信息

答案 1 :(得分:1)

仅为了解更多信息,如果要为标签着色,请将edgePar更改为nodePar,并使用lab.col。由于节点默认值,如果您希望看起来相同,还需要将pch设置为NA:

## a smallish simple dendrogram
dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave"))

## create random colours for leaves based on a md5 hash of the leaf labels
library(digest);
dL <- dendrapply(dhc, function(n){
  if(is.leaf(n)){
    labelCol <- paste("#",substring(digest(attr(n,"label")),1,6), sep="");
    attr(n, "edgePar") <- list(col = labelCol);
    attr(n, "nodePar") <- list(pch = NA, lab.col = labelCol, lab.cex = 0.75);
  }
  n;
});

plot(dL); ## --> colored labels

Dendrogram with Coloured Labels