svgbrushabledot-plot
Dot plot (Wilkinson)
What does a small sample look like, observation by observation?
How to read it
- Count the dots: each one is one observation, stacked in its bin.
- Look at the stack heights for the shape, as you would on a histogram.
- Notice isolated dots: in a small sample each one matters.
- Callout 1: A tall stack: many observations share nearly the same value.
- Callout 2: An isolated dot: one observation far from the rest.
Watch out
- Stacks shift a little with the bin width: read counts, not exact positions.
Defaults
- Range ÷ binwidth
- 30
- How many bins fit across the range: more bins give more detail and shorter stacks.
- Range 5 to 80, step 1
Pinned in the code
- Binning: Wilkinson dot-density (ggplot2 `geom_dotplot(method = "dotdensity")`): scan sorted values; a value ≥ the current bin end starts a bin [x, x + w).
- Stack centre = midpoint of the first and last value in the bin; binwidth w = range / 30 by default.
- Dot diameter = w in x units, shrunk if the tallest stack would not fit (stacks never overflow).
# Dot plot (Wilkinson) — Codesplash Chart Lab (recipe "dot-plot")
# controls: bins=30
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("dot-plot-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
def dot_density(x, w):
"""Wilkinson dot-density binning (ggplot2 method = "dotdensity"): scan the sorted values;
a value ≥ the current bin end starts a new bin [x, x + w). Returns (centre, count) per stack."""
stacks, end = [], -np.inf
for v in np.sort(x):
if v >= end:
stacks.append([])
end = v + w
stacks[-1].append(v)
return [((s[0] + s[-1]) / 2, len(s)) for s in stacks] # centre = midpoint of first and last value
x = df["sleep_h"].dropna().to_numpy(dtype=float)
w = (x.max() - x.min()) / 30 # binwidth = range / 30
stacks = dot_density(x, w)
tallest = max(count for _, count in stacks)
fig, ax = plt.subplots(figsize=(7, 4))
for centre, count in stacks:
for j in range(count): # dot diameter = binwidth in x units
ax.add_patch(plt.Circle((centre, (j + 0.5) * w), 0.47 * w, color=PALETTE[0], linewidth=0))
ax.set_xlim(x.min() - w, x.max() + w)
ax.set_ylim(0, max(tallest * w, (x.max() - x.min()) / 4))
ax.set_aspect("equal")
ax.yaxis.set_visible(False)
ax.grid(axis="y", visible=False)
ax.set_xlabel("sleep_h")
plt.show()Needs pandas, numpy, matplotlib · reads dot-plot-demo.csv · app theme from the optional codesplash helper
# Dot plot (Wilkinson) — Codesplash Chart Lab (recipe "dot-plot")
# controls: bins=30
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("dot-plot-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
def dot_density(x, w):
"""Wilkinson dot-density binning (ggplot2 method = "dotdensity"): scan the sorted values;
a value ≥ the current bin end starts a new bin [x, x + w). Returns (centre, count) per stack."""
stacks, end = [], -np.inf
for v in np.sort(x):
if v >= end:
stacks.append([])
end = v + w
stacks[-1].append(v)
return [((s[0] + s[-1]) / 2, len(s)) for s in stacks] # centre = midpoint of first and last value
x = df["sleep_h"].dropna().to_numpy(dtype=float)
w = (x.max() - x.min()) / 30 # binwidth = range / 30
stacks = dot_density(x, w)
tallest = max(count for _, count in stacks)
fig = go.Figure()
for centre, count in stacks:
for j in range(count): # circles in data units: diameter = binwidth
fig.add_shape(type="circle", x0=centre - 0.47 * w, x1=centre + 0.47 * w, y0=(j + 0.03) * w, y1=(j + 0.97) * w, fillcolor=PALETTE[0], line_width=0)
fig.add_scatter(x=[s[0] for s in stacks], y=[s[1] * w for s in stacks], mode="markers", marker_opacity=0, hovertext=[f"{s[1]} values" for s in stacks], showlegend=False)
fig.update_xaxes(range=[x.min() - w, x.max() + w], title="sleep_h")
fig.update_yaxes(range=[0, max(tallest * w, (x.max() - x.min()) / 4)], scaleanchor="x", visible=False)
fig.show()Needs pandas, numpy, plotly · reads dot-plot-demo.csv · app theme from the optional codesplash helper
# Dot plot (Wilkinson) — Codesplash Chart Lab (recipe "dot-plot")
# controls: bins=30
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("dot-plot-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
x <- df[["sleep_h"]]
x <- sort(x[is.finite(x)])
w <- diff(range(x)) / 30 # binwidth = range / 30
# tallest Wilkinson stack (to keep dots inside the panel, as the app shrinks them)
bin <- integer(length(x)); b <- 0L; end <- -Inf
for (i in seq_along(x)) { if (x[i] >= end) { b <- b + 1L; end <- x[i] + w }; bin[i] <- b }
tallest <- max(tabulate(bin))
p <- ggplot(data.frame(x = x), aes(x)) +
# Wilkinson dot-density: a value >= the current bin end starts [x, x + w); stack centre = midpoint
geom_dotplot(method = "dotdensity", binwidth = w, stackratio = 1, fill = cs_cat[1], colour = NA,
dotsize = min(1, 0.9 / (tallest * w / diff(range(x)) * 1.6))) +
scale_y_continuous(NULL, breaks = NULL) +
labs(x = "sleep_h") + theme_cs
print(p)Needs ggplot2 · reads dot-plot-demo.csv · app theme from the optional codesplashr helper
-- Dot plot (Wilkinson) — Codesplash Chart Lab (recipe "dot-plot")
-- controls: bins=30
-- Wilkinson dot-density binning, binwidth w = range / 30: scan the sorted values; a value ≥ the
-- current bin end starts a new bin [x, x + w); stack centre = midpoint of its first and last value.
-- The scan is sequential (one recursion step per row) — fine for samples, slow for millions of rows.
WITH RECURSIVE v AS (
SELECT "sleep_h"::DOUBLE AS x, row_number() OVER (ORDER BY "sleep_h"::DOUBLE) AS i
FROM read_csv('dot-plot-demo.csv')
WHERE "sleep_h" IS NOT NULL AND isfinite("sleep_h"::DOUBLE)
),
w AS (SELECT (max(x) - min(x)) / 30 AS w FROM v),
scan(i, x, bin_start) AS (
SELECT i, x, x FROM v WHERE i = 1
UNION ALL
SELECT v.i, v.x, CASE WHEN v.x >= scan.bin_start + w.w THEN v.x ELSE scan.bin_start END
FROM scan JOIN v ON v.i = scan.i + 1 CROSS JOIN w
)
SELECT bin_start, (min(x) + max(x)) / 2 AS centre, count(*) AS dots
FROM scan
GROUP BY bin_start
ORDER BY bin_start;Needs duckdb · reads dot-plot-demo.csv