Unlisted
Edited
Oct 14, 2024
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barY(tidy, {
offset: function (facets, Y1, Y2, Z) {
for (const series of facets) {
for (const I of series) {
const basis = d3.max(I, (i) => Y2[i]);
for (const i of I)
if (Z[i] === "<10" || Z[i] === "10-19") {
Y1[i] = Y2[i] = NaN;
} else {
Y1[i] /= basis;
Y2[i] /= basis;
}
}
}
},
x: "state",
y: "population",
fill: "age",
sort: { color: null, x: "-y" }
})
]
})
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barY(transformed, {
x: "state",
y: "percentage",
filter: (d) => d.age !== "<10" && d.age !== "10-19",
fill: "age",
sort: { color: null, x: "-y" }
})
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barY(tidy, {
offset: "normalize",
filter: (d) => d.age !== "<10" && d.age !== "10-19",
x: "state",
y: "population",
fill: "age",
sort: { color: null, x: "-y" }
})
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barY(tidy, {
filter: (d) => d.age !== "<10" && d.age !== "10-19",
...Plot.stackY({
offset: "normalize",
x: "state",
y: "population",
fill: "age",
sort: { color: null, x: "-y" }
})
})
]
})
Insert cell
Insert cell
import { tidy } from "@observablehq/plot-stacked-bar-chart"
Insert cell
transformed = aq
.from(tidy)
.groupby("state")
.derive({ statePop: (d) => aq.op.sum(d.population) })
.ungroup()
.derive({ percentage: (d) => d.population / d.statePop })
Insert cell
transformed
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more