Published
Edited
Sep 17, 2021
Importers
Insert cell
Insert cell
morley = FileAttachment("morley.csv").csv({typed: true})
Insert cell
Plot.plot({
y: {
grid: true,
inset: 6
},
marks: [
boxY(morley, {x: "Expt", y: "Speed"})
]
})
Insert cell
Plot.plot({
x: {
grid: true,
inset: 6
},
marks: [
boxX(morley, {x: "Speed", y: "Expt"})
]
})
Insert cell
Insert cell
function boxX(data, {
x = {transform: x => x},
y = null,
fill = "#ccc",
stroke = "currentColor",
...options
} = {}) {
const group = y == null ? Plot.groupZ : Plot.groupY;
return Plot.marks(
Plot.ruleY(data, group({x1: iqr1, x2: iqr2}, {x, y, stroke, ...options})),
Plot.barX(data, group({x1: quartile1, x2: quartile3}, {x, y, fill, ...options})),
Plot.tickX(data, group({x: "median"}, {x, y, stroke, strokeWidth: 2, ...options})),
Plot.dot(data, Plot.map({x: outliers}, {x, y, z: y, stroke, ...options}))
);
}
Insert cell
function boxY1(
data,
{
x = null,
y = { transform: x => x },
fill = "#ccc",
stroke = "currentColor",
thresholds = "sturges",
...options
} = {}
) {
const group = x == null ? Plot.groupZ : Plot.binX;
return Plot.marks(
Plot.ruleX(
data,
group({ y1: iqr1, y2: iqr2 }, { x, y, stroke, thresholds, ...options })
),
Plot.barY(
data,
group(
{ y1: quartile1, y2: quartile3 },
{ x, y, fill, thresholds, ...options }
)
),
Plot.tickY(
data,
group(
{ y: "median" },
{ x, y, stroke, strokeWidth: 2, thresholds, ...options }
)
),
Plot.dot(
data,
Plot.map({ y: outliers }, { x, y, z: x, stroke, thresholds, ...options })
)
);
}
Insert cell
function boxY(data, {
x = null,
y = {transform: x => x},
fill = "#ccc",
stroke = "currentColor",
...options
} = {}) {
const group = x == null ? Plot.groupZ : Plot.groupX;
return Plot.marks(
Plot.ruleX(data, group({y1: iqr1, y2: iqr2}, {x, y, stroke, ...options})),
Plot.barY(data, group({y1: quartile1, y2: quartile3}, {x, y, fill, ...options})),
Plot.tickY(data, group({y: "median"}, {x, y, stroke, strokeWidth: 2, ...options})),
Plot.dot(data, Plot.map({y: outliers}, {x, y, z: x, stroke, ...options}))
);
}
Insert cell
boxX([0, 3, 4.4, 4.5, 4.6, 5, 7]).plot()
Insert cell
Insert cell
function outliers(values) {
const r1 = iqr1(values);
const r2 = iqr2(values);
return values.map(v => v < r1 || v > r2 ? v : NaN);
}
Insert cell
Insert cell
iqr1 = V => {
const hi = quartile1(V);
const lo = hi - 1.5 * (quartile3(V) - hi);
return d3.min(V, v => lo <= v && v <= hi ? v : NaN);
}
Insert cell
iqr2 = V => {
const lo = quartile3(V);
const hi = lo + 1.5 * (lo - quartile1(V));
return d3.max(V, v => lo <= v && v <= hi ? v : NaN);
}
Insert cell
quartile1 = V => d3.quantile(V, 0.25)
Insert cell
quartile3 = V => d3.quantile(V, 0.75)
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