kassambara / factoextra

Extract and Visualize the Results of Multivariate Data Analyses
http://www.sthda.com/english/rpkgs/factoextra
352 stars 102 forks source link

Clusters colors not matching between fviz_cluster, fviz_dend, fviz_silhouette #103

Open majpark21 opened 4 years ago

majpark21 commented 4 years ago

I'm reporting 3 different representations for my clustering: colored dendrogram, PCA scatterplot and silhouette thanks to fviz_cluster, fviz_dend and fviz_silhouette respectively. I'm facing the following issue: the clusters aren't colored with the same colors across the plots. More specifically, it seems that the both the PCA and the silhouette plots follow the same color-cluster mapping, but not the dendrogram.

I've been digging in the source code a bit, appears that the dendrogram plot is produced by dendextend while the others by ggplot. I guess the difference of mapping originates from there.

I perform my cut using the hcut function, the same hcut object is then passed to the three visualization functions.

Here is an example screenshot of my output: image

As you can see, the smaller cluster is colored red in the dendrogram and blue in the silhouette and scatterplot.

GuillaumeCen commented 4 years ago

I have the same problem.

The solution is to give color in the good order in the dendrogram with the option palette and rect_border.

Before modification :

fviz_dend(res.hcpc, cex = 0.7, palette = "jco", rect = TRUE, rect_fill = TRUE, rect_border = "jco", labels_track_height = 1.8 ) fviz_cluster(res.hcpc, repel = TRUE, show.clust.cent = TRUE, palette = "jco", ggtheme = theme_minimal(), main = "Factor map")

cluster_et_factor_map_sur_donnees_filtrees_bad

After modification 👍 fviz_dend(res.hcpc, cex = 0.7, palette = c("#86868699", "#0073C299", "#EFC00099"), rect = TRUE, rect_fill = TRUE, rect_border = c("#86868699", "#0073C299", "#EFC00099"), labels_track_height = 1.8 ) fviz_cluster(res.hcpc, repel = TRUE, show.clust.cent = TRUE, palette = "jco", ggtheme = theme_minimal(), main = "Factor map") cluster_et_factor_map_sur_donnees_filtrees_good

majpark21 commented 4 years ago

Thanks! I actually arrived to a similar solution back then. Results of my inspections were that fviz_dend colors clusters "from left to right", whereas fviz_silhouette and fviz_cluster use the order of cluster first occurence in the list of individuals.

mluerig commented 2 years ago

here is a generic method to achieve this: https://stackoverflow.com/a/69575597/5238559