Compute variance explained by each Principal Component
We use the formula below to compute the total variance experienced by each PC.
Syntax: pca$sdev^2 / sum(pca$sdev^2)
Code:
R
# drop the species column as its character type num_iris = subset (iris, select = - c (Species) ) # compute pca pca <- prcomp (num_iris, scale = TRUE ) # compute total variance variance = pca$sdev^2 / sum (pca$sdev^2) variance |
Output:
[1] 0.729624454 0.228507618 0.036689219 0.005178709
Example 1: Plotting Scree plot with Line plot
R
library (ggplot2) # drop the species column as its character type num_iris = subset (iris, select = - c (Species) ) # compute pca pca <- prcomp (num_iris, scale = TRUE ) # compute total variance variance = pca $sdev^2 / sum (pca $sdev^2) # Scree plot qplot ( c (1:4), variance) + geom_line () + geom_point (size=4)+ xlab ( "Principal Component" ) + ylab ( "Variance Explained" ) + ggtitle ( "Scree Plot" ) + ylim (0, 1) |
Output:
Example2: Plotting Scree plot with barplot
R
library (ggplot2) # drop the species column as its character type num_iris = subset (iris, select = - c (Species) ) # compute pca pca <- prcomp (num_iris, scale = TRUE ) # compute total variance variance = pca $sdev^2 / sum (pca $sdev^2) # Scree plot qplot ( c (1:4), variance) + geom_col ()+ xlab ( "Principal Component" ) + ylab ( "Variance Explained" ) + ggtitle ( "Scree Plot" ) + ylim (0, 1) |
Output:
How To Make Scree Plot in R with ggplot2
In this article, we are going to see how can we plot a Scree plot in R Programming Language with ggplot2.