Show Sidebar Hide Sidebar

# Splom in R

How to make scatter-plot matrices or "sploms" natively with Plotly.

### New to Plotly?

Plotly's R library is free and open source!
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

``````library(plotly)
packageVersion('plotly')
``````
``````## [1] '4.7.1.9000'
``````

#### Splom trace

A scaterplot matrix is a matrix associated to n numerical arrays (data variables), \$X^1,X^2,.,X_n\$ , of the same length. The cell (i,j) of such a matrix displays the scatter plot of the variable Xi versus Xj ,

The Plotly splom trace implementation for the scaterplot matrix does not require to set \$x=Xi\$ , and \$y=Xj\$, for each scatter plot. All arrays, \$X^1,X^2,.,X_n\$ , are passed once, through a list of dicts called dimensions, i.e. each array/variable represents a dimension.

A trace of type splom is defined as follows:

``````p <- plot_ly() %>%
dimensions = list(
list(label='string-1', values=X1),
list(label='string-2', values=X2),
.
.
.
list(label='string-n', values=Xn)),
text=text,
marker=list(...)
)
``````

The label in each dimension is assigned to the axes titles of the corresponding matrix cell.

text is either a unique string assigned to all points displayed by splom or a list of strings of the same length as the dimensions, \$X_i\$. The `text[k]` is the tooltip for the \$k^{th}\$ point in each cell.

marker sets the markers attributes in all scatter plots.

#### Splom of the Iris data set

``````library(plotly)

``````

The Iris dataset contains four data variables, sepal length, sepal width, petal length petal width, for 150 iris flowers. The flowers are labeled as `Iris-setosa`, `Iris-versicolor`, `Iris-virginica`.

Define a discrete colorscale with three colors corresponding to the three flower classes:

``````pl_colorscale=list(c(0.0, '#19d3f3'),
c(0.333, '#19d3f3'),
c(0.333, '#e763fa'),
c(0.666, '#e763fa'),
c(0.666, '#636efa'),
c(1, '#636efa'))
``````

Then create the splom:

``````axis = list(showline=FALSE,
zeroline=FALSE,
gridcolor='#ffff',
ticklen=4)

p <- df %>%
plot_ly() %>%
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)
),
text=~class,
marker = list(
color = as.integer(df\$class),
colorscale = pl_colorscale,
size = 7,
line = list(
width = 1,
color = 'rgb(230,230,230)'
)
)
) %>%
layout(
title= 'Iris Data set',
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
)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="splom-iris")
``````

The scatter plots on the principal diagonal can be removed by setting `diagonal=list(visible=FALSE)`:

``````library(plotly)

pp <- p %>% style(diagonal = list(visible = F))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(pp, filename="splom-iris2")
``````

To plot only the lower/upper half of the splom we switch the default `showlowerhalf=True` / `showupperhalf=False`:

``````library(plotly)

pp <- p %>% style(showupperhalf = F)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(pp, filename="splom-iris3")
``````

Each list in the dimensions has a key, visible, set by default on True. We can choose to remove a variable from splom, by setting `visible=FALSE` in its corresponding dimension. In this case the default grid associated to the scatterplot matrix keeps its number of cells, but the cells in the row and column corresponding to the visible false dimension are empty:

``````library(plotly)

pp <- plotly_build(p)
pp\$x\$data[[1]]\$dimensions[[3]] <- list(visible = F)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(pp, filename="splom-iris4")
``````

#### Splom for the diabetes dataset

Diabetes dataset is downloaded from kaggle. It is used to predict the onset of diabetes based on 8 diagnostic measures. The diabetes file contains the diagnostic measures for 768 patients, that are labeled as non-diabetic (Outcome=0), respectively diabetic (Outcome=1). The splom associated to the 8 variables can illustrate the strength of the relationship between pairs of measures for diabetic/nondiabetic patients.

``````library(plotly)

``````

We define aa discrete colorscale with two colors: blue for non-diabetics and red for diabetics:

``````pl_colorscale = list(c(0.0, '#119dff'),
c(0.5, '#119dff'),
c(0.5, '#ef553b'),
c(1, '#ef553b'))
``````

Then create the splom:

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

p <- df %>%
plot_ly() %>%
type = 'splom',
dimensions = list(
list(label='Pregnancies', values=~Pregnancies),
list(label='Glucose', values=~Glucose),
list(label='BloodPressure', values=~BloodPressure),
list(label='SkinThickness', values=~SkinThickness),
list(label='Insulin', values=~Insulin),
list(label='BMI', values=~BMI),
list(label='DiabPedigreeFun', values=~DiabetesPedigreeFunction),
list(label='Age', values=~Age)
),
text=~factor(Outcome, labels=c("non-diabetic","diabetic")),
diagonal=list(visible=F),
marker = list(
color = ~Outcome,
colorscale = pl_colorscale,
size = 5,
line = list(
width = 1,
color = 'rgb(230,230,230)'
)
)
) %>%
layout(
title = "Scatterplot Matrix (SPLOM) for Diabetes Dataset<br>Data source: <a href='https://www.kaggle.com/uciml/pima-indians-diabetes-database/data'>[1]</a>",
hovermode='closest',
dragmode = 'select',
plot_bgcolor='rgba(240,240,240, 0.95)',
xaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4, titlefont=list(size=13)),
yaxis=list(domain=NULL, showline=F, zeroline=F, gridcolor='#ffff', ticklen=4, titlefont=list(size=13)),
xaxis2=axis,
xaxis3=axis,
xaxis4=axis,
xaxis5=axis,
xaxis6=axis,
xaxis7=axis,
xaxis8=axis,
yaxis2=axis,
yaxis3=axis,
yaxis4=axis,
yaxis5=axis,
yaxis6=axis,
yaxis7=axis,
yaxis8=axis
)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="splom-diabetes")