• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      未分类

      • 首页
      • 博客
      • 未分类
      • ggplot2|从0开始绘制PCA图

      ggplot2|从0开始绘制PCA图

      • 发布者 一览
      • 分类 未分类
      • 日期 2020年3月8日
      • 评论 0评论

      参考:https://mp.weixin.qq.com/s?__biz=MzIyNDI1MzgzOQ==&mid=2650393990&idx=1&sn=80c1191613970739cdc9f29cfc86d88f&chksm=f01caf66c76b2670fb6c779cb3c71eb9c40d2816c3f5cc2a070bdb2127de3153d962048cd9dc&scene=21#wechat_redirect

      PCA(Principal Component Analysis),即主成分分析方法,是一种使用最广泛的数据降维算法。在数据分析以及生信分析中会经常用到。

      本文利用R语言的ggplot2包,从头带您绘制可发表级别的主成分分析图。

      一 载入数据集和R包

      
      library(ggplot2)
      #使用经典iris数据集
      df <- iris[c(1, 2, 3, 4)]
      head(df)
        Sepal.Length Sepal.Width Petal.Length Petal.Width
      1          5.1         3.5          1.4         0.2
      2          4.9         3.0          1.4         0.2
      3          4.7         3.2          1.3         0.2
      4          4.6         3.1          1.5         0.2
      5          5.0         3.6          1.4         0.2
      6          5.4         3.9          1.7         0.4

      二 进行主成分分析

      
      df_pca <- prcomp(df) #计算主成分
      df_pcs <-data.frame(df_pca$x, Species = iris$Species)  
      head(df_pcs,3)  #查看主成分结果
              PC1        PC2         PC3          PC4 Species
      1 -2.684126 -0.3193972  0.02791483  0.002262437  setosa
      2 -2.714142  0.1770012  0.21046427  0.099026550  setosa
      3 -2.888991  0.1449494 -0.01790026  0.019968390  setosa

      三 绘图展示

      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 
                           PC1         PC2         PC3        PC4      feature
      Sepal.Length  0.36138659 -0.65658877  0.58202985  0.3154872 Sepal.Length
      Sepal.Width  -0.08452251 -0.73016143 -0.59791083 -0.3197231  Sepal.Width
      Petal.Length  0.85667061  0.17337266 -0.07623608 -0.4798390 Petal.Length
      Petal.Width   0.35828920  0.07548102 -0.54583143  0.7536574  Petal.Width

      贡献度绘图

      
      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()
      

      好了  ,更改数据集即可以自己动手绘制PCA了,生信分析得到的PCA的结果直接绘制即可。

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      一览

      上一篇文章

      ggplot2|绘制GO富集柱形图
      2020年3月8日

      下一篇文章

      Bio|manhattan图
      2020年3月8日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      8-1678501786
      肿瘤细胞通过改变CD8+ T细胞中的丙酮酸利用和琥珀酸信号来调控抗肿瘤免疫应答。
      7 12月, 2022

      留言 取消回复

      要发表评论,您必须先登录。

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?