# Autoplot of PCA in R (Example)

In this tutorial, you’ll learn **how to create a scatterplot and a biplot using the autoplot() function** for Principal Component Analysis (PCA) results in the R programming language.

The table of content is structured as shown in the following box:

Let’s start right away.

## Load Libraries and Create Sample Data Set

Before we start, installing the libraries, ggplot2 and the ggfortify, is convenient.

install.packages("ggplot2") install.packages("ggfortify") |

install.packages("ggplot2") install.packages("ggfortify")

If you have already installed these libraries in the past, just ignore this first step. Then skip to the following:

library(ggplot2) library(ggfortify) |

library(ggplot2) library(ggfortify)

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 |

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.

## Perform PCA

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 |

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

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

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 explanations on how to create an autoplot of PCA in R? Then check the following YouTube video of the Statistics Globe YouTube channel.

*The YouTube video will be added soon.*

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