pangxie的个人博客分享 http://blog.sciencenet.cn/u/pangxie

博文

鸢尾花PCA分析与绘图

已有 2725 次阅读 2021-2-6 13:33 |个人分类:R语言|系统分类:科研笔记

参考https://www.plob.org/article/22240.html

一 载入数据集和R包

library(ggplot2)

#使用经典iris数据集

df <- iris[c(1, 2, 3, 4)]

head(df)

#二 进行主成分分析

df_pca <- prcomp(df) #计算主成分

df_pcs <-data.frame(df_pca$x, Species = iris$Species)  

head(df_pcs,3)  #查看主成分结果

#3.1 基础函数绘制PCA图

plot(df_pca$x[,1], df_pca$x[,2])

#3.2 ggplot2 绘制PCA图

#1) Species分颜色

ggplot(df_pcs,aes(x=PC1,y=PC2,color=Species))+ geom_point()

#2)去掉背景及网格线

ggplot(df_pcs,aes(x=PC1,y=PC2,color=Species))+ 

  geom_point()+ 

  theme_bw() +

  theme(panel.border=element_blank(),

        panel.grid.major=element_blank(),

        panel.grid.minor=element_blank(),

        axis.line= element_line(colour = "black"))

#3) 添加PC1  PC2的百分比

percentage<-round(df_pca$sdev / sum(df_pca$sdev) * 100,2)

percentage<-paste(colnames(df_pcs),"(",

                  paste(as.character(percentage), "%", ")", sep=""))

ggplot(df_pcs,aes(x=PC1,y=PC2,color=Species))+

  geom_point()+ 

  xlab(percentage[1]) +

  ylab(percentage[2])

#4) 添加置信椭圆

ggplot(df_pcs,aes(x=PC1,y=PC2,color = Species))+ 

  geom_point()+stat_ellipse(level = 0.95, show.legend = F) + 

  annotate('text', label = 'setosa', x = -2, y = -1.25, 

           size = 5, colour = '#f8766d') +

  annotate('text', label = 'versicolor', x = 0, y = - 0.5, 

           size = 5, colour = '#00ba38') +

  annotate('text', label = 'virginica', x = 3, y = 0.5,

           size = 5, colour = '#619cff')

#5) 查看各变量对于PCA的贡献

df_r <- as.data.frame(df_pca$rotation)

df_r$feature <- row.names(df_r)

df_r 

#贡献度绘图

ggplot(df_r,aes(x=PC1,y=PC2,label=feature,color=feature )) + 

  geom_point()+ geom_text(size=3)

#四 PCA绘图汇总展示

ggplot(df_pcs,aes(x=PC1,y=PC2,color=Species )) +

  geom_point()+xlab(percentage[1]) +

  ylab(percentage[2]) + 

  stat_ellipse(level = 0.95, show.legend = F) +

  annotate('text', label = 'setosa', x = -2, y = -1.25,

           size = 5, colour = '#f8766d') +

  annotate('text', label = 'versicolor', x = 0, y = - 0.5, 

           size = 5, colour = '#00ba38') +

  annotate('text', label = 'virginica', x = 3, y = 0.5, 

           size = 5, colour = '#619cff') +

  labs(title="Iris PCA Clustering", 

      subtitle=" PC1 and PC2 principal components ",    

      caption="Source: Iris") + theme_classic()


Rplot.png




https://m.sciencenet.cn/blog-3448646-1270918.html

上一篇:微生物菌群CDA与RDA分析
下一篇:alpha多样性指数绘图

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-24 03:31

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部