4

我想用 reorder.dendrogram 重新排序一个树状图,但不能弯曲我的头关于如何设置参数。

树状图有一个“异常值”分支,我想将其移至另一侧。

我可以通过这个 cut/merge 调用来做到这一点,但肯定可以通过重新排序来实现吗?

这是树状图:

tdro <- structure(list(structure(9L, members = 1L, height = 0, label = "leaf1", leaf = TRUE, class = "dendrogram"), 
structure(list(structure(list(structure(list(structure(list(
    structure(15L, label = "leaf2", members = 1L, height = 0, leaf = TRUE, class = "dendrogram"), 
    structure(14L, label = "leaf3", members = 1L, height = 0, leaf = TRUE, class = "dendrogram")), members = 2L, midpoint = 0.5, height = 24.8381484584436, class = "dendrogram"), 
    structure(list(structure(13L, label = "leaf4", members = 1L, height = 0, leaf = TRUE, class = "dendrogram"), 
        structure(12L, label = "leaf5", members = 1L, height = 0, leaf = TRUE, class = "dendrogram")), members = 2L, midpoint = 0.5, height = 24.3975287023022, class = "dendrogram")), members = 4L, midpoint = 1.5, height = 33.1101361501252, class = "dendrogram"), 
    structure(list(structure(10L, members = 1L, height = 0, label = "leaf6", leaf = TRUE, class = "dendrogram"), 
        structure(list(structure(11L, label = "leaf7", members = 1L, height = 0, leaf = TRUE, class = "dendrogram"), 
            structure(8L, label = "leaf8", members = 1L, height = 0, leaf = TRUE, class = "dendrogram")), members = 2L, midpoint = 0.5, height = 30.9363490461899, class = "dendrogram")), members = 3L, midpoint = 0.75, height = 32.3912839969328, class = "dendrogram")), members = 7L, midpoint = 3.125, height = 36.9011152635297, class = "dendrogram"), 
    structure(list(structure(5L, members = 1L, height = 0, label = "leaf9", leaf = TRUE, class = "dendrogram"), 
        structure(list(structure(list(structure(4L, members = 1L, height = 0, label = "leaf10", leaf = TRUE, class = "dendrogram"), 
            structure(list(structure(2L, label = "leaf11", members = 1L, height = 0, leaf = TRUE, class = "dendrogram"), 
              structure(6L, label = "leaf12", members = 1L, height = 0, leaf = TRUE, class = "dendrogram")), members = 2L, midpoint = 0.5, height = 20.537001267289, class = "dendrogram")), members = 3L, midpoint = 0.75, height = 27.9153607968991, class = "dendrogram"), 
            structure(list(structure(list(structure(7L, label = "leaf13", members = 1L, height = 0, leaf = TRUE, class = "dendrogram"), 
              structure(3L, label = "leaf14", members = 1L, height = 0, leaf = TRUE, class = "dendrogram")), members = 2L, midpoint = 0.5, height = 19.3278637150999, class = "dendrogram"), 
              structure(1L, members = 1L, height = 0, label = "leaf15", leaf = TRUE, class = "dendrogram")), members = 3L, midpoint = 1.25, height = 27.6624008548179, class = "dendrogram")), members = 6L, midpoint = 2.5, height = 30.8706298846279, class = "dendrogram")), members = 7L, midpoint = 1.75, height = 41.426993488228, class = "dendrogram")), members = 14L, midpoint = 5.9375, height = 41.7600747862578, class = "dendrogram")), members = 15L, midpoint = 3.46875, height = 66.2893195420674, class = "dendrogram")
plot(tdro)

这是我所追求的:

ddr_cut <- cut(tdro, 50)
want_tdro <- merge(ddr_cut$lower[[2]],
                   ddr_cut$lower[[1]],
                   height=attr(ddr_cut$upper, "height"))
plot(want_tdro)

问题是,下面的调用应该是怎样的:

plot(reorder(tdro, c(15,1:14)))

这样就变成了want_tdro?为什么?_

4

1 回答 1

5

您提供给reorder.dendrogram()函数的权重必须与原始数据用于构建树状图的顺序相同。这不一定与您标记的叶子的顺序相同("leaf1""leaf15")。您可以通过使用最简单的权重 来检查基础顺序,1:15然后查看分配给每个叶子的值。

tdro2 <- reorder(tdro, 1:15, mean)
str(tdro2)

如果您查看str()函数的输出,报告的值对应于我们分配的简单权重。由此,我们推导出数据的底层顺序是leaf15,leaf11,leaf14,leaf10,leaf9,leaf12,leaf13,leaf8,leaf1,leaf6,leaf7,leaf5,leaf4,leaf3,leaf2。

由于您希望叶子的顺序是leaf2-leaf15,leaf1,因此您应该为leaf2提供权重1,为leaf3提供权重2,...,为leaf15提供权重14,为leaf1提供权重15,但您必须提供这些权重按照叶子的基本顺序,如下所示:

tdro3 <- reorder(tdro, c(14, 10, 13, 9:8, 11:12, 7, 15, 5:6, 4:1), mean)
plot(tdro3)
于 2013-05-07T13:06:19.350 回答