3

亲爱的居民 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。由于论文截止日期迫在眉睫,我将不胜感激任何建议。谢谢,-伊丽莎白

4

2 回答 2

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

在此处输入图像描述


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

于 2012-05-13T15:12:39.683 回答
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

带有彩色标签的树状图

于 2014-04-24T04:43:32.323 回答