【R语言】factoextra包绘制PCA主成分分析图

PCA主成分分析绘图

1.加载安装包

这里要用到三个包:“ggplot2”,“factoextra”,“FactoMineR”

> remove(list = ls()) #清除工作空间对象
> library(ggplot2)
> library(factoextra)
> library(FactoMineR)
2.导入数据
> df1 <- read.csv("C:/Users/luokai/Desktop/PCA1.csv",header = F,row.names = 1)
> head(df1)
                          V2          V3          V4          V5          V6          V7          V8          V9         V10
group                     S1          S1          S1          S2          S2          S2          S3          S3          S3
Lysobacter       15.67017534 13.61251717 15.31950199 0.620339447 1.028696844 0.931545775 9.590293125 12.01051385 13.18866976
Sphingopyxis     1.421585551 2.066318096  1.89594359 1.184185059 2.530712182 2.001358393 19.49599794 11.86851337 5.062409126
Luteimonas       10.25218367 5.792161251  7.84162888 0.188194401  0.25226339 0.301435814 4.858893936 6.421368599 7.970115437
Sphingomonas     1.763317345  2.20514914 1.944134178 3.473059503 8.116277912 7.852562178 3.111211348 5.399206852 4.232195358
Stenotrophomonas 5.698662704 4.113037875 4.859814216 0.565634948 0.714519085 0.692012891 3.851443855 4.370827154  4.76256787

这里我的数据有三类(group): S1,S2和S3,每类3个数据。

3.数据集处理
> df1 <- as.data.frame(t(df1)) #转置
> df2 <- subset(df1, select = -group)  #这里把表示属性的group列删了,不然待会儿PCA分析会报错。
4.PCA分析
> df2.pca <- PCA(df2,scale.unit = T,graph = F)
Error in PCA(df2, scale.unit = T, graph = F) : 
The following variables are not quantitative:  Lysobacter
The following variables are not quantitative:  Sphingopyxis
The following variables are not quantitative:  Luteimonas
The following variables are not quantitative:  Sphingomonas
The following variables are not quantitative:  Stenotrophomonas
The following variables are not quantitative:  Xanthomonas

使用的是"FactoMineR"包的PCA命令来进行主成分分析。
这里报错,检查发现数据集df2中的数据都是character而非numeric,所以在PCA分析时会出现错误,运用以下命令转换一下数据类型再分析就可以了。

> df2[,1:1761] <- as.numeric(unlist(df2[,1:1761])) #此数据集有1761列,都转化成numeric
> df2.pca <- PCA(df2,scale.unit = T,graph = F) #不要生成图片
5.作图
> fviz_pca_ind(df2.pca,
             geom.ind = "point", #只显示点
             mean.point=F, #这个设成True作出的图上会多出一个较大的中心点
             pointsize =5,
             pointshape = 21,
             fill.ind = df1$group, #根据类别着色
             palette = c("#894329","#12727D","#FCB886"), #颜色,我这里有三组
             legend.title = "Groups",
             title="") +
  theme_bw() 

图片导出:主成分分析

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
R语言中,你可以使用`prcomp()`函数进行主成分分析(PCA)并绘制PCA的结果。 首先,确保你安装了需要的,比如`ggplot2`和`prcomp`。如果没有安装,可以使用以下命令安装: ```R install.packages("ggplot2") install.packages("prcomp") ``` 然后,加载这些: ```R library(ggplot2) library(prcomp) ``` 接下来,准备你的数据集。假设你的数据集名为`data`,其中含需要进行PCA的数值变量。 使用`prcomp()`函数进行主成分分析并将结果保存在一个对象中: ```R pca <- prcomp(data, scale.=TRUE) ``` 在这个例子中,我们使用了`scale.=TRUE`参数对数据进行标准化。 然后,可以绘制PCA结果。你可以选择绘制累积方差贡献率或者散点绘制累积方差贡献率: ```R variances <- pca$sdev^2 cumulative_variances <- cumsum(variances) / sum(variances) df_variance <- data.frame(PrincipalComponent = 1:length(pca$sdev), CumulativeVariance = cumulative_variances) ggplot(df_variance, aes(x = PrincipalComponent, y = CumulativeVariance)) + geom_line() + xlab("Principal Component") + ylab("Cumulative Variance") + ggtitle("Cumulative Variance Explained by Principal Components") ``` 绘制散点: ```R df_pca <- data.frame(PrincipalComponent1 = pca$x[, 1], PrincipalComponent2 = pca$x[, 2]) ggplot(df_pca, aes(x = PrincipalComponent1, y = PrincipalComponent2)) + geom_point() + xlab("PC1") + ylab("PC2") + ggtitle("PCA Scatter Plot") ``` 这样,你就可以根据你的数据绘制PCA主成分分析了。记得替换`data`为你自己的数据集。如果你有更多的主成分需要绘制,可以相应地调整代码。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值