holtzy / D3-graph-gallery

A collection of simple graphics made with D3.js
https://www.d3-graph-gallery.com
MIT License
792 stars 234 forks source link

Boxplot calculations are incorrect #10

Open jtr13 opened 4 years ago

jtr13 commented 4 years ago

The calculations below from https://www.d3-graph-gallery.com/graph/boxplot_basic.html are not correct.

var min = q1 - 1.5 * interQuantileRange
var max = q1 + 1.5 * interQuantileRange

Outliers are more than 1.5 IQR above Q3 and below Q1. Those cutoffs are called fences. The whiskers* are not fences: they indicate the highest and lowest nonoutlier data values.

See: https://ggplot2.tidyverse.org/reference/geom_boxplot.html

If you don't want to indicate outliers than just set min and max to the min and max values of the dataset.

lrusso96 commented 4 years ago

The correct way should be:

var min = d3.max([d3.min(data), q1 - 1.5 * interQuantileRange])
var max = d3.min([d3.max(data), q1 + 1.5 * interQuantileRange])

Hope it helps.