Diverging Bars
The change in population of each state from 2010 to 2019, in bars that grow left or right of 0, colored by sign, with x axes at the top and the bottom. Two cm.label marks place the names along 0, away from their bars, and the changes past their ends. After Observable Plot.
js
const statepop = await d3.csv("/data/us-state-population-2010-2019.csv", d3.autoType);
const change = (d) => (d[2019] - d[2010]) / d[2010];
const renderer = cm.svg({width: 640, height: 800});
const root = cm.flow().renderer(renderer);
root
.data(statepop)
.transform(cm.encode, {y: "State", x: change, fill: (d) => Math.sign(change(d))})
.transform(cm.spreadY, {sort: "x"})
.transform(cm.plane, {
marginLeft: 50,
marginRight: 48,
// Like Plot's, percent maps the changes to percentages, signed by tickFormat.
x: {
label: "← decrease · Change in population, 2010–2019 (%) · increase →",
axis: "both",
labelAnchor: "center",
percent: true,
tickFormat: "+",
},
y: {axis: null},
fill: {type: "ordinal", range: [d3.schemePiYG[3][0], d3.schemePiYG[3][2]]},
})
.add(cm.rect)
.add(cm.label, {
text: "State",
position: (d) => (change(d.datum) >= 0 ? "left" : "right"),
padding: 9,
})
.add(cm.label, {
text: (d) => change(d.datum),
format: "+.1%",
position: (d) => (change(d.datum) >= 0 ? "right" : "left"),
})
.add(cm.annotate(cm.link), {x: 0});
root.render();
document.body.append(renderer.node());