You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用FactoMineR与factoextra做PCA分析时ggarrange函数报错

解决ggarrange函数未找到及相关绘图组合问题

问题原因

ggarrange()函数不属于factoextra或FactoMineR包,它是ggpubr包中的工具,未加载对应包就会提示"函数未找到"。另外原代码还有两个细节问题:

  1. plot(res.pca, choix = "var")生成的是R基础绘图,不是ggplot对象,无法用ggarrange()组合
  2. 代码里的tittle是拼写错误,正确写法是title

解决步骤

  1. 安装并加载ggpubr包
  2. 替换载荷图生成代码为ggplot格式的函数
  3. 修正拼写错误

修正后的完整代码

library(factoextra)
library(FactoMineR)
library(ggpubr)  # 加载ggpubr包,提供ggarrange函数

# 执行PCA分析
res.pca = PCA(clin.oc[,1:14], graph = TRUE)

# 生成各ggplot格式的图
scree = fviz_screeplot(res.pca, ncp=5, addlabels=TRUE, size=0.5, title='Scree plot')
loadings = fviz_pca_var(res.pca, title='Loadings')  # 用fviz_pca_var生成ggplot格式的载荷图

n = fviz_contrib(res.pca, choice = "var", axes = 1, top = 5, title='Variable contributions')
scores = fviz_pca_ind(res.pca, label="none", habillage=as.factor(clin), title='scores')  # 修正tittle为title

# 组合所有图
ggarrange(scree, loadings, n, scores)

关键说明

  • fviz_pca_var()是factoextra包中生成ggplot格式变量载荷图的函数,替代基础绘图的plot()后,才能和其他fviz系列的ggplot图一起用ggarrange()组合
  • 若未安装ggpubr包,先运行install.packages("ggpubr")完成安装

内容的提问来源于stack exchange,提问作者Abdullah Jamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:00:34