rggplot2visualizationfactoextrafactominer

how to add legend in `fviz_dend`


I want to add legend in this dendrogram related to cluster # enter image description here

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"))

Solution

  • We don't have your data, so we can use the built-in USArrests data set:

    library(factoextra)
    
    data(USArrests)
    hc1 <- hclust(dist(scale(USArrests)))
    

    When we run your plot code on this, we get the following dendrogram:

    dend
    

    enter image description here

    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_rect layer and removing the fill from the aesthetic parameters of the layer, all of which is a bit complex, but would look like this:

    library(rlang)
    
    params <- dend$layers[[4]]$aes_params
    dend$layers[[4]]$aes_params <- params[!names(params) %in% c('fill', 'colour')]
    dend$layers[[4]]$mapping$fill <- quo_set_env(quo(cluster), 
                                      quo_get_env(dend$layers[[4]]$mapping$xmin))
    

    Now we can do:

    dend +
      scale_fill_manual(values = c(c1 = "blue", c3 = "darkgreen", c2 = "red"))
    

    enter image description here