# Principal Component Analysis
# ++++++++++++++++++++++++++
data(iris)
res.pca <- prcomp(iris[, -5], scale = TRUE)
# Extract eigenvalues/variances
get_eig(res.pca)
# Default plot
fviz_eig(res.pca)
# Customize the plot
# - Add labels
# - Change line color, bar fill and color.
# - Change axis limits and themes
p <- fviz_eig(res.pca, addlabels = TRUE, hjust = -0.3,
linecolor = "#FC4E07",
barfill="white", barcolor ="darkblue")+
ylim(0, 85)+ # y axis limits
theme_minimal() # themes: http://www.sthda.com/english/wiki/ggplot2-themes
print(p)
# Change plot title and axis labels
p + labs(title = "Variances - PCA",
x = "Principal Components", y = "% of variances")
# Scree plot - Eigenvalues
fviz_eig(res.pca, choice = "eigenvalue", addlabels=TRUE)
# Use only bar or line plot: geom = "bar" or geom = "line"
fviz_eig(res.pca, geom="line")
# Correspondence Analysis
# +++++++++++++++++++++++++++++++++
library(FactoMineR)
data(housetasks)
res.ca <- CA(housetasks, graph = FALSE)
get_eig(res.ca)
fviz_eig(res.ca, linecolor = "#FC4E07",
barcolor = "#00AFBB", barfill = "#00AFBB")+
theme_minimal()
# Multiple Correspondence Analysis
# +++++++++++++++++++++++++++++++++
library(FactoMineR)
data(poison)
res.mca <- MCA(poison, quanti.sup = 1:2,
quali.sup = 3:4, graph=FALSE)
get_eig(res.mca)
fviz_eig(res.mca, linecolor = "#FC4E07",
barcolor = "#2E9FDF", barfill = "#2E9FDF")+
theme_minimal()
# Multiple Factor Analysis
# +++++++++++++++++++++++++++++++++
library(FactoMineR)
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)
get_eig(res.mfa)
fviz_eig(res.mfa, linecolor = "#FC4E07",
barcolor = "#E7B800", barfill = "#E7B800")+
theme_minimal()
Run the code above in your browser using DataLab