Adding ellipses to a principal component analysis (PCA) plot

后端 未结 3 1256
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 21:11

I am having trouble adding grouping variable ellipses on top of an individual site PCA factor plot which also includes PCA variable factor arrows.

My code:



        
3条回答
  •  攒了一身酷
    2020-12-02 21:39

    Since you do not mention this in your question, I will assume that the package you used is vegan, since it has the function rda() that accepts the scale=TRUE argument.

    Your initial plot() call was modified as some of variables are not given.

    library(vegan)
    prin_comp<-rda(data[,2:9], scale=TRUE)
    pca_scores<-scores(prin_comp)
    
    plot(pca_scores$sites[,1],
         pca_scores$sites[,2],
         pch=21,
         bg=as.numeric(data$Waterbody),
         xlim=c(-2,2), 
         ylim=c(-2,2))
    arrows(0,0,pca_scores$species[,1],pca_scores$species[,2],lwd=1,length=0.2)
    

    To make ellipses, function ordiellipse() of package vegan is used. As arguments PCA analysis object and grouping variable must be provided. To control number of points included in ellipse, argument conf= can be used.

    ordiellipse(prin_comp,data$Waterbody,conf=0.99)
    

    enter image description here

提交回复
热议问题