# Multiple Factor Analysis
# ++++++++++++++++++++++++
# Install and load FactoMineR to compute MFA
# install.packages("FactoMineR")
library("FactoMineR")
data(poison)
res.mfa <- MFA(poison, group=c(2,2,5,6), type=c("s","n","n","n"),
name.group=c("desc","desc2","symptom","eat"),
num.group.sup=1:2, graph=FALSE)
# Graph of individuals
# ++++++++++++++++++++
# Default plot
# Use repel = TRUE to avoid overplotting (slow if many points)
# Color of individuals: col.ind = "#2E9FDF"
fviz_mfa_ind(res.mfa, repel = TRUE, col.ind = "#2E9FDF")+
theme_minimal()
## Not run:
# # 1. Control automatically the color of individuals
# # using the "cos2" or the contributions "contrib"
# # cos2 = the quality of the individuals on the factor map
# # 2. To keep only point or text use geom = "point" or geom = "text".
# # 3. Change themes: http://www.sthda.com/english/wiki/ggplot2-themes
#
# fviz_mfa_ind(res.mfa, col.ind = "cos2")+
# theme_minimal()
#
# # Change gradient color
# # Use repel = TRUE to avoid overplotting (slow if many points)
# fviz_mfa_ind(res.mfa, col.ind="cos2", repel = TRUE) +
# scale_color_gradient2(low = "white", mid = "#2E9FDF",
# high= "#FC4E07", midpoint=0.4, space = "Lab")+
# theme_minimal()
# ## End(Not run)
# Color individuals by groups, add concentration ellipses
# Remove labels: label = "none".
grp <- as.factor(poison[, "Vomiting"])
p <- fviz_mfa_ind(res.mfa, label="none", habillage=grp,
addEllipses=TRUE, ellipse.level=0.95)
print(p)
## Not run:
# # Change group colors using RColorBrewer color palettes
# # Read more: http://www.sthda.com/english/wiki/ggplot2-colors
# p + scale_color_brewer(palette="Paired") +
# scale_fill_brewer(palette="Paired") +
# theme_minimal()
# ## End(Not run)
# Change group colors manually
# Read more: http://www.sthda.com/english/wiki/ggplot2-colors
p + scale_color_manual(values=c("#999999", "#E69F00"))+
scale_fill_manual(values=c("#999999", "#E69F00"))+
theme_minimal()
## Not run:
# # Select and visualize some individuals (ind) with select.ind argument.
# # - ind with cos2 >= 0.4: select.ind = list(cos2 = 0.4)
# # - Top 20 ind according to the cos2: select.ind = list(cos2 = 20)
# # - Top 20 contributing individuals: select.ind = list(contrib = 20)
# # - Select ind by names: select.ind = list(name = c("44", "38", "53", "39") )
#
# # Example: Select the top 20 according to the cos2
# fviz_mfa_ind(res.mfa, select.ind = list(cos2 = 20))
# ## End(Not run)
# Graph of qantitative variable categories
# ++++++++++++++++++++++++++++++++++++++++
data(wine)
res.mfa <- MFA(wine, group=c(2,5,3,10,9,2), type=c("n",rep("s",5)),
ncp=5, name.group=c("orig","olf","vis","olfag","gust","ens"),
num.group.sup=c(1,6), graph=FALSE)
# Default plot
fviz_mfa_quanti_var(res.mfa, col.var = "#FC4E07")+
theme_minimal()
## Not run:
# # Control variable colors using their contributions
# fviz_mfa_quanti_var(res.mfa, col.var = "contrib")+
# scale_color_gradient2(low = "white", mid = "blue",
# high = "red", midpoint = 20) +
# theme_minimal()
#
# # Select variables with select.var argument
# # You can select by contrib, cos2 and name
# # as previously described for ind
# # Select the top 10 contributing variables
# fviz_mfa_quanti_var(res.mfa, select.var = list(contrib = 10))
# ## End(Not run)
# Graph of categorical variable categories
# ++++++++++++++++++++++++++++++++++++++++
data(poison)
res.mfa <- MFA(poison, group=c(2,2,5,6), type=c("s","n","n","n"),
name.group=c("desc","desc2","symptom","eat"),
num.group.sup=1:2, graph=FALSE)
# Plot
# Control variable colors using their contributions
fviz_mfa_quali_var(res.mfa, col.var = "contrib")+
scale_color_gradient2(low = "white", mid = "blue",
high = "red", midpoint = 2) +
theme_minimal()
## Not run:
# # Select the top 10 contributing variable categories
# fviz_mfa_quali_var(res.mfa, select.var = list(contrib = 10))
# ## End(Not run)
# Biplot of categorical variable categories and individuals
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Use repel = TRUE to avoid overplotting
grp <- as.factor(poison[, "Vomiting"])
fviz_mfa_quali_biplot(res.mfa, repel = FALSE, col.var = "#E7B800",
habillage = grp, addEllipses = TRUE, ellipse.level = 0.95)+
theme_minimal()
# Graph of partial individuals (starplot)
# +++++++++++++++++++++++++++++++++++++++
fviz_mfa_ind_starplot(res.mfa, col.partial = "group.name")+
scale_color_brewer(palette = "Dark2")+
theme_minimal()
## Not run:
# # Select the partial points of the top 5
# # contributing individuals
# fviz_mfa_ind_starplot(res.mfa,
# select.partial = list(contrib = 2)) +
# theme_minimal()
#
# # Change colours of star segments
# fviz_mfa_ind_starplot(res.mfa, select.partial = list(contrib = 5),
# col.partial = "group.name") +
# scale_color_brewer(palette = "Dark2") +
# theme_minimal()
# ## End(Not run)
# Graph of groups (correlation square)
# ++++++++++++++++++++++++++++++++++++
fviz_mfa_group(res.mfa)
#' # Graph of partial axes
# ++++++++++++++++++++++++
fviz_mfa_axes(res.mfa)
Run the code above in your browser using DataLab