在R中的dendogram中添加标签

在R中的dendogram中添加标签,r,time-series,hierarchical-clustering,dendrogram,dendextend,R,Time Series,Hierarchical Clustering,Dendrogram,Dendextend,我正在尝试对时间序列应用层次聚类,以便识别在时间序列中具有类似行为的状态,即相对于基准的居住百分比变化。我得到了树状图,但我在x轴上得到的索引只是数字,我想要州名。 我的数据如下所示: 这是我代码的一部分 data <- dataset #Convert to factor cols <- c("country_region_code", "country_region", "sub_region_1", "is

我正在尝试对时间序列应用层次聚类,以便识别在时间序列中具有类似行为的状态,即相对于基准的居住百分比变化。我得到了树状图,但我在x轴上得到的索引只是数字,我想要州名。 我的数据如下所示:

这是我代码的一部分

data <- dataset
#Convert to factor
cols <- c("country_region_code", "country_region", "sub_region_1", "iso_3166_2_code")
data[cols] <- lapply(data[cols], factor)
sapply(data, class)
data$date <- as.Date(data$date)
summary(data)

#Data preparation
n <- 10
s <- sample(1:100, n)
i <- c(s,0+s,   279+s,  556+s,  833+s,  1110+s, 1387+s, 1664+s, 1941+s, 2218+s, 2495+s, 2772+s, 3049+s, 3326+s, 3603+s, 3880+s, 4157+s, 4434+s, 4711+s, 4988+s, 5265+s, 5542+s, 5819+s, 6096+s, 6373+s, 6650+s, 6927+s, 7204+s, 7481+s, 7758+s, 8035+s, 8312+s, 8589+s, 8866+s)
d <- data[i,3:4]
d$residential <- data[i,11]
d[,2] =NULL
str(d)

pattern <- c(rep('Mexico', n),
             rep('Aguascalientes', n),
             rep('Baja California',n),
             rep('Baja California Sur',n),
             rep('Campeche',n),
             rep('Coahuila',n),
             rep('Colima',n),
             rep('Chiapas',n),
             rep('Chihuahua',n),
             rep('Durango',n),
             rep('Guanajuato',n),
             rep('Guerrero',n),
             rep('Hidalgo',n),
             rep('Jalisco',n),
             rep('México City',n),
             rep('Michoacan',n),
             rep('Morelos',n),
             rep('Nayarit',n),
             rep('Nuevo León',n),
             rep('Oaxaca',n),
             rep('Puebla',n),
             rep('Querétaro',n),
             rep('Quintana Roo',n),
             rep('San Luis Potosí',n),
             rep('Sinaloa',n),
             rep('Sonora',n), 
             rep('Tabasco',n),
             rep('Tamaulipas',n),
             rep('Tlaxcala',n),
             rep('Veracruz',n),
             rep('Yucatán',n),
             rep('Zacatecas.',n))
d <- data.matrix(d)
distance <- dist(d, method = 'euclidean')
hc <- hclust(distance, method="ward.D")
plot(hc, cex=.7, hang = -1, col='blue', labels=pattern)

我希望有人能帮助我,我有点厌倦了这个

也许它可以用一个替代基本r绘图函数的方法工作。试试看石斛。它应该在轴上显示标签。为此,您将需要ggplot2

devtools::install("nicolash2/ggdendroplot")
library(ggdendroplot)
library(ggplot2)

ggplot() + geom_dendro(hc)

如果您想修改它(打开它、给它上色等等),请查看github页面:

很抱歉,但是这个包有什么意义?在设置对象的参数之后,为什么不直接使用dendextend::as.ggdend呢?这个包只是一个快速简单的解决方案。当然,它没有Dendestend那么多的选项。我想你也可以使用denextend来解决这个问题,我只是想提供一种可能的方法。鉴于这篇文章没有其他答案,我不明白这有什么害处。出于同样的原因,我也不知道为什么会被否决,即使这不是一个理想的答案。嘿,请为你的问题做一个独立的可复制的例子。此外,请查看Dendestend的详细案例,它应该支持您的用例:
devtools::install("nicolash2/ggdendroplot")
library(ggdendroplot)
library(ggplot2)

ggplot() + geom_dendro(hc)