position-beeswarm
Density-shaped per-row offset on x, or on y with orientation: "y".
For every bucket of rows sharing a swarm-axis value (x by default), each point is offset along that axis by width × (2·v − 1) × d, where v walks the van der Corput base-2 sequence in value order and d is the point’s Gaussian kernel density estimate normalised to the bucket’s peak. Dense regions spread wide, sparse tails stay close to the spine, and the result is a violin-shaped swarm.
A discrete (categorical) x is swarmed directly on its level positions, keeping the axis labels. as-factor is only needed to force a numeric column onto a discrete axis.
Usage
position-beeswarm(
width: 0.4,
adjust: 1,
orientation: auto,
method: "quasirandom",
binwidth: auto,
spacing: auto,
)Parameters
| Parameter | Default | Description |
|---|---|---|
width |
0.4 |
Maximum absolute offset applied along the swarm axis, in that axis’s data units. |
adjust |
1 |
Bandwidth multiplier for the density estimate, greater than 0: adjust: 0.5 halves the smoothing. |
orientation |
auto |
Axis the swarm spreads along: "x", "y", or auto. auto picks "y" when only the y axis is discrete, and "x" otherwise. |
method |
"quasirandom" |
"quasirandom" (default) spreads points by local density. "center" bins the value axis and stacks the points of each bin side by side, centred on the row, so the width of a stack shows the number of points. width caps nothing under "center". |
binwidth |
auto |
"center" only. Size of a bin on the value axis, in data units. auto is the range of the value column over the layer divided by 30, or 1 when the range is 0. Bins are centred on multiples of binwidth, starting from 0. Each point moves to the nearest multiple, so a value already on that grid does not move. |
spacing |
auto |
"center" only. Distance between neighbouring points of a stack, in data units of the swarm axis, the same for every row. auto is width / 10, which keeps points of the default size apart on a panel of ordinary size. |
Returns
Position dictionary with name: "beeswarm", consumed by plot.
Examples
Swarm overplotted points per group; a tighter width keeps the cloud close to its spine.
#let d = ()
#for grp in ("a", "b") {
for i in range(0, 50) {
d.push((grp: grp, y: calc.sin(i * 0.7) + calc.sin(i * 1.9) + (if grp == "b" { 4 } else { 0 })))
}
}
#plot(
data: d,
mapping: aes(x: as-factor("grp"), y: "y"),
layers: (geom-point(size: 2pt, position: position-beeswarm(width: 0.25)),),
width: 10cm,
height: 6cm,
)A discrete y axis swarms up and down around each level:
#let d = ()
#for grp in ("a", "b") {
for i in range(0, 50) {
d.push((grp: grp, x: calc.sin(i * 0.7) + calc.sin(i * 1.9) + (if grp == "b" { 4 } else { 0 })))
}
}
#plot(
data: d,
mapping: aes(x: "x", y: as-factor("grp")),
layers: (geom-point(size: 2pt, position: position-beeswarm(width: 0.25)),),
width: 10cm,
height: 6cm,
)Count-based stacks: each row grows wider with its number of points.
#let d = ()
#for i in range(0, 40) { d.push((grp: "a", y: calc.rem(i * 7, 11))) }
#for i in range(0, 12) { d.push((grp: "b", y: calc.rem(i * 5, 11))) }
#plot(
data: d,
mapping: aes(x: as-factor("grp"), y: "y"),
layers: (geom-point(size: 2pt, position: position-beeswarm(method: "center", binwidth: 1)),),
width: 10cm,
height: 6cm,
)