3D Plot of PCA in R (2 Examples)

 

In this tutorial I’ll demonstrate how to draw a 3D Plot of a Principal Component Analysis (PCA) in the R programming language.

This is the table of content:

Here’s how to do it.

 

Prepare Data and Load Add-On Library

First of all, we will need to load the library that will allow us to plot our PCA in 3D. This is the rgl library, so if you haven’t installed it yet, please install it now using the code below:

install.packages("rgl")

Next, load the library:

library(rgl)

Now, it’s time to prepare our data in order to perform the PCA. For this example, we will be using the mtcars database, which contains data about 32 car models and 11 variables.

Take a look at the data set:

data(mtcars)
 
head(mtcars)

plot pca

As shown, there are two variables that look like categorical variables. As PCA works better using only numeric data, we will exclude these variables from our data set:

data_cars <- mtcars[,-(8:9), 
                    drop=FALSE]
 
head(data_cars)

plot pca

Now, we have a data set with 32 observations and 9 variables. Let’s see how this looks in a 3D PCA plot.

 

Perform and Visualize the PCA

The next step is to create our PCA:

pca_cars <- prcomp(data_cars, 
                   scale=TRUE)
 
summary(pca_cars)
 
# Importance of components:
#                           PC1    PC2     PC3     PC4     PC5     PC6     PC7
# Standard deviation     2.3782 1.4429 0.71008 0.51481 0.42797 0.35184 0.32413
# Proportion of Variance 0.6284 0.2313 0.05602 0.02945 0.02035 0.01375 0.01167
# Cumulative Proportion  0.6284 0.8598 0.91581 0.94525 0.96560 0.97936 0.99103
#                           PC8     PC9
# Standard deviation     0.2419 0.14896
# Proportion of Variance 0.0065 0.00247
# Cumulative Proportion  0.9975 1.00000

The PCA shows that the first two principal components explain 82% of the variance.

Let’s calculate the scores too in order to represent them in our 3D plot. We can find them in “$x” in our PCA:

scores = as.data.frame(pca_cars$x)
 
head(scores[1:4])

plot pca

Here, we can see the four principal components in our PCA with its scores. Let’s see how our PCA looks using the two principal components in a biplot:

biplot(pca_cars)

plot pca

Let’s plot them in 3D now.

 

Example 1: Plot the PCA in 3D

Now, we will use the three first components in our PCA and plot them in a 3D plot. In order to accomplish this, we will use the plot3d() function and, then, the text3d() function, so we can include the names of the cars in our 3D plot:

plot3d(scores[,1:3], 
       size=5,
       col = seq(nrow(scores)))
 
text3d(scores[,1:3],
       texts=c(rownames(scores)), 
       cex= 0.7, pos=3)

plot pca

 

Example 2: Add Biplot to the 3D Plot

We can also add the information related to the variables as we did in the biplot. For this, we will be using the plot3d() and text3d() functions too, but adding a for loop that allows us to include the arrows that we can see in the biplot:

plot3d(scores[,1:3])
 
text3d(scores[,1:3],
       texts=rownames(data_cars),
       cex=0.8)
 
text3d(pca_cars$rotation[,1:3], 
       texts=rownames(pca_cars$rotation), 
       col="red", 
       cex=0.8)
 
coords <- NULL
for (i in 1:nrow(pca_cars$rotation)) {
  coords <- rbind(coords, 
                  rbind(c(0,0,0),
                                pca_cars$rotation[i,1:3]))
}
 
lines3d(coords, 
        col="red", 
        lwd=1)

plot pca

 

Video, Further Resources & Summary

Do you need more explanations on how to create a 3D Plot 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.

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to plot your PCA results in 3D. In case you have further questions, you may leave a comment below.

 

Paula Villasante Soriano Statistician & R Programmer

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top