factoextra icon indicating copy to clipboard operation
factoextra copied to clipboard

fviz_mca_var: how to colour the variables differently ?

Open Corataz opened this issue 6 years ago • 1 comments

Hello, Thank you for this great package, easy to understand and to handle :)

I have an issue : I would like to colour differently the 12 active variables I used for my analysis. All the examples I found were about colouring the variables according to the variable contribution or cos2, but not having different colors for the different variable names.

My data set : str(data) 'data.frame': 87 obs. of 20 variables: $ An_publi : int 2006 2006 2007 2007 2007 2007 2007 2008 2007 2008 ... $ Pays : Factor w/ 4 levels "Afrique","Amérique du Nord",..: 2 2 4 4 4 4 4 2 4 4 ... $ Ocean : Factor w/ 4 levels "Mer méditerrannée",..: 2 2 1 1 1 1 1 2 1 1 ... $ Pop_type : Factor w/ 4 levels "écloserie","élevage",..: 2 2 2 4 2 3 2 2 4 2 ... $ Espece : Factor w/ 2 levels "Mytilus edulis",..: 1 1 2 2 2 2 2 2 2 2 ... $ Type_etude : Factor w/ 2 levels "expérimentation",..: 2 2 2 1 2 1 1 1 1 1 ... $ Animal : Factor w/ 2 levels "non","oui": 1 2 2 1 1 1 1 1 1 1 ... $ Polluants : Factor w/ 2 levels "non","oui": 1 1 1 2 1 1 2 2 2 1 ... $ Climat : Factor w/ 2 levels "non","oui": 1 1 1 1 1 1 1 1 1 2 ... $ Site : Factor w/ 2 levels "non","oui": 2 2 1 2 1 1 1 1 1 1 ... $ EDM : Factor w/ 2 levels "non","oui": 1 1 1 1 1 2 1 1 1 2 ... $ O_pathogenes : Factor w/ 2 levels "non","oui": 1 1 1 1 1 1 1 1 1 1 ... $ Pratiques : Factor w/ 2 levels "non","oui": 2 1 1 1 2 1 1 1 1 1 ... $ Animal_FR : Factor w/ 2 levels "0","1": NA 1 1 NA NA NA NA NA NA NA ... $ Polluants_FR : Factor w/ 2 levels "0","1": NA NA NA 1 NA NA 2 2 1 NA ... $ Climat_FR : Factor w/ 1 level "1": NA NA NA NA NA NA NA NA NA 1 ... $ Site_FR : Factor w/ 2 levels "0","1": 2 2 NA 2 NA NA NA NA NA NA ... $ EDM_FR : Factor w/ 2 levels "0","1": NA NA NA NA NA 2 NA NA NA 2 ... $ O_pathogenes_FR: Factor w/ 2 levels "0","1": NA NA NA NA NA NA NA NA NA NA ... $ Pratiques_FR : Factor w/ 2 levels "0","1": 2 NA NA NA 2 NA NA NA NA NA ...

I have 12 active variables, 1 quantitative supplementary and 7 qualitative supplementary :grinning: res.mca<-MCA(data, quanti.sup=1, quali.sup=c(14:20), graph=FALSE)

I tried : couleur=rainbow(12, alpha=0.8) # a vector for a different colour for each active cariable fviz_mca_var(res.mca, col.var = couleur, repel = TRUE) + theme_minimal()

but here is the error message : Error in fviz(X, element = choice, axes = axes, geom = geom.var, color = col.var, : The length of color variableshould be the same as the number of rows in the data.

Why the number of "rows" ?

May you help me ?

Thank you in advance for your help, Kind regards, Coralie

Corataz avatar Apr 26 '18 13:04 Corataz

@Corataz use dput to post the data. See this post on how to use dput. Also see this post on how to create a minimum complete verifiable example for others to replicate your problem.

duttashi avatar Jul 31 '18 17:07 duttashi