K-Science.info

K-Science.info

Principal Components Analysis:

Stacks Image 8988

Running a PCA in R

 D1<-c(arida_PCAoutline$PC1,arida_PCAoutline$PC2,arida_PCAoutline$PC3)
 D2<-c(arida_PCAlandmarks$PC1,arida_PCAlandmarks$PC1,arida_PCAlandmarks$PC1)
 
 
 
 mat.D1<-matrix(D1, nrow = length(D1)/3, ncol= 3)
 mat.D2<-matrix(D2, nrow = length(D2)/3, ncol= 3)

 abs.mat.D1<-max(abs(D1))
 abs.mat.D2<-max(abs(D1))
 
 rel.mat.D1<-(mat.D1/abs.mat.D1)
 rel.mat.D2<-(mat.D2/abs.mat.D2)
 
 
 Specimen_name<-c(arida_PCAoutline$specimen_name,arida_PCAlandmarks$specimen_name)
 PC1<-c(rel.mat.D1[,1],rel.mat.D2[,1])
 PC2<-c(rel.mat.D1[,2],rel.mat.D2[,2])
 PC3<-c(rel.mat.D1[,3],rel.mat.D2[,3])
 
 B<-cbind(Specimen_name,PC1,PC2,PC3)
 
 
 ggplot(B,aes(x=PC1,y=PC2,color=Specimen_name))+
     geom_point(size=5)+
     ggtitle("Passiflora Data PCA")+
     scale_x_continuous(name="PCA Axis 1 ", ) +
     scale_y_continuous(name="PCA Axis 2 ", )+
     theme(plot.title = element_text(size = 40, face = "bold")) +
     theme(axis.title.y = element_text(size = 30, angle = 90)) +
     theme(axis.title.x = element_text(size = 30, angle = 360)) +
     theme(legend.title = element_text(size=20, face = "bold")) +
     theme(legend.text = element_text(size=20, face = "italic")) +
     theme(plot.title = element_text(hjust = 0.5))+
labs(colour = "Data Indicator")
Stacks Image 6175
Stacks Image 13909
Stacks Image 6130
Stacks Image 13911
Use Princomp function to generate PC1, PC2, PC3, etc. Note that the below figure shows the summary of each component and not the scores. When plotted the scores create the graphic. We will utilize ggplot.
Stacks Image 13917
ggplot(data,aes(x=PC2,y=PC3,color=Name))+
geom_point(size=5)+
ggtitle("PCA of Gulf White Shrimp Genetic Distances")+
scale_x_continuous(name="PCA Axis 2 -11% Variation", ) +
scale_y_continuous(name="PCA Axis 3 -9% Variation", )+
theme(plot.title = element_text(size = 40, face = "bold")) +
theme(axis.title.y = element_text(size = 30, angle = 90)) +
theme(axis.title.x = element_text(size = 30, angle = 360)) +
theme(legend.title = element_text(size=20, face = "bold")) +
theme(legend.text = element_text(size=20, face = "italic")) +
theme(plot.title = element_text(hjust = 0.5))+
labs(colour = "Name")


Stacks Image 13913
Stacks Image 13915