Diastolic blood pressure and age have a correlation of 0.04 among 5,232 NHANES adults, as close to zero as any pair of measurements in the survey. A table of correlations would list the pair as unrelated. A scatter plot shows that they are strongly related: average diastolic pressure rises from 64 mm Hg in adults under 20 to 78 mm Hg in their fifties and falls back to 67 mm Hg after 70, a curve that a straight line, and therefore a correlation coefficient, cannot describe. A second pair, HDL cholesterol and height, has a correlation of $-0.19$, which suggests that taller people have less of the "good" cholesterol. Within men, and within women, the correlation is close to zero. Both numbers are correct, and both are misleading without a picture of the data and of the groups within it.
This article covers the displays that put several features on one page: scatter plots and the scatterplot matrix, the problem of too many points, correlation heatmaps and their pitfalls, color, shape and panels for third and fourth variables, and parallel coordinates for many variables at once. The data are adults from the National Health and Nutrition Examination Survey (NHANES) of 2017 to 2018, joined from several survey files (Performing Exploratory Data Analysis describes the table), and the 178 wines of the wine data used throughout the Unsupervised Learning articles. The individual scatter plot and how to describe one by its direction, form and strength are covered in Tables and Graphs for Data and Introduction to Regression; summaries by group in tables are in Exploring Data with Groups and Pivot Tables.
Pairs of numerical features: the scatterplot matrix
A table with $p$ numerical features has $p(p-1)/2$ pairs, and looking at each pair is the most direct way to find relationships, curves, clusters and unusual points. A scatterplot matrix arranges the scatter plots of all pairs in a $p \times p$ grid: the panel in row $i$ and column $j$ plots feature $i$ against feature $j$, and the diagonal, where a feature would be plotted against itself, shows its distribution instead. The display was popularized in the 1980s (Chambers, Cleveland, Kleiner and Tukey, 1983; Carr, Littlefield, Nicholson and Littlefield, 1987). Each pair appears twice, once above and once below the diagonal with the axes exchanged, so a common variant fills one triangle with scatter plots and the other with the correlation coefficients.
Five features give ten distinct pairs, and the matrix shows them all at once. Some patterns jump out. BMI and waist circumference fall in a tight band ($r = 0.904$), so they carry nearly the same information, a redundancy that matters for regression (Multiple Linear Regression shows what strongly correlated inputs do to coefficients) and that principal component analysis exploits. Systolic pressure rises with age ($r = 0.485$), with a spread that also grows with age. HDL cholesterol falls as waist circumference rises ($r = -0.331$). The diagonal shows that BMI, waist and HDL are skewed to the right, and that the age histogram has a tall bar at its right end: everyone 80 or older is recorded as 80.
The matrix scales badly. Ten features give 45 pairs and 100 panels, each too small to read, and the eye is drawn to the strongest relations, which are usually the least interesting (BMI and weight are related by definition). In practice the matrix is used for a handful of features chosen for a question, and a correlation heatmap, described below, is used to survey many.
Too many points
Overplotting
A scatter plot draws one mark per observation, and when marks land on top of one another the plot no longer shows how many observations are where. This is overplotting. It has two sources, and NHANES has both. One is sheer number: thousands of points in a small area. The other is discreteness: values recorded on a coarse grid land on exactly the same spots. Ages are recorded in whole years, with everyone 80 or over recorded as 80, and systolic pressures are means of up to three readings recorded in even numbers (Types of Numerical Data). Among the 5,247 adults with a systolic pressure, there are only 63 distinct ages and 237 distinct pressures, and only 3,352 distinct (age, pressure) positions. 3,105 adults, 59.2%, share their exact position with at least one other adult; the most crowded position holds 8 adults aged 18 with a mean pressure of 111.33 mm Hg.
Remedies
- Jitter. Adding a small random displacement to a discrete variable separates points that share a value. For age in whole years, a displacement uniform between $-0.45$ and $0.45$ years keeps every point within its own year. Jitter changes the data drawn, not the data analyzed, and should be stated in the caption.
- Transparency. Drawing each point with opacity $\alpha$, a fraction between 0 and 1, makes overlapping points add up: a region where $k$ points overlap has an opacity of $1 - (1 - \alpha)^k$, which increases with $k$. At $\alpha = 0.12$, one point is faint, ten overlapping points reach 72% opacity and twenty reach 92%. Beyond about 20 or 30 overlaps the region saturates, so transparency distinguishes sparse from dense but not dense from very dense; the right $\alpha$ depends on the number of points.
- Binning. For very large data, the points can be replaced by counts in a grid of cells, shaded by count, which is a two-dimensional histogram. Carr et al. (1987) proposed hexagonal cells, which tile the plane like squares but are closer to circles, so that the distance from a cell's center to its edge varies less with direction. In the figure, 628 hexagons are occupied, with between 1 and 37 adults each and a median of 6. Binning makes the plot's size independent of the number of points, which is why it is the usual choice for millions of observations, at the cost of hiding individual points and requiring a choice of cell size.
- Smaller marks, or a summary. Points can be shrunk, or the scatter replaced or supplemented by a line through conditional means or a smoother, as in the line of mean pressure by year of age in Tables and Graphs for Data.
Correlation heatmaps
A table with many numerical features can be surveyed through its correlation matrix, the $p \times p$ array of correlation coefficients between every pair, drawn as a heatmap in which each cell's color shows the coefficient. The Pearson coefficient $r$, which measures how closely a scatter plot follows a straight line, is defined and connected to the least-squares slope in Simple Linear Regression.
The heatmap of twelve NHANES measurements shows a block of three body-size measurements with correlations near 0.9 (BMI and waist 0.905, weight and waist 0.896, weight and BMI 0.894), a cluster of age with systolic pressure (0.487) and of systolic with diastolic pressure (0.453), height correlated with weight (0.436), and HDL cholesterol negatively correlated with every size measurement (weight $-0.330$, waist $-0.329$). Half of the 66 pairs have $|r| < 0.1$.
Three design choices make a heatmap readable:
- A diverging scale centered at zero. Correlations run from $-1$ to $1$ with a meaningful midpoint, so the color scale should have a neutral color at zero and two hues of equal strength on either side, and should span $-1$ to $1$ symmetrically. A default sequential color map would make $-0.3$ look like a weaker version of $+0.3$, or, if fitted to the range of the data, make the smallest correlation in the table look like a strong negative one. Best Practices for Visualizing Data covers diverging and sequential scales.
- A meaningful order. Alphabetical or file order scatters related variables. Ordering the rows and columns by a hierarchical clustering of the variables, with $1 - |r|$ as the distance between two variables, puts strongly correlated variables next to each other so that blocks become visible, as in the figure.
- Printed values. Colors are hard to read precisely, and two cells of $0.45$ and $0.49$ look alike; printing the coefficient in each cell, as here, lets the reader look up a value after the colors have shown where to look.
Four ways a correlation misleads
A heatmap is a summary of summaries, and it inherits every weakness of the correlation coefficient. Four are listed below, and the first three are drawn in the next figure.
- The relation is not a straight line. Pearson's $r$ measures linear association only. Diastolic pressure and age have $r = 0.035$, and Spearman's rank correlation, which measures any monotonic association, is 0.040, because the relation rises and then falls. The mean diastolic pressure is 63.8 mm Hg at ages 18 and 19 (250 adults), 68.0 in the twenties, 73.4 in the thirties, 77.1 in the forties, 77.7 in the fifties, 73.9 in the sixties and 67.4 at 70 and over. A least-squares quadratic in age explains 13.7% of the variance in diastolic pressure, against 0.1% for a straight line, and peaks at age 50.0. In a heatmap the pair is a dark cell indistinguishable from unrelated pairs.
- The relation is a difference between groups. HDL cholesterol and height have $r = -0.194$ over 5,099 adults. Within men the correlation is 0.006 (2,456 men), and within women 0.035 (2,643 women). The pooled correlation comes entirely from the difference between the sexes: men are taller on average, and their mean HDL is 48.2 mg/dL against 57.8 for women. Adding sex as color, as in the figure, shows this at a glance. The same mechanism, a comparison that reverses or disappears within groups, is the subject of the section on group composition in Exploring Data with Groups and Pivot Tables.
- Skew and extreme values dominate. C-reactive protein is heavily right-skewed (Visualizing Data with One Feature). Its Pearson correlation with BMI is 0.265 on the raw values and 0.479 on the logarithms; removing just the ten largest CRP values raises the raw correlation to 0.307, because those ten adults are far from the line. Spearman's rank correlation, the Pearson correlation of the ranks of the two variables, is unaffected by any monotonic transformation and resistant to extreme values; for CRP and BMI it is 0.491. In the NHANES matrix, the pairs where Spearman's and Pearson's coefficients differ most all involve a skewed variable: CRP with waist (Pearson 0.241, Spearman 0.481), with BMI and with weight, and glycohemoglobin with age (0.320 and 0.491). Computing both matrices and looking at their difference is a quick way to find the pairs that need a scatter plot.
- The numbers rest on different rows. With missing values, each coefficient is usually computed on the rows where both variables are present. In the NHANES matrix the pairs rest on between 4,875 and 5,444 adults, and only 4,674 have all twelve values. When missingness is large or related to the values, coefficients computed on different subsets need not be consistent with one another, and the matrix can even fail to be a valid correlation matrix (one with no negative eigenvalues).
A further weakness is the number of comparisons. Sixty-six correlations computed on the same data will include a few that look striking by chance, and choosing the most striking for further study is a form of the multiple comparisons problem (Multiple Comparisons and the Bonferroni Correction). With 5,000 observations even trivial correlations are statistically significant, so a heatmap should be read for size and pattern, not for significance.
A third and fourth feature
A scatter plot uses the two positions of the page for two features. Further features have to be carried by other properties of the marks, by splitting the plot into panels, or by a different geometry altogether.
Color, shape and size
Color (hue) is the usual channel for a categorical third feature, as sex is in the HDL panel above. With more than a handful of categories, colors become hard to tell apart and to match to a legend; Best Practices for Visualizing Data discusses palettes and color-vision deficiency. For a numerical third feature, a sequential color scale that runs from dark to light encodes magnitude, and a scale based on lightness is read far more reliably than one based on hue. Shape distinguishes a few categories and is preserved in grayscale print and for readers who cannot distinguish the colors; using shape and color together for the same feature, as the HDL panel does, is redundant on purpose. Size encodes a numerical feature as the area of the mark, the "bubble chart"; areas are judged less accurately than positions or lengths (Cleveland and McGill, 1984), large marks hide small ones, and size is best kept for a rough third variable.
These channels interact with overplotting. Color on a crowded scatter plot shows mostly the group drawn last, which sits on top. Drawing groups in random order, adding transparency, or splitting them into panels avoids that.
Small multiples
Faceting, or small multiples, draws one panel per value of a categorical feature, all with the same axes, so that the panels can be compared by eye. Tufte (1983) named the idea, and Becker, Cleveland and Shyu (1996) developed it as "trellis" displays, with panels conditioned on the values of one or more variables. Faceting avoids the overplotting and legend-matching of color, and it scales to more categories: six or twelve panels are readable where six or twelve colors are not.
The figure combines both channels: race and Hispanic origin as panels, sex as color and shape, and age on the horizontal axis. The pattern is the same in all six panels. Over all adults, men's mean systolic pressure exceeds women's by 6.5 mm Hg at ages 18 and 19, 8.4 in the twenties and 9.5 in the thirties; the gap narrows to 2.7 in the forties, reverses in the fifties ($-1.6$) and sixties ($-1.2$), and at 70 and over women's mean is 7.0 mm Hg higher. A single panel with twelve lines would hide this; six panels with two lines each make the repetition, and the exceptions, easy to see. The panels share their axes, which is essential: panels with independently scaled axes invite comparisons the scales do not support. Shared axes can also be a cost when groups differ greatly in range, in which case the choice should be stated.
Small multiples carry their own risk. The panels for the smaller groups rest on few adults per cell (the 70-and-over men in the "other Hispanic" panel number 23; the smallest cell, the 18-and-19-year-old men in that group, has 8), and their wiggles are mostly sampling variation. Printing $n$ in each panel title, as here, is a minimum.
Many features: parallel coordinates
When there are more features than a scatterplot matrix can show, parallel coordinates draw one vertical axis per feature, side by side, and each observation as a line that crosses every axis at its value (Inselberg, 1985; Wegman, 1990). A $p$-dimensional point becomes a polyline with $p$ vertices, so any number of features fits on one page.
Parallel coordinates show which features separate groups, which observations are unusual on several features at once, and which pairs of adjacent features are related: lines that run parallel between two axes indicate a positive correlation, and lines that cross in an X between them indicate a negative one. For the wines, the cultivars differ most in flavanoids (their means differ by up to 2.2 standard deviations), in OD280/OD315 (2.1), in proline (1.9) and in color intensity (1.9), which agrees with what principal component analysis finds when it reads its components.
The display has strong requirements. Features must be rescaled to a common range, here by standardizing, or the axis with the largest numbers dominates. The order of the axes matters, since only adjacent pairs can be compared directly, and a good order is a choice; here it was chosen to separate cultivars. With more than a few hundred observations the lines merge, and transparency, highlighting a subset or plotting group means becomes necessary. Interactive tools that let the reader reorder axes and brush a range on one axis to highlight the corresponding lines make parallel coordinates far more useful than any static version.
Two categorical features
Two categorical features are summarized by a contingency table of counts (Exploring Data with Groups and Pivot Tables), and drawn as bars: grouped side by side, stacked one above the other, or stacked to 100% to compare proportions. Which form to use depends on which comparison the reader should make most easily, and the perceptual argument for choosing among them is set out in Best Practices for Visualizing Data, which draws the blood pressure categories of the 2017 guideline by age group in all three forms.
Choosing a display
| Features | Display | Watch for |
|---|---|---|
| Two numerical | scatter plot | overplotting from size or discreteness: jitter, transparency, hexagonal bins |
| A handful of numerical | scatterplot matrix | grows as $p^2$; strongest panels often trivial |
| Many numerical | correlation heatmap, then scatter plots of the pairs it points to | nonlinearity, groups, skew, different rows behind each cell |
| Numerical by a categorical | color and shape, or one panel per category | crowding hides the group drawn first; panels need shared axes and printed $n$ |
| Many numerical, with groups | parallel coordinates | rescaling, axis order, line clutter |
In Python
import numpy as np
import pandas as pd
import seaborn as sns
cols = ["age", "sbp", "bmi", "waist", "hdl"]
sns.pairplot(adults[cols + ["sex"]].dropna(), hue="sex", plot_kws=dict(s=5, alpha=0.3))
pd.plotting.scatter_matrix(adults[cols], alpha=0.2, figsize=(8, 8))
adults.plot.hexbin(x="age", y="sbp", gridsize=30) # hexagonal bins
r = adults[cols].corr() # Pearson, pairwise complete
rho = adults[cols].corr(method="spearman") # rank correlation
sns.clustermap(r, cmap="vlag", vmin=-1, vmax=1, annot=True, fmt=".2f",
metric="correlation") # heatmap ordered by clustering
sns.relplot(data=adults, x="age", y="sbp", hue="sex", col="race", col_wrap=3,
kind="line", errorbar=None) # small multiples of means
pd.plotting.parallel_coordinates(wine_std, "cultivar", alpha=0.3)
seaborn.clustermap orders the rows and columns by hierarchical clustering; its metric="correlation" clusters the rows of the correlation matrix by their correlation distance, which is close to, but not the same as, the $1 - |r|$ ordering used in the figure. Every number in this article is computed by tools/experiments/data-science/visualizing-multiple-features.py in the site's repository, from the NHANES table built by tools/experiments/data-science/exploredata.py and scikit-learn's copy of the wine data; the figures are drawn by tools/figures/visualizing-multiple-features.py.
References
- Aeberhard, S., and Forina, M. (1992). Wine [Dataset]. UCI Machine Learning Repository. https://doi.org/10.24432/C5PC7J record · 8 pages
- Becker, R. A., Cleveland, W. S., and Shyu, M.-J. (1996). The visual design and control of trellis display. Journal of Computational and Graphical Statistics, 5(2), 123–155. record
- Carr, D. B., Littlefield, R. J., Nicholson, W. L., and Littlefield, J. S. (1987). Scatterplot matrix techniques for large N. Journal of the American Statistical Association, 82(398), 424–436. record
- Chambers, J. M., Cleveland, W. S., Kleiner, B., and Tukey, P. A. (1983). Graphical Methods for Data Analysis. Wadsworth. record
- Cleveland, W. S., and McGill, R. (1984). Graphical perception: Theory, experimentation, and application to the development of graphical methods. Journal of the American Statistical Association, 79(387), 531–554. record · 2 pages
- Inselberg, A. (1985). The plane with parallel coordinates. The Visual Computer, 1(2), 69–91. record
- National Center for Health Statistics (2020). National Health and Nutrition Examination Survey 2017–2018: Demographics (DEMO_J), Blood Pressure (BPX_J), Body Measures (BMX_J), Glycohemoglobin (GHB_J), Total Cholesterol (TCHOL_J), HDL Cholesterol (HDL_J), High-Sensitivity C-Reactive Protein (HSCRP_J) and Lead, Cadmium, Total Mercury, Selenium and Manganese in Blood (PBCD_J) data files and documentation. Centers for Disease Control and Prevention. https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2017 record · 45 pages
- Tufte, E. R. (1983). The Visual Display of Quantitative Information. Graphics Press. record · 2 pages
- Wegman, E. J. (1990). Hyperdimensional data analysis using parallel coordinates. Journal of the American Statistical Association, 85(411), 664–675. record