# Biplot of PCA in R (Examples)

In this article, you will learn how to draw a biplot of a Principal Component Analysis (PCA) in the R programming language.

The table of content looks as follows:

Let’s take a look!

## Load Data and Add-On Libraries

First of all, we will use the factoextra package. If you haven’t installed it yet, now is the right time to do it.

install.packages("factoextra") |

install.packages("factoextra")

The next step (or the first step if you have already installed this package) is to load the library.

library("factoextra") |

library("factoextra")

Now, we will load our data. For this tutorial, we will use the iris dataset, which contains information about the measurements of sepal length, sepal width, petal length, and petal width from 3 species of iris and 50 flowers.

data(iris) |

data(iris)

Now let’s check the number of columns and rows, and the overview of the first few rows, as shown below.

dim(iris) # [1] 150 5 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 |

dim(iris) # [1] 150 5 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

All is set, time to start the analysis!

## PCA

Since PCA is designed for continuous variables, we will perform our PCA for the numerical variables, excluding the “species” column. If you are interested in PCAs using categorical variables, check our tutorial: Can PCA be Used for Categorical Variables? First, we need to call the prcomp() function to run the analysis.

pca <- prcomp(iris[, -5], scale = TRUE) |

pca <- prcomp(iris[, -5], scale = TRUE)

We have specified `scale = TRUE`

inside the function to conduct a PCA using a correlation matrix, which ensures that the sensitivity to larger variable variations is taken into account.

Using the command `pca$rotation`

, we can see the loadings, which are represented by the vectors in biplots.

pca$rotation # PC1 PC2 PC3 PC4 # Sepal.Length 0.5210659 -0.37741762 0.7195664 0.2612863 # Sepal.Width -0.2693474 -0.92329566 -0.2443818 -0.1235096 # Petal.Length 0.5804131 -0.02449161 -0.1421264 -0.8014492 # Petal.Width 0.5648565 -0.06694199 -0.6342727 0.5235971 |

pca$rotation # PC1 PC2 PC3 PC4 # Sepal.Length 0.5210659 -0.37741762 0.7195664 0.2612863 # Sepal.Width -0.2693474 -0.92329566 -0.2443818 -0.1235096 # Petal.Length 0.5804131 -0.02449161 -0.1421264 -0.8014492 # Petal.Width 0.5648565 -0.06694199 -0.6342727 0.5235971

We can also check the principal component scores stored in `x`

, which are shown by the scatter points in biplots.

head(pca$x) # PC1 PC2 PC3 PC4 # [1,] -2.257141 -0.4784238 0.12727962 0.024087508 # [2,] -2.074013 0.6718827 0.23382552 0.102662845 # [3,] -2.356335 0.3407664 -0.04405390 0.028282305 # [4,] -2.291707 0.5953999 -0.09098530 -0.065735340 # [5,] -2.381863 -0.6446757 -0.01568565 -0.035802870 # [6,] -2.068701 -1.4842053 -0.02687825 0.006586116 |

head(pca$x) # PC1 PC2 PC3 PC4 # [1,] -2.257141 -0.4784238 0.12727962 0.024087508 # [2,] -2.074013 0.6718827 0.23382552 0.102662845 # [3,] -2.356335 0.3407664 -0.04405390 0.028282305 # [4,] -2.291707 0.5953999 -0.09098530 -0.065735340 # [5,] -2.381863 -0.6446757 -0.01568565 -0.035802870 # [6,] -2.068701 -1.4842053 -0.02687825 0.006586116

In the next sections, we will plot some biplots using the data discussed above. We don’t need to extract the data as previously shown to plot the biplots. However, if you are interested in having the exact numeric values, you can use the shared scripts above.

## Example 1: Biplot of PCA Using base R

To create a biplot using base R, we need to call the biplot() function, specifying the `pca`

object and `scale = 0`

, which indicates that the data is already standardized.

biplot(pca, scale = 0) |

biplot(pca, scale = 0)

The biplot shows the distribution of data points and variables concerning the first and second principal components. If you want to learn more about how to interpret the biplot, you can check our tutorial Biplot for PCA Explained.

Alternatively, we can change the color of the loading vectors and the scatter points using the `col`

argument and remove the labels of the data points using the `xlabs`

argument. In the rep() function, 150 refers to the number of data points, and * refers to the marker type.

biplot(pca, col = c('darkblue', 'red'), scale = 0, xlabs = rep("*", 150)) |

biplot(pca, col = c('darkblue', 'red'), scale = 0, xlabs = rep("*", 150))

Using the biplot() function, you can also modify the font size, axis limits, or length of arrows via the `cex`

, `xlim`

, and `expand`

arguments, respectively. If you want to customize your biplot in a more advanced way, you should use the factoextra package. In the next section, some alternatives are shown.

## Example 2: Biplot of PCA Using factoextra Package

It’s also possible to create a biplot using the fviz_pca_biplot() function of the factoextra package, which is specialized to visualize PCA output. Like in base R, we must input the initialized `pca`

object to run the function.

fviz_pca_biplot(pca) |

fviz_pca_biplot(pca)

The data points and the loading vectors are labeled by default, like in base R. However, different from base R, the grid lines and title are plotted, and the explained percentage of variance is shown in the axis labels.

Please be aware that the principal components are called as dimensions in factoextra, e.g., Dim1, Dim2. The labels can be modified by the labs function of ggplot2. You can also use other ggplot2 functions by appending them using the `+`

operator.

We can customize the default output in different ways. For instance, we can keep only the vector labels by using `label = "var"`

and color the data points by the specie using the `habillage`

argument. Note that this argument also automatically changes the marker type per specie.

fviz_pca_biplot(pca, label="var", habillage = iris$Species) |

fviz_pca_biplot(pca, label="var", habillage = iris$Species)

The group coloring can be changed using the scale_color_manual() function of ggplot2 by setting the `values`

argument to a vector of colors or using the scale_color_brewer() function of ggplot2 by specifying the color palette via the `palette`

argument. If you are also interested in framing the data points by group, you can visit our Draw Ellipse Plot for Groups in PCA in R tutorial.

One might also be interested in coloring the data points based on their quality of representation by the first two components. In such a case, the `col.ind`

argument should be set to `"cos2"`

, as shown below. The default colors can be changed using the argument `gradient.cols`

and the vector colors can be set to a unique black color via `col.var = "black"`

.

fviz_pca_biplot(pca, label = "var", col.ind = "cos2", col.var = "black", gradient.cols = c("blue","green","red")) |

fviz_pca_biplot(pca, label = "var", col.ind = "cos2", col.var = "black", gradient.cols = c("blue","green","red"))

The figure shows that the flower samples are represented well if they are colored red and poorly represented if colored blue. The same coloring can also be set for the loading vectors via `col.var = "cos2"`

.

Another way of coloring by statistics is coloring by the contribution to the components. See below how the variable’s contributions are shown through the colors of loading vectors by setting `col.var = "contrib"`

.

fviz_pca_biplot(pca, label = "var", col.ind = "black", col.var = "contrib", gradient.cols = c("blue","green","red")) |

fviz_pca_biplot(pca, label = "var", col.ind = "black", col.var = "contrib", gradient.cols = c("blue","green","red"))

This is the end of our tutorial explaining how to plot biplots in R. We have shown the options provided by the base and factoextra libraries. Alternatively, the autoplot() function of ggfortify, which is a more generic function to visualize multivariate data, can be used for plotting biplots. If interested, see Autoplot of PCA in R.

## Video, Further Resources & Summary

Do you want to know more about the biplot of a PCA in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

*The YouTube video will be added soon.*

In addition, you may want to have a look at some of the other tutorials on Statistics Globe:

- What is PCA?
- Principal Component Analysis (PCA) in R
- PCA Using Correlation & Covariance Matrix
- Choose Optimal Number of Components for PCA
- Biplot for PCA Explained
- Biplot of PCA in R
- Draw Ellipse Plot for Groups in PCA in R
- Autoplot of PCA in R

In this post you have learned two examples of how to **make a biplot of a PCA in R**. Leave a comment if you have any questions.

### Statistics Globe Newsletter