Autoplot of PCA in R (Example)
The table of content is structured as shown in the following box:
Let’s start right away.
Load Libraries and Create Sample Data Set
If you have already installed these libraries in the past, just ignore this first step. Then skip to the following:
Next, we will call our sample data, which is a built-in dataset called iris in R.
data(iris) head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Now, let’s perform our PCA using the sample data frame.
In order to perform a PCA in R, we will choose all the columns except for the Species column since it is categorical. Hence, the first step is subsetting the dataset. For PCA designed for categorical variables, see our tutorial: Can PCA be Used for Categorical Variables?.
data <- iris[,1:4] iris_pca <- prcomp(data, scale=TRUE) summary(iris_pca) #Importance of components: # PC1 PC2 PC3 PC4 # Standard deviation 1.7084 0.9560 0.38309 0.14393 # Proportion of Variance 0.7296 0.2285 0.03669 0.00518 # Cumulative Proportion 0.7296 0.9581 0.99482 1.00000
Now, we can visualize our PCA results via the autoplot() function.
Scatterplot using autoplot()
All we need to visualize our PCA on a scatterplot using the autoplot() function is to input the
iris_pca object to the function. If the users want to add a title and color the data points by group, they should also use the data, colour, and main arguments.
autoplot(iris_pca, data=iris, main = "Scatterplot", colour = 'Species')
As seen, the axis labels specifying the principal components and explained variances (compare with the summary output of iris_pca) and the legend are added by default.
Biplot using autoplot()
We should use some additional function arguments to plot a biplot via autoplot(). First and foremost,
loadings should be set to
TRUE to make sure that one of the main visual components of biplots, loading vectors, are added. Then the rest is about how to color and label those vectors.
autoplot(iris_pca, data = iris, colour = 'Species', main = "Biplot", loadings = TRUE, loadings.label=TRUE, loadings.colour = "black", loadings.label.colour="black", loadings.label.repel=TRUE)
We hope you found this tutorial helpful. See you at the next one!
Video, Further Resources & Summary
Do you need more background on how to perform a Principal Component Analysis in R? Then check the following YouTube video of the Statistics Globe YouTube channel.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
You could also have a look at some of the other tutorials available on Statistics Globe:
- What is PCA?
- Can PCA be Used for Categorical Variables?
- Principal Component Analysis in R
- Scatterplot of PCA in R
- Biplot of PCA in R
- Biplot for PCA Explained – How to Interpret
This post has shown how to make an autoplot of a PCA in R. In case you have any questions, you can leave a comment below.
Statistics Globe Newsletter