liveBook · Manning

D3.scaleQuantile()

As explained in D3.js in Action, Third Edition

d3.scaleQuantile() is a D3-scale constructor that creates a quantile scale from a discrete input domain and discrete output range.

d3.scaleQuantile() creates a quantile scale from a discrete array of data values and a discrete output range. The range length determines how many groups the values are divided into. section 6.4.1

Use the scale to calculate quartiles from an array of values:

  • Filter the dataset and map the relevant field into a numeric array. section 6.4.1
  • Pass the array to domain() and provide four range values, such as [0, 1, 2, 3]. section 6.4.1
  • Call quantiles() to receive the three thresholds separating the four groups: the 25th percentile, median, and 75th percentile. section 6.4.1

Quartiles for a box plot

const femalesSalaries = data.filter(d => d.gender === "Female").map(d => d.salary); const femalesQuartilesScale = d3.scaleQuantile() .domain(femalesSalaries) .range([0, 1, 2, 3]); const femalesQuartiles = femalesQuartilesScale.quantiles(); const femalesExtent = d3.extent(femalesSalaries);

Combine the thresholds with d3.extent() for minimum and maximum values, then position the rectangle, median line, and whiskers of a box plot. section 6.4.1 section 6.6

How D3.js in Action, Third Edition covers it

How deep it goes
Explains domain, range, thresholds, and implementation steps without covering quantile algorithm internals.
Examples
Uses women’s and men’s salary arrays to calculate quartiles and minimum and maximum values for a box plot.
What it assumes
Assumes JavaScript arrays, filter(), map(), and D3 data while teaching scale construction from the example.
What you can do after
Apply d3.scaleQuantile() to calculate quartile thresholds and support box-plot positioning.
How it connects
Connects quantile thresholds with median, extent, box plots, histograms, and data filtering.

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.