Plot.plot({
x: {
label: "Population (millions) →",
transform: (d) => d * 1e-6
},
y: {
label: "↑ Share of vaccinated people per age group",
tickFormat: "%"
},
color: {
domain: ["all", "one", "full"],
range: ["#ddd", "lightblue", "steelblue"]
},
marks: [
Plot.rectY(data, {
...Plot.stackX({ x: "population" }),
y: 1,
fill: () => "all",
insetLeft: 0.5,
insetRight: 0.5
}),
Plot.rectY(data, {
...Plot.stackX({ x: "population" }),
y: one_adjust,
fill: () => "one",
insetLeft: 0.5,
insetRight: 0.5
}),
Plot.rectY(data, {
...Plot.stackX({ x: "population" }),
y: full_adjust,
fill: () => "full",
insetLeft: 0.5,
insetRight: 0.5
}),
Plot.text(data, {
...Plot.stackX({ x: "population" }),
y: 1,
fill: "black",
text: "Age group",
dy: 11
}),
Plot.text(data, {
...Plot.stackX({ x: "population" }),
y: 1,
fill: "black",
text: (d) => `${+(d.population * 1e-6).toFixed(1)}M`,
dy: 23,
fontSize: 8
}),
Plot.text(data, {
...Plot.stackX({ x: "population" }),
y: (d) => (one_adjust(d) > 0.05 ? one_adjust(d) : null),
fill: "white",
text: (d) => percent(one_adjust(d)),
dy: 11
}),
Plot.text(data, {
...Plot.stackX({ x: "population" }),
y: (d) => (one_adjust(d) <= 0.05 ? one_adjust(d) : null),
fill: "white",
text: (d) => percent(one_adjust(d)),
dy: -5
}),
Plot.text(data, {
...Plot.stackX({ x: "population" }),
y: (d) => (full_adjust(d) > 0.05 ? full_adjust(d) : null),
fill: "white",
text: (d) => percent(full_adjust(d)),
dy: 11
}),
Plot.frame()
],
width
})