liveBook · Manning

Beeswarm plot

As explained in D3.js in Action, Third Edition

A beeswarm plot is a visualization that positions individual data points along an axis while avoiding overlap through perpendicular displacement.

A beeswarm plot shows a distribution without hiding its individual data points. Each point sits near the position for its value, while nearby points shift to avoid overlapping. The result makes both the overall shape and the individual observations visible. section 12.4.1

Build one with a D3 force simulation:

  • Create one node per observation and draw each node as an SVG circle. section 12.4.1
  • Use forceX(d => xScale(d.value)) to pull each circle toward its value on the horizontal axis. Use forceY(0) to pull circles toward the vertical center. section 12.4.1
  • Add forceCollide().radius(7) to keep circles apart, then update their displayed positions on each simulation tick. section 12.4.1

A distribution of 300 values

Generate 300 values with d3.randomNormal(), then apply the three forces to their circles. The resulting swarm reveals the normal distribution while retaining every point. Treat the horizontal positions as approximate: the simulation balances the value-positioning force against centering and collision, so a circle may not land exactly at its value. section 12.4.1

Figure 12.15 Beeswarm plot representing normally distributed values
figure

The finished plot shows how individual points remain visible within the distribution. section 12.4.1

How D3.js in Action, Third Edition covers it

How deep it goes
Works through D3 positioning and collision forces, simulation ticks, and the trade-off that final value positions are approximate.
Examples
Uses 300 normally distributed values and a separate character chart with color and size encodings.
What you can do after
Build it from scratch with D3 forceSimulation, forceX, forceY, and forceCollide.
How it connects
Connects force-directed layout, collision detection, SVG circles, and distributions.

Also explained in

Compare how 2 books explain beeswarm plot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.