# Multiple Correspondence Analysis
# ++++++++++++++++++++++++++++++
# Install and load FactoMineR to compute MCA
# install.packages("FactoMineR")
library("FactoMineR")
data(poison)
poison.active <- poison[1:55, 5:15]
head(poison.active)
res.mca <- MCA(poison.active, graph=FALSE)
# Graph of individuals
# +++++++++++++++++++++
# Default Plot
# Color of individuals: col.ind = "steelblue"
fviz_mca_ind(res.mca, col.ind = "steelblue")
# 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_mca_ind(res.mca, col.ind = "cos2", repel = TRUE)+
theme_minimal()
## Not run:
# # You can also control the transparency
# # of the color by the cos2
# fviz_mca_ind(res.mca, alpha.ind="cos2") +
# theme_minimal()
# ## End(Not run)
# Color individuals by groups, add concentration ellipses
# Remove labels: label = "none".
grp <- as.factor(poison.active[, "Vomiting"])
p <- fviz_mca_ind(res.mca, label="none", habillage=grp,
addEllipses=TRUE, ellipse.level=0.95)
print(p)
# Change group colors using RColorBrewer color palettes
# Read more: http://www.sthda.com/english/wiki/ggplot2-colors
p + scale_color_brewer(palette="Dark2") +
scale_fill_brewer(palette="Dark2") +
theme_minimal()
# 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()
# 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 40 according to the cos2
fviz_mca_ind(res.mca, select.ind = list(cos2 = 20))
# Graph of variable categories
# ++++++++++++++++++++++++++++
# Default plot: use repel = TRUE to avoid overplotting
fviz_mca_var(res.mca, col.var = "#FC4E07")+
theme_minimal()
# Control variable colors using their contributions
# use repel = TRUE to avoid overplotting
fviz_mca_var(res.mca, col.var = "contrib")+
scale_color_gradient2(low="white", mid="blue",
high="red", midpoint=2, space = "Lab") +
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_mca_var(res.mca, select.var = list(contrib = 10))
# Biplot
# ++++++++++++++++++++++++++
grp <- as.factor(poison.active[, "Vomiting"])
fviz_mca_biplot(res.mca, repel = TRUE, col.var = "#E7B800",
habillage = grp, addEllipses = TRUE, ellipse.level = 0.95)+
theme_minimal()
## Not run:
# # Keep only the labels for variable categories:
# fviz_mca_biplot(res.mca, label ="var")
#
# # Keep only labels for individuals
# fviz_mca_biplot(res.mca, label ="ind")
#
# # Hide variable categories
# fviz_mca_biplot(res.mca, invisible ="var")
#
# # Hide individuals
# fviz_mca_biplot(res.mca, invisible ="ind")
#
# # Control automatically the color of individuals using the cos2
# fviz_mca_biplot(res.mca, label ="var", col.ind="cos2") +
# theme_minimal()
#
# # Change the color by groups, add ellipses
# fviz_mca_biplot(res.mca, label="var", col.var ="blue",
# habillage=grp, addEllipses=TRUE, ellipse.level=0.95) +
# theme_minimal()
#
# # Select the top 30 contributing individuals
# # And the top 10 variables
# fviz_mca_biplot(res.mca,
# select.ind = list(contrib = 30),
# select.var = list(contrib = 10))
# ## End(Not run)
Run the code above in your browser using DataLab