更改树状图叶节点

6

我想修改从hclust对象绘制的树状图中叶子的属性。最少,我想要改变颜色,但是任何你能提供的帮助都会被赞赏。

我尝试过谷歌答案,但是我看到的每个解决方案似乎比我想象的要困难得多。

3个回答

16
不久前,Joris Meys友好地为我提供了这段代码片段,它可以改变叶子的颜色。根据您的属性进行修改。
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)

3
提示:如果您想更改标签,请在“colLab”中返回与“n”不同的内容(可能很明显,但我记得花了一个下午才意识到!) - nico

2
这道题的解决方案使用了一个叫做 "dendextend" 的新包,专门为此类问题而建立。
您可以在该包的演示文稿和vignettes中看到许多示例,在以下URL的“usage”部分中查看: https://github.com/talgalili/dendextend 以下是这个问题的解决方案:
# define dendrogram object to play with:
dend <- as.dendrogram(hclust(dist(USArrests[1:3,]), "ave"))
# loading the package
install.packages('dendextend') # it is now on CRAN
library(dendextend)# let's add some color:
labels_colors(dend) <- 2:4
labels_colors(dend)
plot(dend)

enter image description here


1

不清楚你想用它做什么,但我经常需要在树状图中识别一个分支。我已经修改了rect.hclust方法,添加了密度和标签输入。

You would call it like this:


k <- 3 # number of branches to identify
labels.to.identify <- c('1','2','3')
required.density <- 10 # the density of shading lines, in lines per inch <br>
rect.hclust.nice(tree, k, labels=labels.to.identify, density=density.required)

这是函数



rect.hclust.nice = function (tree, k = NULL, which = NULL, x = NULL, h = NULL, border = 2, 
    cluster = NULL,  density = NULL,labels = NULL, ...) 
{
    if (length(h) > 1 | length(k) > 1) 
        stop("'k' and 'h' must be a scalar")
    if (!is.null(h)) {
        if (!is.null(k)) 
            stop("specify exactly one of 'k' and 'h'")
        k <- min(which(rev(tree$height) < h))
        k <- max(k, 2)
    }
    else if (is.null(k)) 
        stop("specify exactly one of 'k' and 'h'")
    if (k < 2 | k > length(tree$height)) 
        stop(gettextf("k must be between 2 and %d", length(tree$height)), 
            domain = NA)
    if (is.null(cluster)) 
        cluster <- cutree(tree, k = k)
    clustab <- table(cluster)[unique(cluster[tree$order])]
    m <- c(0, cumsum(clustab))
    if (!is.null(x)) {
        if (!is.null(which)) 
            stop("specify exactly one of 'which' and 'x'")
        which <- x
        for (n in 1L:length(x)) which[n] <- max(which(m < x[n]))
    }
    else if (is.null(which)) 
        which <- 1L:k
    if (any(which > k)) 
        stop(gettextf("all elements of 'which' must be between 1 and %d", 
            k), domain = NA)
    border <- rep(border, length.out = length(which))
    labels <- rep(labels, length.out = length(which))
    retval <- list()
    for (n in 1L:length(which)) {
        rect(m[which[n]] + 0.66, par("usr")[3L], m[which[n] + 
            1] + 0.33, mean(rev(tree$height)[(k - 1):k]), border = border[n], col = border[n], density = density, ...)
        text((m[which[n]] + m[which[n] + 1]+1)/2, grconvertY(grconvertY(par("usr")[3L],"user","ndc")+0.02,"ndc","user"),labels[n])
        retval[[n]] <- which(cluster == as.integer(names(clustab)[which[n]]))
    }
    invisible(retval)
}

这是函数的其余部分 :) - skullkey
亲爱的skullkey, 我已经创建了一个rect.dendrogram函数。此外,我还集成了您添加“text”到该函数的修改。如果您有兴趣,可以在此处找到代码:https://github.com/talgalili/dendextend/blob/master/R/rect.dendrogram.R如果您希望在函数文档的致谢中出现真实姓名,请与我联系。祝好,Tal Galili - Tal Galili

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接