# PCA Visualization in R

Visualize Principle Component Analysis (PCA) of your high-dimensional data in R with Plotly.

New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Visualize Principle Component Analysis (PCA) of your high-dimensional data in R with Plotly.

This page first shows how to visualize higher dimension data using various Plotly figures combined with dimensionality reduction (aka projection). Then, we dive into the specific details of our projection algorithm.

We will use Tidymodels or Caret to load one of the datasets, and apply dimensionality reduction. Tidymodels is a popular Machine Learning (ML) library that offers various tools for creating and training ML algorithms, feature engineering, data cleaning, and evaluating and testing models.

## High-dimensional PCA Analysis with splom

The dimensionality reduction technique we will be using is called the Principal Component Analysis (PCA). It is a powerful technique that arises from linear algebra and probability theory. In essence, it computes a matrix that represents the variation of your data (covariance matrix/eigenvectors), and rank them by their relevance (explained variance/eigenvalues).

### Visualize all the original dimensions

First, let's plot all the features and see how the species in the Iris dataset are grouped. In a Scatter Plot Matrix (splom), each subplot displays a feature against another, so if we have $N$ features we have a $N \times N$ matrix.

In our example, we are plotting all 4 features from the Iris dataset, thus we can see how sepal_width is compared against sepal_length, then against petal_width, and so forth. Keep in mind how some pairs of features can more easily separate different species.

library(plotly)

data(iris)

axis = list(showline=FALSE,
zeroline=FALSE,
gridcolor='#ffff',
ticklen=4,
titlefont=list(size=13))

fig <- iris %>%
plot_ly()
fig <- fig %>%
type = 'splom',
dimensions = list(
list(label='sepal length', values=~Sepal.Length),
list(label='sepal width', values=~Sepal.Width),
list(label='petal length', values=~Petal.Length),
list(label='petal width', values=~Petal.Width)
),
color = ~Species, colors = c('#636EFA','#EF553B','#00CC96') ,
marker = list(
size = 7,
line = list(
width = 1,
color = 'rgb(230,230,230)'
)
)
)
fig <-  fig %>% style(diagonal = list(visible = FALSE))
fig <- fig %>%
layout(
hovermode='closest',
dragmode= 'select',
plot_bgcolor='rgba(240,240,240, 0.95)',
xaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4),
yaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4),
xaxis2=axis,
xaxis3=axis,
xaxis4=axis,
yaxis2=axis,
yaxis3=axis,
yaxis4=axis
)

fig


### Visualize all the principal components

Now, we apply PCA to the same dataset, and retrieve all the components. We use the same splom trace to display our results, but this time our features are the resulting principal components, ordered by how much variance they are able to explain.

The importance of explained variance is demonstrated in the example below. The subplot between PC3 and PC4 is clearly unable to separate each class, whereas the subplot between PC1 and PC2 shows a clear separation between each species.

library(plotly)
library(stats)
data(iris)
X <- subset(iris, select = -c(Species))
prin_comp <- prcomp(X)
explained_variance_ratio <- summary(prin_comp)[["importance"]]['Proportion of Variance',]
explained_variance_ratio <- 100 * explained_variance_ratio
components <- prin_comp[["x"]]
components <- data.frame(components)
components <- cbind(components, iris$Species) components$PC3 <- -components$PC3 components$PC2 <- -components$PC2 axis = list(showline=FALSE, zeroline=FALSE, gridcolor='#ffff', ticklen=4, titlefont=list(size=13)) fig <- components %>% plot_ly() %>% add_trace( type = 'splom', dimensions = list( list(label=paste('PC 1 (',toString(round(explained_variance_ratio[1],1)),'%)',sep = ''), values=~PC1), list(label=paste('PC 2 (',toString(round(explained_variance_ratio[2],1)),'%)',sep = ''), values=~PC2), list(label=paste('PC 3 (',toString(round(explained_variance_ratio[3],1)),'%)',sep = ''), values=~PC3), list(label=paste('PC 4 (',toString(round(explained_variance_ratio[4],1)),'%)',sep = ''), values=~PC4) ), color = ~iris$Species, colors = c('#636EFA','#EF553B','#00CC96')
) %>%
style(diagonal = list(visible = FALSE)) %>%
layout(
legend=list(title=list(text='color')),
hovermode='closest',
dragmode= 'select',
plot_bgcolor='rgba(240,240,240, 0.95)',
xaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4),
yaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4),
xaxis2=axis,
xaxis3=axis,
xaxis4=axis,
yaxis2=axis,
yaxis3=axis,
yaxis4=axis
)

fig


### Visualize a subset of the principal components

When you will have too many features to visualize, you might be interested in only visualizing the most relevant components. Those components often capture a majority of the explained variance, which is a good way to tell if those components are sufficient for modelling this dataset.

In the example below, our dataset contains 10 features, but we only select the first 4 components, since they explain 99% of the total variance.

library(plotly)
library(stats)
library(MASS)

db = Boston

prin_comp <- prcomp(db, rank. = 4)

components <- prin_comp[["x"]]
components <- data.frame(components)
components <- cbind(components, db$medv) components$PC2 <- -components$PC2 colnames(components)[5] = 'Median_Price' tot_explained_variance_ratio <- summary(prin_comp)[["importance"]]['Proportion of Variance',] tot_explained_variance_ratio <- 100 * sum(tot_explained_variance_ratio) tit = 'Total Explained Variance = 99.56' axis = list(showline=FALSE, zeroline=FALSE, gridcolor='#ffff', ticklen=4) fig <- components %>% plot_ly() %>% add_trace( type = 'splom', dimensions = list( list(label='PC1', values=~PC1), list(label='PC2', values=~PC2), list(label='PC3', values=~PC3), list(label='PC4', values=~PC4) ), color=~Median_Price, marker = list( size = 7 ) ) %>% style(diagonal = list(visible = F)) %>% layout( title= tit, hovermode='closest', dragmode= 'select', plot_bgcolor='rgba(240,240,240, 0.95)', xaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4), yaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4), xaxis2=axis, xaxis3=axis, xaxis4=axis, yaxis2=axis, yaxis3=axis, yaxis4=axis ) options(warn=-1) fig  ## 2D PCA Scatter Plot In the previous examples, you saw how to visualize high-dimensional PCs. In this example, we show you how to simply visualize the first two principal components of a PCA, by reducing a dataset of 4 dimensions to 2D. library(plotly) library(stats) data(iris) X <- subset(iris, select = -c(Species)) prin_comp <- prcomp(X, rank. = 2) components <- prin_comp[["x"]] components <- data.frame(components) components <- cbind(components, iris$Species)
components$PC2 <- -components$PC2

fig <- plot_ly(components, x = ~PC1, y = ~PC2, color = ~iris$Species, colors = c('#636EFA','#EF553B','#00CC96'), type = 'scatter', mode = 'markers')%>% layout( legend=list(title=list(text='color')), plot_bgcolor='#e5ecf6', xaxis = list( title = "0", zerolinecolor = "#ffff", zerolinewidth = 2, gridcolor='#ffff'), yaxis = list( title = "1", zerolinecolor = "#ffff", zerolinewidth = 2, gridcolor='#ffff')) fig  ## Visualize PCA with scatter3d With scatter3d, you can visualize an additional dimension, which let you capture even more variance. data("iris") X <- subset(iris, select = -c(Species)) prin_comp <- prcomp(X, rank. = 3) components <- prin_comp[["x"]] components <- data.frame(components) components$PC2 <- -components$PC2 components$PC3 <- -components$PC3 components = cbind(components, iris$Species)

tot_explained_variance_ratio <- summary(prin_comp)[["importance"]]['Proportion of Variance',]
tot_explained_variance_ratio <- 100 * sum(tot_explained_variance_ratio)

tit = 'Total Explained Variance = 99.48'

fig <- plot_ly(components, x = ~PC1, y = ~PC2, z = ~PC3, color = ~iris$Species, colors = c('#636EFA','#EF553B','#00CC96') ) %>% add_markers(size = 12) fig <- fig %>% layout( title = tit, scene = list(bgcolor = "#e5ecf6") ) fig  ## Plotting explained variance Often, you might be interested in seeing how much variance PCA is able to explain as you increase the number of components, in order to decide how many dimensions to ultimately keep or analyze. This example shows you how to quickly plot the cumulative sum of explained variance for a high-dimensional dataset like PimaIndiansDiabetes. With a higher explained variance, you are able to capture more variability in your dataset, which could potentially lead to better performance when training your model. For a more mathematical explanation, see this Q&A thread. library(plotly) library(stats) library(base) library(mlbench) data(PimaIndiansDiabetes) X <- subset(PimaIndiansDiabetes, select = -c(diabetes)) prin_comp <- prcomp(X) explained_variance_ratio <- summary(prin_comp)[["importance"]]['Proportion of Variance',] cumsum <- cumsum(explained_variance_ratio) data <- data.frame(cumsum,seq(1, length(cumsum), 1)) colnames(data) <- c('Explained_Variance','Components') fig <- plot_ly(data = data, x = ~Components, y = ~Explained_Variance, type = 'scatter', mode = 'lines', fill = 'tozeroy') %>% layout( xaxis = list( title = "# Components", tickvals = seq(1, length(cumsum), 1)), yaxis = list( title = "Explained Variance")) fig  ## Visualize Loadings It is also possible to visualize loadings using shapes, and use annotations to indicate which feature a certain loading original belong to. Here, we define loadings as: $$loadings = eigenvectors \cdot \sqrt{eigenvalues}$$ For more details about the linear algebra behind eigenvectors and loadings, see this Q&A thread. library(plotly) library(stats) data(iris) X <- subset(iris, select = -c(Species)) prin_comp <- prcomp(X, rank = 2) components <- prin_comp[["x"]] components <- data.frame(components) components <- cbind(components, iris$Species)
components$PC2 <- -components$PC2
explained_variance <- summary(prin_comp)[["sdev"]]
explained_variance <- explained_variance[1:2]
comp <- prin_comp[["rotation"]]
comp[,'PC2'] <- - comp[,'PC2']
fig <- plot_ly(components, x = ~PC1, y = ~PC2, color = ~iris$Species, colors = c('#636EFA','#EF553B','#00CC96'), type = 'scatter', mode = 'markers') %>% layout( legend=list(title=list(text='color')), plot_bgcolor = "#e5ecf6", xaxis = list( title = "0"), yaxis = list( title = "1")) for (i in seq(4)){ fig <- fig %>% add_segments(x = 0, xend = loadings[i, 1], y = 0, yend = loadings[i, 2], line = list(color = 'black'),inherit = FALSE, showlegend = FALSE) %>% add_annotations(x=loadings[i, 1], y=loadings[i, 2], ax = 0, ay = 0,text = features[i], xanchor = 'center', yanchor= 'bottom') } fig  ## References Learn more about scatter3d, and splom here: • https://plot.ly/r/3d-scatter-plots/ • https://plot.ly/r/splom/ The following resources offer an in-depth overview of PCA and explained variance: • https://en.wikipedia.org/wiki/Explained_variation • https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues/140579#140579 • https://stats.stackexchange.com/questions/143905/loadings-vs-eigenvectors-in-pca-when-to-use-one-or-another • https://stats.stackexchange.com/questions/22569/pca-and-proportion-of-variance-explained ### What About Dash? Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash for R at https://dashr.plot.ly/installation. Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this: library(plotly) fig <- plot_ly() # fig <- fig %>% add_trace( ... ) # fig <- fig %>% layout( ... ) library(dash) library(dashCoreComponents) library(dashHtmlComponents) app <- Dash$new()
app$layout( htmlDiv( list( dccGraph(figure=fig) ) ) ) app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)