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