Unlisted
Edited
Mar 14, 2023
Insert cell
Insert cell
Plot.plot({
width,
marks: [
Plot.ruleX(data, Plot.binX({y1: loqr1, y2: hiqr2}, {x: "year", y: "value", interval: 2})),
Plot.rectY(data, Plot.binX({y1: "p25", y2: "p75"}, {x: "year", y: "value", fill: "#ccc", interval: 2})),
Plot.ruleY(data, Plot.binX({y: "p50"}, {x: "year", y: "value", strokeWidth: 2, interval: 2})),
Plot.dot(data, Plot.map({y: oqr}, {x: "year", y: "value", z: (d) => Math.floor(d.year * 2)}))
]
})
Insert cell
// A map function that returns only outliers, returning NaN for non-outliers
function oqr(values) {
const r1 = loqr1(values);
const r2 = hiqr2(values);
return values.map((v) => (v < r1 || v > r2 ? v : NaN));
}
Insert cell
function loqr1(values) {
const lo = d3.quantile(values, 0.25) * 2.5 - d3.quantile(values, 0.75) * 1.5;
return d3.min(values, (d) => (d >= lo ? d : NaN));
}
Insert cell
function hiqr2(values) {
const hi = d3.quantile(values, 0.75) * 2.5 - d3.quantile(values, 0.25) * 1.5;
return d3.max(values, (d) => (d <= hi ? d : NaN));
}
Insert cell
data = FileAttachment("test_data.csv").csv({typed: true})
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more