---
webr:
packages: [dplyr, ggplot2, ggrepel, MASS]
resources: [R/setup.R, py/setup.py, data/penguins.csv, data/nhanes_adults.csv, fonts/NotoSansJP-Regular.ttf, fonts/NotoSansJP-Bold.ttf]
cell-options:
autorun: true
pyodide:
packages: [pandas, matplotlib, seaborn]
cell-options:
autorun: true
---
{{< include ./_extensions/r-wasm/live/_knitr.qmd >}}
# Scatter plots: colour, shape, overplotting, labels {#sec-scatter}
```{webr}
#| include: false
source("R/setup.R")
```
```{pyodide}
#| include: false
exec(open("py/setup.py").read())
```
## Mapping vs setting
The most common stumbling block in ggplot2 is **wanting "all points blue" but writing the colour inside `aes()`**.
::: {.panel-tabset group="lang"}
## R (ggplot2)
```{webr}
# Wrong: a constant inside aes() creates a "group" named "blue"
ggplot(penguins, aes(flipper_length_mm, body_mass_g, colour = "blue")) + geom_point()
```
```{webr}
# Right: appearance unrelated to variables goes outside aes() (as a geom argument)
ggplot(penguins, aes(flipper_length_mm, body_mass_g)) + geom_point(colour = "steelblue")
```
## Python (seaborn / matplotlib)
```{pyodide}
fig, axes = plt.subplots(1, 2, figsize=(9, 3.6))
# hue= maps to a "column"; color= sets the colour for everything
sns.scatterplot(data=penguins, x="flipper_length_mm", y="body_mass_g", hue="species", ax=axes[0])
sns.scatterplot(data=penguins, x="flipper_length_mm", y="body_mass_g", color="steelblue", ax=axes[1])
axes[0].set_title("hue=\"species\" (mapping)")
axes[1].set_title("color=\"steelblue\" (setting)")
plt.tight_layout()
plt.show()
```
:::
- **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
## 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.
::: {.panel-tabset group="lang"}
## R (ggplot2)
```{webr}
sp_cols <- c(Adelie = "#E69F00", Chinstrap = "#56B4E9", Gentoo = "#009E73")
ggplot(penguins, aes(flipper_length_mm, body_mass_g, colour = species)) +
geom_point(size = 2) +
scale_colour_manual(values = sp_cols) +
labs(x = "Flipper length (mm)", y = "Body mass (g)", colour = "Species")
```
## Python (seaborn / matplotlib)
```{pyodide}
sp_cols = {"Adelie": "#E69F00", "Chinstrap": "#56B4E9", "Gentoo": "#009E73"}
fig, ax = plt.subplots()
sns.scatterplot(data=penguins, x="flipper_length_mm", y="body_mass_g",
hue="species", palette=sp_cols, s=30, ax=ax)
ax.set(xlabel="Flipper length (mm)", ylabel="Body mass (g)")
ax.legend(title="Species")
plt.show()
```
:::
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.
### 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.
::: {.panel-tabset group="lang"}
## R (ggplot2)
```{webr}
ggplot(penguins, aes(bill_length_mm, bill_depth_mm, colour = body_mass_g)) +
geom_point(size = 2) +
scale_colour_viridis_c(name = "Body mass (g)") +
labs(x = "Bill length (mm)", y = "Bill depth (mm)")
```
## Python (seaborn / matplotlib)
```{pyodide}
fig, ax = plt.subplots()
sc = ax.scatter(penguins["bill_length_mm"], penguins["bill_depth_mm"],
c=penguins["body_mass_g"], cmap="viridis", s=20)
fig.colorbar(sc, ax=ax, label="Body mass (g)")
ax.set(xlabel="Bill length (mm)", ylabel="Bill depth (mm)")
plt.show()
```
:::
::: {.callout-warning}
## Avoid 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).
:::
## 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**.
::: {.panel-tabset group="lang"}
## R (ggplot2)
```{webr}
ggplot(nhanes, aes(Height, Weight)) + geom_point() +
labs(x = "Height (cm)", y = "Weight (kg)", title = "As is")
```
```{webr}
# 1. Small and transparent
ggplot(nhanes, aes(Height, Weight)) + geom_point(size = 0.5, alpha = 0.1) +
labs(x = "Height (cm)", y = "Weight (kg)", title = "Small and transparent")
```
```{webr}
# 2. Count per cell (2D histogram)
ggplot(nhanes, aes(Height, Weight)) + geom_bin_2d(bins = 50) +
scale_fill_viridis_c(name = "Count") +
labs(x = "Height (cm)", y = "Weight (kg)", title = "Count per cell")
```
```{webr}
# 3. Overlay contours (2D density)
ggplot(nhanes, aes(Height, Weight, colour = Gender)) +
geom_point(size = 0.4, alpha = 0.08) +
geom_density_2d(bins = 6) +
labs(x = "Height (cm)", y = "Weight (kg)", colour = "Sex", title = "Density contours")
```
## Python (seaborn / matplotlib)
```{pyodide}
fig, axes = plt.subplots(1, 3, figsize=(12, 3.8), sharex=True, sharey=True)
axes[0].scatter(nhanes["Height"], nhanes["Weight"])
axes[0].set_title("As is")
axes[1].scatter(nhanes["Height"], nhanes["Weight"], s=2, alpha=0.1)
axes[1].set_title("Small and transparent")
hb = axes[2].hexbin(nhanes["Height"], nhanes["Weight"], gridsize=45, cmap="viridis", mincnt=1)
fig.colorbar(hb, ax=axes[2], label="Count")
axes[2].set_title("Count per hexagonal cell")
for a in axes:
a.set_xlabel("Height (cm)")
axes[0].set_ylabel("Weight (kg)")
plt.tight_layout()
plt.show()
```
```{pyodide}
fig, ax = plt.subplots()
sns.scatterplot(data=nhanes, x="Height", y="Weight", hue="Gender", s=3, alpha=0.1, legend=False, ax=ax)
sample = nhanes.dropna(subset=["Height", "Weight"]).sample(3000, random_state=1) # estimate the density on a subset (for speed)
sns.kdeplot(data=sample, x="Height", y="Weight", hue="Gender", levels=6, ax=ax)
ax.set(xlabel="Height (cm)", ylabel="Weight (kg)", title="Density contours")
plt.show()
```
:::
## 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.
::: {.panel-tabset group="lang"}
## R (ggplot2)
```{webr}
library(ggrepel)
heaviest <- penguins |> filter(!is.na(body_mass_g)) |>
group_by(species) |> slice_max(body_mass_g, n = 1, with_ties = FALSE)
ggplot(penguins, aes(flipper_length_mm, body_mass_g, colour = species)) +
geom_point(alpha = 0.6) +
geom_point(data = heaviest, size = 3, shape = 21, fill = "white", stroke = 1.5) +
geom_label_repel(data = heaviest, aes(label = paste0(species, " ", body_mass_g, " g")),
show.legend = FALSE, min.segment.length = 0, box.padding = 0.6) +
labs(x = "Flipper length (mm)", y = "Body mass (g)", colour = "Species")
```
`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.
## Python (seaborn / matplotlib)
```{pyodide}
heaviest = penguins.dropna(subset=["body_mass_g"]).loc[lambda d: d.groupby("species")["body_mass_g"].idxmax()]
fig, ax = plt.subplots()
sns.scatterplot(data=penguins, x="flipper_length_mm", y="body_mass_g", hue="species", alpha=0.6, ax=ax)
offsets = {"Adelie": (-70, 15), "Chinstrap": (-60, 30), "Gentoo": (-110, -10)} # offset from the point (points)
for _, r in heaviest.iterrows():
ax.annotate(f"{r.species} {r.body_mass_g:.0f} g",
xy=(r.flipper_length_mm, r.body_mass_g), # the point to annotate (data coordinates)
xytext=offsets[r.species], textcoords="offset points", # text position (offset from the point)
arrowprops=dict(arrowstyle="-", color="grey"),
bbox=dict(boxstyle="round", fc="white", ec="grey"))
ax.set(xlabel="Flipper length (mm)", ylabel="Body mass (g)")
plt.show()
```
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).
:::
## 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.
::: {.panel-tabset group="lang"}
## R (ggplot2)
```{webr}
# In ggplot2, the easy way is to show the distribution with a "rug" (thin lines) along the axes
ggplot(penguins, aes(bill_length_mm, bill_depth_mm, colour = species)) +
geom_point(alpha = 0.7) +
geom_rug(alpha = 0.4, length = unit(0.02, "npc")) +
labs(x = "Bill length (mm)", y = "Bill depth (mm)", colour = "Species")
```
To put histograms or densities in the margins, combine figures with `patchwork` in Chapter 8.
## Python (seaborn / matplotlib)
```{pyodide}
g = sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", height=5)
g.set_axis_labels("Bill length (mm)", "Bill depth (mm)")
plt.show()
```
seaborn's `jointplot()` draws the scatter plot and the marginal distributions in one line (a figure-level function).
:::
::: {.callout-important}
## Chapter 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`)
:::