【问题标题】:Change Dendrogram leaves改变树状图叶子
【发布时间】:2011-06-10 20:32:54
【问题描述】:

我想修改从 hclust 对象的绘图生成的树状图中叶子的属性。至少,我想更改颜色,但您可以提供的任何帮助将不胜感激。

我确实尝试用谷歌搜索答案,但我看到的每个解决方案似乎都比我想象的要困难得多。

【问题讨论】:

    标签: r dendrogram phylogeny dendextend


    【解决方案1】:

    不久前,Joris Meys 好心地为我提供了这个改变树叶颜色的 sn-p 代码。修改它以反映您的属性。

    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)
    

    【讨论】:

    • 请注意:如果您想更改标签,只需在 colLab 中返回与 n 不同的内容(可能很明显,但我记得花了一个美好的下午才意识到这一点!)
    【解决方案2】:

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

    你可以这样称呼它:

    
    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 
    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,我创建了一个rect.dendrogram 函数。并且,在其他事情之间,集成了您在功能中添加“文本”的修改。如果有兴趣,您可以在这里找到代码:github.com/talgalili/dendextend/blob/master/R/rect.dendrogram.R 如果您希望您的真实姓名出现在函数文档的致谢名单中,请与我们联系。关于,Tal Galili
    【解决方案3】:

    这是一个使用名为“dendextend”的新包来解决这个问题的方法,该包正是为这类事情而构建的。

    您可以在以下 URL 的“使用”部分的演示文稿和小插曲中看到许多示例: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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 2013-06-18
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多