liveBook · Manning

Quantile quantization

As explained in D3.js in Action, Third Edition

A quantile scale maps continuous input values to discrete output values according to thresholds that divide the input distribution into groups of similar sizes.

Quantile quantization divides continuous data into groups of similar size by choosing thresholds from the input distribution. In D3, a quantile scale uses a discrete data domain and a discrete range; the range length determines how many groups are computed. section 6.4.1

For quartiles, configure the scale with four range values:

  • Pass the data values as the scale domain. section 6.4.1
  • Pass [0, 1, 2, 3] as the range to divide the data into four groups. section 6.4.1
  • Call quantiles() to obtain the three thresholds separating the first, second, and third quartiles. section 6.4.1

Comparing salary distributions

Filter the dataset into women’s and men’s salary arrays, create one quantile scale for each array, and calculate each group’s quartile thresholds. Use those thresholds with minimum, maximum, and median values to position a box plot’s rectangle, whiskers, and median line. section 6.4 section 6.4.1

Listing 6.5 Calculating quartile and min/max values (box-plot.js)
const femalesSalaries = data.filter(d => d.gender ===            #1
  "Female").map(d => d.salary);                   
const femalesQuartilesScale = d3.scaleQuantile() 
  .domain(femalesSalaries)               
  .range([0, 1, 2, 3]);                 

const femalesQuartiles = femalesQuartilesScale   #2
  .quantiles();      
const femalesExtent = d3.extent(femalesSalaries); 

const malesSalaries = data.filter(d => d.gender ===
  "Male").map(d => d.salary);                     
const malesQuartilesScale = d3.scaleQuantile()      
  .domain(malesSalaries)                            
  .range([0, 1, 2, 3]);    

const malesQuartiles = malesQuartilesScale.quantiles();
const malesExtent = d3.extent(malesSalaries);

Inspect the complete D3 calculation for separate female and male salary arrays, quantile scales, thresholds, and extents. section 6.4.1

How D3.js in Action, Third Edition covers it

How deep it goes
Defines discrete domains, ranges, and threshold access, but stops short of explaining the underlying threshold algorithm.
Examples
Uses women’s and men’s salaries to calculate quartile thresholds and construct comparative box plots.
What it assumes
Assumes JavaScript arrays and D3 data handling while demonstrating filtering, mapping, and scale configuration.
What you can do after
Apply it via d3.scaleQuantile() to calculate quartile thresholds for distribution visualizations.
How it connects
Connects quantile scales with quartiles, medians, box plots, salary distributions, and D3.

Also explained in

Compare how 2 books explain quantile quantization →

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.