PCA图_绘图(一)
使用的R包:FactoMineR factoextra
FactoMineR提供PCA算法以及(原始)结果;factoextra则extract and visualize the output
Principal Component Analysis (PCA), which is used to summarize the
information contained in a continuous (i.e, quantitative)
multivariate data by reducing the dimensionality of the data
without loosing important information.
library("FactoMineR")
library("factoextra")
data("decathlon2")
df <- decathlon2[1:23, 1:10] # loading data
pcaResult = PCA(df, graph = FALSE) # PCA来自FactoMineR,只要result而不graph
get_eig(pcaResult) # eigenvalue 特征值
# get_eigenvalue(pcaResult)
# get_pca(pcaResult)
# fviz_screeplot(pcaResult, addlabels = TRUE, ylim = c(0, 50))
var = get_pca_var(pcaResult)
# var
# head(var$coord)
# head(var$contrib)
fviz_pca_var(pcaResult, col.var = "black")
# Control variable colors using their contributions
fviz_pca_var(pcaResult, col.var="contrib",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE # Avoid text overlapping
)
ind = get_pca_ind(pcaResult)
# ind
# head(ind$coord)
fviz_pca_ind(pcaResult, col.ind = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE # Avoid text overlapping (slow if many points)
)
# Biplot of individuals and variables
fviz_pca_biplot(pcaResult, repel = TRUE)
所以该怎么理解variables和individuals…
换熟悉的iris数据集看看
# Compute PCA on the iris data set
# The variable Species (index = 5) is removed
# before PCA analysis
iris.pca <- PCA(iris[,-5], graph = FALSE)
# Visualize
# Use habillage to specify groups for coloring
fviz_pca_ind(iris.pca,
label = "none", # hide individual labels
habillage = iris$Species, # color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
addEllipses = TRUE # Concentration ellipses
)
参考资料
FactoMineR: Exploratory Multivariate Data Analysis with R
factoextra : Extract and Visualize the Results of Multivariate Data Analyses
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 易语言 —— 开山篇