Method · Statistics Library

Scatterplot matrix

A scatterplot matrix shows every pair of several numeric columns as a grid of scatterplots, with each column's own distribution on the diagonal and each pair's correlation above it, as a first look at which columns move together.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

With four columns there are six pairs, and the matrix draws them all at once. Each cell below the diagonal is the scatterplot of one pair: the column named under it runs across, the column named beside it runs up. The diagonal shows each column on its own (a histogram or a density curve), and the matching cell above the diagonal gives the same pair's correlation, from −1 to 1.

Read the pictures before the numbers. A correlation only measures how well a straight line (Pearson) or a steady rise or fall (Spearman) fits. A curve, separate clusters or one far-off point can make it misleadingly large or small.

Color the points by a group and look again. Coloring changes only the points: the correlations above the diagonal still use all rows, so filter the table to one group to get that group's own correlation. A trend across all points can differ from the trend inside each group, and can even point the other way. In the penguins, longer bills go with shallower bills overall (r = −0.24), yet inside each species longer bills go with deeper bills (r from 0.39 to 0.65): the species differ in both measurements, and mixing them reverses the picture.

A strong correlation is not a cause: two columns can move together because both depend on a third. The matrix tests nothing; it shows where to look next.

Practitioner explanation

KernelStats takes 2 to 8 numeric columns in the order chosen (more are refused). Each scatterplot and each correlation uses the rows where both of its columns are filled (pairwise complete, R's cor(use = "pairwise.complete.obs")), so different cells can rest on different rows; the table gives each pair's n. Correlations are Pearson or Spearman, with |r| described by convention (below 0.1 almost none, 0.1 to 0.4 weak, 0.4 to 0.7 moderate, 0.7 and above strong). The diagonal is a histogram with Sturges' bins, a density curve as R's density() draws it, or nothing; with correlations off, the upper cells mirror the scatterplots.

Group colors the points (at most 10 groups), but the correlations are always computed over all rows together, never within groups: for the within-group correlations, run the matrix on one group at a time (filter the rows), or fit a model that includes the group. Above 1,500 rows the scatterplots draw a 1,500-row sample (the same rows in every cell); correlations and diagonals use every row. For p-values and intervals use the Correlation Matrix; for a correlation adjusted for other columns, Partial Correlation.

When to use it

Consider it when

You have 3 to 8 numeric columns and want a first look at how each pair relates (straight, curved, clustered, outliers) before a regression, a principal component analysis or a cluster analysis.

Use caution when

Only two columns (a Scatter Plot shows them larger and can add a fitted line), more than 8 columns (choose the ones that matter, or use the Correlation Matrix for a table), categorical columns (use a crosstab), or when you need p-values (use the Correlation Matrix).

Formulas and symbols

Pearson correlation of a pair (pairwise complete rows)

rxy=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2∑i(yi−yˉ)2r_{xy} = \dfrac{\sum_{i} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i} (x_i - \bar{x})^2 \sum_{i} (y_i - \bar{y})^2}}

The sums run over the rows where both x and y are filled; Spearman's correlation is the same formula applied to the ranks.

Alternatives

  • Scatter Plot (Scatter Plot). One pair of columns, drawn larger, with an optional fitted line.
  • Correlation Matrix (Correlation Matrix). You want the correlations with p-values and intervals, or more than 8 columns at once.
  • Partial Correlation (Partial Correlation). You want the correlation of two columns after allowing for one or more others.

Worked example

Scenario

Scatterplot matrix of bill length, bill depth, flipper length and body mass in the penguins sample (342 rows with values; 2 blank rows left out), colored by species, Pearson correlations; then the bill pair for each species alone (rows filtered).

Result

Strongest pair: flipper length and body mass, r = 0.87; weakest: bill length and bill depth, r = −0.24. Bill length and bill depth within species: Adelie r = 0.39 (n = 151), Chinstrap r = 0.65 (n = 68), Gentoo r = 0.64 (n = 123).

Interpretation

Flipper length and body mass rise together strongly. The weak negative overall correlation between the bill measurements reverses within every species: it comes from mixing species that differ in both, a case of Simpson's paradox.

Dataset: penguins.csv

Common misconceptions

Misconception

Mixing the groups cannot reverse a trend: what holds for all the points holds within each group.

Correction

It can. In the penguins, bill length and bill depth correlate at −0.24 across all the birds but at +0.39 (Adelie), +0.65 (Chinstrap) and +0.64 (Gentoo) within species, because the species differ in both measurements. Color by the group, and compute the correlation inside each group (filter the table to one group) before drawing a conclusion about it.

Misconception

One panel shows a tight upward pattern, so one of those two columns must be causing the other.

Correction

A tight pattern shows that two columns move together, not why. Both can depend on a third: in the penguins, flipper length and body mass correlate at 0.87, and both reflect overall body size. The matrix cannot tell cause from common cause.

Read more: Misconception: correlation implies causation

Misconception

A correlation near 0 means the two columns are unrelated.

Correction

It means no straight line (Pearson) or steady rise or fall (Spearman) fits. A U-shaped or clustered pattern can give a correlation near 0, so look at the scatterplot.

References

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.3.26.11 Scatterplot Matrix, NIST/SEMATECH (2012), §1.3.3.26.11.