I want to add legend in this dendrogram related to cluster #

I am using this code but but it not showing legend and also it did not gave any warning or error also.
dend <- fviz_dend(hc1, k = 3,
k_colors = "aaas",
color_labels_by_k = T,
rect = TRUE,
rect_border = "aaas" ,
rect_fill = TRUE,horiz = T,
cex = 0.8, lwd=1)+ labs(title = "")+
theme(panel.background = element_rect(colour = "black"),
axis.text.y =element_text(colour="black", size = 10, face = "bold"),
axis.text.x =element_text(colour="black", size = 10, face = "bold"),
axis.ticks = element_line(colour = "black"),
axis.ticks.y = element_line(colour = "black"),
panel.grid.minor = element_line())+
guides(fill=guide_legend(title = "Cluster #"))
dend <- dend+
scale_fill_manual(values = c("I" = "blue", "II" = "darkgreen", "III" = "red"))
We don't have your data, so we can use the built-in
USArrestsdata set:When we run your plot code on this, we get the following dendrogram:
Although the result is a ggplot object, the fill color of the rectangles is not mapped to a variable. Instead, the colors are are specified directly as aesthetic parameters, so there is no legend generated. Getting round this requires setting an aesthetic mapping in the
geom_rectlayer and removing thefillfrom the aesthetic parameters of the layer, all of which is a bit complex, but would look like this:Now we can do: