Plot.plot({
width: 928,
height: 500,
x: { label: null },
y: { tickFormat: "s", tickSpacing: 50, label: null },
color: { scheme: "Spectral" },
marks: [
Plot.barY(tidy, {
x: "state",
y: "population",
fill: "age",
sort: { color: null, x: "-y" }
}),
Plot.text(
tidy,
Plot.pointerX(
(({ x, y, ...options }) => ({ px: x, py: y, ...options }))(
Plot.stackY({
x: "state",
y: "population",
frameAnchor: "top-left",
dy: -20,
text: (d) =>
`${d["population"]} people ${d["age"]} years old in ${d["state"]}`
})
)
)
),
Plot.tip(
tidy,
Plot.pointerX(
Plot.stackY({
x: "state",
y: "population",
title: (d) =>
`${d["population"]} people ${d["age"]} years old in ${d["state"]}`
})
)
)
]
})