The example is split into 2 Parts:
Part 1 must be completed first to create a file,
SNPs_cleaned.csv, that has been completely prepared for
analysis.
Now in Part 2, you will analyze the data with PCA. The steps here will be:
scale())prcomp())screeplot())vegan::scores())In the code below all code is provided. Your tasks will be to do 4 things:
Load the vcfR package with library()
library(vcfR) # KEY
##
## ***** *** vcfR *** *****
## This is vcfR 1.13.0
## browseVignettes('vcfR') # Documentation
## citation('vcfR') # Citation
## ***** ***** ***** *****
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.6-4
library(ggplot2)
library(ggpubr)
Set the working directory
Load the data
SNPs_cleaned <- read.csv(file = "SNPs_cleaned.csv")
warning("If this didn't work, its may be because you didn't set your working directory.")
## Warning: If this didn't work, its may be because you didn't set your working
## directory.
TODO: To do PCA, we scale our data using scale(). It allows us to compare data that is not measured the same way.
SNPs_scaled <- scale(SNPs_cleaned)
TODO: Use prcomp() to run PCA. The results will help us visualize variation in our dataset.
pca_scaled <- prcomp(SNPs_scaled)
TODO: Use screeplot() to make a plot that tells us which features are important.
TODO: UPDATE PLOT WITH TITLE(“my_pca”)
screeplot(pca_scaled,
ylab = "Relative importance",
main = "give me a basic title")
TODO: The screeplot tells us that PC1 is the most important. The rest are significantly less important.
TODO: Use summary() to view a summary of the importance of components. Use the function below to find the variation in percentage. Make sure to add the return function. Assign to a new object. Use barplot() to visualize the data in a graph.
summary_out_scaled <- summary(pca_scaled)
PCA_variation <- function(pca_summary, PCs = 2){
var_explained <- pca_summary$importance[2,1:PCs]*100
var_explained <- round(var_explained,1)
return(var_explained)
}
var_out <- PCA_variation(summary_out_scaled,PCs = 10)
N_columns <- ncol(SNPs_scaled)
barplot(var_out,
main = "Percent variation Scree plot",
ylab = "Percent variation explained")
abline(h = 1/N_columns*100, col = 2, lwd = 2)
TODO: The variance explained show us the rotation of graphs and correlation.
TODO: Use biplot() to create a plot that shows the principal components and loading vectors in one plot.
biplot(pca_scaled)
TODO: Uing a biplot is a bad idea because there are many vectors and this causes the plot to be difficult to analyze.
TODO: Extract scores to plot using the vegan function scores(). This will scale and transform the data as well.
pca_scores <- vegan::scores(pca_scaled)
TODO: Put population IDs into a vector using c().
pop_id <- c("Nel","Nel","Nel","Nel","Nel","Nel","Nel","Nel",
"Nel", "Nel", "Nel", "Nel", "Nel", "Nel", "Nel", "Alt",
"Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Alt",
"Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Sub", "Sub",
"Sub", "Sub", "Sub", "Sub", "Sub", "Sub", "Sub", "Sub",
"Sub", "Cau", "Cau", "Cau", "Cau", "Cau", "Cau", "Cau",
"Cau", "Cau", "Cau", "Cau", "Cau", "Div", "Div", "Div",
"Div", "Div", "Div", "Div", "Div", "Div", "Div", "Div",
"Div", "Div", "Div", "Div")
TODO: Put the PCA scores and population IDs into a data frame using data.frame(). Assign it to a new object. This helps us compare the two sets of data.
pca_scores2 <- data.frame(pop_id,
pca_scores)
TODO: Use ggscatter() from the ggpubr package to create a scatterplot of PC1, PC2, and population ID data.
TODO: UPDATE X and Y AXES WITH AMOUNT OF VARIATION EXPLAINED (19.9, 2.3)
ggpubr::ggscatter(data = pca_scores2,
y = "PC2",
x = "PC1",
color = "pop_id",
shape = "pop_id",
xlab = "PC1 (19.9% variation)",
ylab = "PC2 (2.3% variation)",
main = "Percent Variation Scatterplot")
TODO: The amount of variation each PC is responsible for is shown on the axes. The plot shows us that categories Alt and Nel have a correlation. Categories Cau and Dic also have a correlation.