我想用reorder.dendrogram对树形图进行重新排序,但不能忽视如何设置参数。
树形图有一个“异常值”分支,我想转移到另一边。
我可以通过这个剪切/合并调用来做到这一点,但肯定可以重新排序吗?
这是树形图:
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
? 为什么?
答案 0 :(得分: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。 < / p>
由于你想要叶子的顺序是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)