美文网首页生信小白R语言R 数据可视化
ggplot2|从0开始绘制发表级PCA图

ggplot2|从0开始绘制发表级PCA图

作者: 生信补给站 | 来源:发表于2019-07-24 22:37 被阅读38次

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

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

    一 载入数据集和R包

    library(ggplot2)

    #使用经典iris数据集

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

    二 进行主成分分析

    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)

    贡献度绘图

    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的结果直接绘制即可。

    原链接:https://mp.weixin.qq.com/s/1f-0DdRH7WU2hAbGRqrECg

    更多关于生信 ,R ,Python的内容请扫码关注小号,谢谢。

    相关文章

      网友评论

        本文标题:ggplot2|从0开始绘制发表级PCA图

        本文链接:https://www.haomeiwen.com/subject/zqdqrctx.html