# Biplot of PCA in R (2 Examples)

In this article you’ll 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.

## Creation of Sample Data and Add-On Libraries

First of all, we will be using the factoextra library. 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 had already installed this library), is to load the package:

library("factoextra") |

library("factoextra")

Now, we will create a data frame in order to use it as an example in this tutorial. Our data frame will have 40 samples and 14 features:

set.seed(99991) data <- matrix(nrow=40, ncol=14) colnames(data) <- c(paste("F", 1:14, sep="")) rownames(data) <- paste("sample", 1:nrow(data), sep="") for (i in 1:40) { v.values <- rpois(14, lambda = sample(x=1:199, size=1)) data[i, ] <- c(v.values) } |

set.seed(99991) data <- matrix(nrow=40, ncol=14) colnames(data) <- c(paste("F", 1:14, sep="")) rownames(data) <- paste("sample", 1:nrow(data), sep="") for (i in 1:40) { v.values <- rpois(14, lambda = sample(x=1:199, size=1)) data[i, ] <- c(v.values) }

Thus, our data will look like this (we will show only the head):

head(data) # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] # [1,] 106 91 97 125 119 110 101 124 93 117 124 117 110 126 # [2,] 32 37 34 53 39 40 49 50 28 37 44 25 50 34 # [3,] 150 128 158 138 148 114 151 154 154 117 134 116 120 139 # [4,] 33 39 32 32 37 24 47 40 41 37 40 25 37 29 # [5,] 173 187 181 172 173 179 151 181 196 158 193 185 179 186 # [6,] 130 141 118 104 126 111 111 128 141 110 130 105 98 100 |

head(data) # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] # [1,] 106 91 97 125 119 110 101 124 93 117 124 117 110 126 # [2,] 32 37 34 53 39 40 49 50 28 37 44 25 50 34 # [3,] 150 128 158 138 148 114 151 154 154 117 134 116 120 139 # [4,] 33 39 32 32 37 24 47 40 41 37 40 25 37 29 # [5,] 173 187 181 172 173 179 151 181 196 158 193 185 179 186 # [6,] 130 141 118 104 126 111 111 128 141 110 130 105 98 100

Now, let’s perform our PCA.

## Perform the PCA

We will perform our PCA using the prcomp() function:

df_pca <- prcomp(data, scale=TRUE) summary(df_pca) # Importance of components: # PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8 PC9 PC10 PC11 # Standard deviation 3.6697 0.30958 0.26649 0.25720 0.24067 0.23292 0.20599 0.18870 0.17864 0.15272 0.14200 # Proportion of Variance 0.9619 0.00685 0.00507 0.00473 0.00414 0.00388 0.00303 0.00254 0.00228 0.00167 0.00144 # Cumulative Proportion 0.9619 0.96878 0.97385 0.97857 0.98271 0.98659 0.98962 0.99216 0.99444 0.99611 0.99755 # PC12 PC13 PC14 # Standard deviation 0.12196 0.10081 0.09642 # Proportion of Variance 0.00106 0.00073 0.00066 # Cumulative Proportion 0.99861 0.99934 1.00000 |

df_pca <- prcomp(data, scale=TRUE) summary(df_pca) # Importance of components: # PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8 PC9 PC10 PC11 # Standard deviation 3.6697 0.30958 0.26649 0.25720 0.24067 0.23292 0.20599 0.18870 0.17864 0.15272 0.14200 # Proportion of Variance 0.9619 0.00685 0.00507 0.00473 0.00414 0.00388 0.00303 0.00254 0.00228 0.00167 0.00144 # Cumulative Proportion 0.9619 0.96878 0.97385 0.97857 0.98271 0.98659 0.98962 0.99216 0.99444 0.99611 0.99755 # PC12 PC13 PC14 # Standard deviation 0.12196 0.10081 0.09642 # Proportion of Variance 0.00106 0.00073 0.00066 # Cumulative Proportion 0.99861 0.99934 1.00000

Let’s see how the two principal components look in a biplot.

## Example 1: Biplot of PCA Using Base R

Creating a biplot of our PCA using base R is quite easy. We just have to use the biplot() function:

biplot(df_pca) |

biplot(df_pca)

## Example 2: Biplot of PCA Using factoextra Package

We can also create a biplot by using the fviz_pca_biplot() function from the factoextra package:

fviz_pca_biplot(df_pca, repel = TRUE, col.var = "deepskyblue", col.ind = "gray40") |

fviz_pca_biplot(df_pca, repel = TRUE, col.var = "deepskyblue", col.ind = "gray40")

As shown, in both types of biplot we can see how the samples and the features are distributed in relation to the first and the second principal components.

## 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.*

You can also take a look to some of the other tutorials on Statistics Globe:

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.

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

### Statistics Globe Newsletter