Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 44 ยท Shapes and Paths

Scatter Plots, Bubble Charts and Symbols in D3

Build scatter plots and bubble charts in D3 with sqrt radius scales, d3.symbol shapes for categories, opacity and jitter against overplotting.

A scatter plot places one mark per observation at its (x, y) position and reveals relationships, clusters and outliers. Adding a third variable as the mark's size gives a bubble chart; adding a fourth as color or shape makes it a rich multivariate view. D3 makes this simple: one join of circles (or symbols) and two or three scales.

A basic scatter plot

You already built one in the Axes lesson. The only structure it needs is a join, one scale per axis and a mark per datum.

JavaScript
g.selectAll("circle")
  .data(data)
  .join("circle")
  .attr("cx", d => x(d.height))
  .attr("cy", d => y(d.weight))
  .attr("r", 4);

Choose the domains with d3.extent and .nice(). Unlike bars, a scatter plot's axes do not need to start at zero, because position, not length, carries the information. Pick the range that shows the data well.

Bubble charts: a third variable as area

Map the third variable to the circle's radius with a square-root scale, so that area matches the value (see Power, Log and Time Scales). Set the minimum radius to something visible (not 0) only if tiny values would vanish.

A bubble chart: income, life expectancy, population

Two design touches matter here. Large bubbles are drawn first (sorted by population descending) so small ones are never hidden underneath. And a semi-transparent fill (fill-opacity) lets overlapping bubbles stay readable.

Overplotting

With hundreds or thousands of points, marks hide each other. Remedies:

  • Lower opacity (0.2 to 0.5): dense regions get darker.
  • Smaller marks (radius 1.5 to 2.5).
  • Jitter discrete values: add a small random offset to x or y.
  • Binning: show 2D bins as a heatmap or hexagons (the d3-hexbin plugin) instead of points.
  • Contours: d3.contourDensity draws density outlines (see chapter 9 projects).

Symbols: shapes for categories

Color alone is not accessible. A symbol generator lets you vary the shape too. d3.symbol() returns a path string for a built-in shape at a given area in square pixels.

JavaScript
const symbol = d3.symbol().type(d3.symbolCircle).size(80);
symbol();       // path string for a circle with area 80

Built-in types: symbolCircle, symbolCross, symbolDiamond, symbolSquare, symbolStar, symbolTriangle, symbolWye, and the stroke-friendly symbolPlus, symbolTimes, symbolAsterisk, symbolDiamond2, symbolSquare2, symbolTriangle2. The array d3.symbolsFill and d3.symbolsStroke list the filled and the outline-friendly sets in a good order for categories. Pair them with an ordinal scale:

JavaScript
const shape = d3.scaleOrdinal(categories, d3.symbolsFill);
path.attr("d", d => d3.symbol(shape(d.category), 64)());

Note that size is the area (square pixels), not the radius or width. Symbols are drawn around (0, 0), so position them with transform="translate(x, y)".

Shape and color for categories

The legend uses the same scales as the marks, which guarantees they match.

Finding the point under the mouse: Delaunay

Hovering small circles is fiddly. A better approach is to find the nearest point to the pointer using a Delaunay triangulation (d3.Delaunay from d3-delaunay, part of d3). Build it once from the point coordinates, then find(x, y) returns the index of the nearest point, even when the mouse is not directly over it.

Nearest point with d3.Delaunay

d3.pointer(event) gives the mouse position relative to the SVG; interactions are the subject of chapter 6. The Delaunay structure makes the search fast even for tens of thousands of points.

For very large scatter plots (above roughly 10,000 points), SVG slows down because every mark is a DOM node. Switch to a <canvas> for drawing and keep D3's scales and Delaunay for the math.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use scaleSqrt for bubble radius?

    People read area. Radius proportional to the square root of the value gives area proportional to the value.
  2. 2. What is the size argument of d3.symbol().size(n)?

    Symbol size is the area, so doubling it does not double the width.
  3. 3. How can you reduce overplotting in a dense scatter plot?

    Transparency and smaller marks make density visible, and jitter separates identical values.
  4. 4. What does delaunay.find(x, y) return?

    find returns the index of the closest point, which lets you build easy hover interaction.

Summary

  • Scatter plots map two variables to position; bubble charts add a sqrt-scaled radius; color and symbol add more.
  • Scatter axes need not start at zero; draw large bubbles first and use transparency.
  • d3.symbol draws shapes by area; d3.symbolsFill and an ordinal scale give distinct shapes.
  • d3.Delaunay.find locates the nearest point for hover; switch to canvas for very large data.

Next: Paths, Curves and Links