PCA Visualization in ggplot2

How to do PCA Visualization in ggplot2 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.

ggfortify lets ggplot2 know how to interpret PCA objects. After loading ggfortify, you can use ggplot2::autoplot function for stats::prcomp and stats::princomp objects.

Default plot

library(plotly)
library(ggfortify)

df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)

p <- autoplot(pca_res)

ggplotly(p)
Click to copy

PCA result should only contains numeric values. If you want to colorize by non-numeric values which original data has, pass original data using data keyword and then specify column name by colour keyword. Use help(autoplot.prcomp) (or help(autoplot.*) for any other objects) to check available options.

library(plotly)
library(ggfortify)

df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)

p <- autoplot(pca_res, data = iris, colour = 'Species')

ggplotly(p)
Click to copy
-0.10.00.1-0.2-0.10.00.10.2
SpeciessetosaversicolorvirginicaPC1 (72.96%)PC2 (22.85%)

Adding data labels

Passing label = TRUEdraws each data label using rownames

library(plotly)
library(ggfortify)

df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)

p <- autoplot(pca_res, data = iris, colour = 'Species', label = TRUE, label.size = 3)

ggplotly(p)
Click to copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150-0.10.00.1-0.2-0.10.00.10.2
SpeciessetosaversicolorvirginicaPC1 (72.96%)PC2 (22.85%)

Passing shape = FALSE makes plot without points. In this case, label is turned on unless otherwise specified.

library(plotly)
library(ggfortify)

df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)

p <- autoplot(pca_res, data = iris, colour = 'Species', shape = FALSE, label.size = 3)

ggplotly(p)
Click to copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150-0.10.00.1-0.2-0.10.00.10.2
SpeciessetosaAaversicolorAavirginicaAaPC1 (72.96%)PC2 (22.85%)

Displaying eigenvectors.

Passing loadings = TRUE draws eigenvectors.

library(plotly)
library(ggfortify)

df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)

p <- autoplot(pca_res, data = iris, colour = 'Species', loadings = TRUE)

ggplotly(p)
Click to copy
-0.10.00.1-0.2-0.10.00.10.2
SpeciessetosaversicolorvirginicaPC1 (72.96%)PC2 (22.85%)

You can attach eigenvector labels and change some options.

library(plotly)

df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)

p <- autoplot(pca_res, data = iris, colour = 'Species',
              loadings = TRUE, loadings.colour = 'blue',
              loadings.label = TRUE, loadings.label.size = 3)

ggplotly(p)
Click to copy
Sepal.LengthSepal.WidthPetal.LengthPetal.Width-0.10.00.1-0.2-0.10.00.10.2
SpeciessetosaversicolorvirginicaPC1 (72.96%)PC2 (22.85%)

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)
Click to copy