I want to plot the clusters of dengrogram like so
but on the image the clusters don't fit the actual cluster showed in the table based on the clusters of the dendrogram.
In the table, cluster 8 consists of one city and on the image there are multiple for instance.
Bissen | 6 |
---|---|
Biwer | 6 |
Boulaide | 7 |
Cents-Clausen | 7 |
Cessange | 8 |
Eich-Weimerskirch-Pfaffenthal | 9 |
Esch-Brill | 10 |
Besides the order of the clusters on the image sometimes change when I run it, How can i fix this?
Here is the an extract of the script:
unstacked_occup <- spread(agregated_data_occup,NAME,rolling_avg)
unstacked_occup[is.na(unstacked_occup)] = 0
unstacked_occup <- subset( unstacked_occup, select = -WEEKEND )
unstacked_occup <- subset( unstacked_occup, select = -Date )
plot the clusters
cluster_occup <- dtwclust::tsclust(unstacked_occup, type = "h", k = 12L, distance = "dtw")
dev.off()
plot(cluster_occup, type="sc")
#dendrogram
hclus_occup <- stats::cutree(cluster_occup, k = 2) %>%
as.data.frame(.) %>%
dplyr::rename(.,cluster_group = .) %>%
tibble::rownames_to_column("type_col")
hcdata_occup <- ggdendro::dendro_data(cluster_occup)
names_order_occup <- hcdata_occup$labels$label
plot_occup <- hcdata_occup %>%
ggdendro::ggdendrogram(., rotate=FALSE, leaf_labels=FALSE)
gplot_occup<-ggplotGrob(plot_occup)
grid.arrange(gplot_occup, ncol=2, widths=c(4,0.001))
merge clusters into table occupied
occup_cluster <- cutree(cluster_occup, k=12L)
cluster_table_occup<-merge(agregated_data_occup[1:4],occup_cluster,by.x = "NAME", by.y = "row.names")
colnames(cluster_table_occup)<-c("NAME","Date", "WEEKEND","rolling_avg","Cluster")
head(cluster_table_occup)
write_xlsx (cluster_table_occup, "ALT Regions with clusters (12) - rollingavg .xlsx")