Scatterplot Matrix in ggplot2

How to make Scatterplot Matrix using `ggpairs()` 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.

Default splot plot

The ggpairs() function of the GGally package allows to build a great scatterplot matrix.

Scatterplots of each pair of numeric variable are drawn on the left part of the figure. Pearson correlation is displayed on the right. Variable distribution is available on the diagonal.

library(plotly)
library(GGally)

data <- data.frame( var1 = 1:100 + rnorm(100,sd=20), v2 = 1:100 + rnorm(100,sd=27), v3 = rep(1, 100) + rnorm(100, sd = 1))
data$v4 = data$var1 ** 2
data$v5 = -(data$var1 ** 2)

p <- ggpairs(data, title="correlogram with ggpairs()")

ggplotly(p)
Click to copy
-50050100-15000-10000-50000050001000015000-10123-500501000.0000.0030.0060.009-50050100Corr:0.599***-10123Corr:0.107Corr:0.092050001000015000Corr:0.053Corr:0.526***Corr:0.903***-15000-10000-50000Corr:-1.000***Corr:-0.053Corr:-0.526***Corr:-0.903***
correlogram with ggpairs()var1v2v3v4v5v5v4v3v2var1

Visualize correlation

he ggcorr() function allows to visualize the correlation of each pair of variable as a square. Note that the method argument allows to pick the correlation type you desire.

library(plotly)
library(GGally)

data <- data.frame( var1 = 1:100 + rnorm(100,sd=20), v2 = 1:100 + rnorm(100,sd=27), v3 = rep(1, 100) + rnorm(100, sd = 1))
data$v4 = data$var1 ** 2
data$v5 = -(data$var1 ** 2)

p <- ggcorr(data, method = c("everything", "pearson"))

ggplotly(p)
Click to copy

Split by group

It is possible to use ggplot2 aesthetics on the chart, for instance to color each category.

library(plotly)
library(GGally)

data(flea)

p <- ggpairs(flea, columns = 2:4, ggplot2::aes(colour=species))

ggplotly(p)
Click to copy
1201501802102404550551101201301400.000.010.020.030.04110120130140Corr: 0.026 Concinna: 0.766*** Heikert.: 0.640***Heptapot.: 0.558** 455055Corr: 0.673*** Concinna: 0.722*** Heikert.: 0.507** Heptapot.: 0.647** Corr: -0.096 Concinna: 0.683*** Heikert.: 0.646***Heptapot.: 0.774***
tars1tars2headheadtars2tars1

Change plot types

Change the type of plot used on each part of the correlogram. This is done with the upper and lower argument.

library(plotly)
library(GGally)

data(tips, package = "reshape")

p <- ggpairs(
      tips[, c(1, 3, 4, 2)],
      upper = list(continuous = "density", combo = "box_no_facet"),
      lower = list(continuous = "points", combo = "dot_no_facet")
)

ggplotly(p)
Click to copy
10203040502.55.07.510.0YesNoMaleFemale0.000.020.04FemaleMaleNoYes2.55.07.510.0
total_billsexsmokertiptipsmokersextotal_billsmokersexNoYes

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