改变树状图的分 支长度(秘籍图)

问题描述 投票:0回答:1

我正在尝试用R中的库pheatmap绘制热图。我认为,默认情况下,分支长度与在此步骤中合并的群集的“不相似性”成正比。我想碰碰运气,所以这是一个固定值,因为对我而言,它看起来很奇怪!

[如果有人对如何解决这个问题有一个想法,我将非常高兴。

这里是示例代码

library(pheatmap)
test = matrix(rnorm(6000), 100, 60)
pheatmap(test)

干杯!

r dendrogram pheatmap
1个回答
0
投票

这里是两个具有高度相似性的列组的示例:

library(pheatmap)
test = cbind(matrix(rnorm(3000), 100, 30),
            matrix(rnorm(3000)+10, 100, 30))
pheatmap(test)

enter image description here

TIn pheatmap树状图由pheatmap:::draw_dendrogram函数绘制和分支长度存储在h对象中。下面我定义等长分支,添加命令hc$height <- cumsum(rep(1/length(hc$height), length(hc$height)))如下:

draw_dendrogram <- function(hc, gaps, horizontal = T) {
    # Define equal-length branches
    hc$height <- cumsum(rep(1/length(hc$height), length(hc$height)))
    h = hc$height/max(hc$height)/1.05
    m = hc$merge
    o = hc$order
    n = length(o)
    m[m > 0] = n + m[m > 0]
    m[m < 0] = abs(m[m < 0])
    dist = matrix(0, nrow = 2 * n - 1, ncol = 2, dimnames = list(NULL, 
        c("x", "y")))
    dist[1:n, 1] = 1/n/2 + (1/n) * (match(1:n, o) - 1)
    for (i in 1:nrow(m)) {
        dist[n + i, 1] = (dist[m[i, 1], 1] + dist[m[i, 2], 1])/2
        dist[n + i, 2] = h[i]
    }
    draw_connection = function(x1, x2, y1, y2, y) {
        res = list(x = c(x1, x1, x2, x2), y = c(y1, y, y, y2))
        return(res)
    }
    x = rep(NA, nrow(m) * 4)
    y = rep(NA, nrow(m) * 4)
    id = rep(1:nrow(m), rep(4, nrow(m)))
    for (i in 1:nrow(m)) {
        c = draw_connection(dist[m[i, 1], 1], dist[m[i, 2], 1], 
            dist[m[i, 1], 2], dist[m[i, 2], 2], h[i])
        k = (i - 1) * 4 + 1
        x[k:(k + 3)] = c$x
        y[k:(k + 3)] = c$y
    }
    x = pheatmap:::find_coordinates(n, gaps, x * n)$coord
    y = unit(y, "npc")
    if (!horizontal) {
        a = x
        x = unit(1, "npc") - y
        y = unit(1, "npc") - a
    }
    res = polylineGrob(x = x, y = y, id = id)
    return(res)
}
# Replace the non-exported function `draw_dendrogram` in `pheatmap`:
assignInNamespace(x="draw_dendrogram", value=draw_dendrogram, ns="pheatmap")

pheatmap(test)

结果是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.