如何将树状图的标签更改为与列表中的名称匹配的标签?现在我只知道名单号码。
代码:
library(IncDTW)
library(ggplot2)
library(ggdendro)
A <- matrix(1:50, nrow = 50, ncol = 1)
B <- matrix(1:100, nrow = 75, ncol = 1)
C <- matrix(25:49, nrow = 25, ncol = 1)
D <- matrix(1:50, nrow = 50, ncol = 1)
treeList <- list(A,B,C,D)
names(treeList)[1] <- "A"
names(treeList)[2] <- "B"
names(treeList)[3] <- "C"
names(treeList)[4] <- "D"
result <- dtw_dismat(treeList, dist_method = "norm2", return_matrix = F)
distMatrixResult <- result$dismat
hc <- hclust(distMatrixResult, method = "average")
ggdendrogram(hc)发布于 2019-02-08 13:50:53
在dtw_distmat函数之后,您正在松开标签:
$dismat
1 2 3 4
1 0.000000 2.60 4.013333 0.000000
2 2.600000 0.00 6.510000 2.600000
3 4.013333 6.51 0.000000 4.013333
4 0.000000 2.60 4.013333 0.000000您可以使用hclust函数转换dendro_data的输出。然后,您可以更改此转换对象的标签:
hc <- dendro_data(hc)
dict <- setNames(c('A', 'B', 'C', 'D'), 1:4)
hc$labels$label <- sapply(hc$labels$label, function(x) dict[[as.character(x)]])在此之后,ggdendrogram(hc)将返回带有标签的绘图。
发布于 2019-02-11 08:21:18
谢谢你给我指点这个新特性。我将在下一个版本的IncDTW中考虑这个问题。同时,一个快速解决方法是执行以下操作:
a <- matrix(1:9, 3, 3)
a <- a + t(a)
as.dist(a)
b <- usedist::dist_setNames(a, letters[1:3])
b
# where dist_setnames() does the following:
dm <- as.matrix(d)
dimnames(dm) <- list(nm, nm)
stats::as.dist(dm)因此,dist_setnames()将dist.object转换为矩阵,这对于时间序列的小列表很好,但如果距离矩阵变大,则可能是一个问题。
https://stackoverflow.com/questions/54593085
复制相似问题