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
