PCA Visualization in ggplot2

How to do PCA Visualization in ggplot2 with Plotly.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

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)

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)

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)

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)

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)

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)

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)