2  Scatter plots: colour, shape, overplotting, labels

2.1 Mapping vs setting

The most common stumbling block in ggplot2 is wanting “all points blue” but writing the colour inside aes().

  • Mapping: appearance that changes with the values of a data column → inside aes() in ggplot2; hue=, size=, style= in seaborn
  • Setting: the same appearance for every point → an argument of geom_xxx() in ggplot2; color=, s=, marker= in seaborn / matplotlib

2.2 Colour palettes: using the same colours in both languages

The default palettes differ between R and Python. If you mix figures from both in a presentation or paper, the reliable approach is to choose the colours yourself and use them in both. Here we pick 3 colours from the Okabe-Ito palette, which takes colour-vision diversity into account.

If you specify colours by name (species name → colour), then the same species always gets the same colour, regardless of the order in the data or whether some species are absent from a facet.

2.2.1 Colour for continuous values: viridis

When mapping a continuous variable to colour, the safe choice is a viridis-family palette, whose lightness changes uniformly so the order is readable even in black-and-white print.

WarningAvoid rainbow palettes (jet, rainbow)

Because the lightness of a rainbow palette is not uniform, you see boundaries that don’t really exist around the yellows and light blues. Use a viridis-family palette for continuous values, and a diverging palette with white in the middle (such as RdBu) for quantities split into positive and negative (correlation coefficients, differences).

2.3 When points overlap: a scatter plot of 10,000 people

Height and weight of just over 10,000 people aged 20 and over from NHANES. Drawn as is, the middle is filled in solid and you can’t tell where most people are.

2.4 Labelling points

We label only the heaviest penguin of each species with its body mass. Placing labels so they don’t overlap the points or other labels is surprisingly hard.

geom_text_repel() / geom_label_repel() from ggrepel automatically search for positions where labels don’t overlap each other or the points, and draw leader lines when needed.

matplotlib’s ax.annotate() lets you specify the point being annotated (xy) and the text position (xytext) separately. With textcoords="offset points", you give the offset from the point in points, so it doesn’t shift even if the axis range changes. To avoid overlaps automatically, use the adjustText package (it is not available in the browser Python).

2.5 Adding marginal distributions

Adding the distribution of each variable above and to the right of a scatter plot lets you see the relationship between the two variables and each variable’s distribution at the same time.

To put histograms or densities in the margins, combine figures with patchwork in Chapter 8.

seaborn’s jointplot() draws the scatter plot and the marginal distributions in one line (a figure-level function).

ImportantChapter summary
  • Distinguish mapping (changes with column values: inside aes(), hue=) from setting (the same for everything: outside aes(), color=)
  • Choose colours yourself, by name, and use them in both languages. viridis for continuous values; avoid rainbow palettes
  • With many points: small and transparent, count per cell, or contours
  • Labels: ggrepel in R, ax.annotate() in Python (separate xy and xytext)