Skip to content

Population Pyramid ​

The population of Germany by age, sex and marital status, from the German Federal Institute for Population Research (BiB). Men go left as negative values, and cm.stackX stacks each side per age. After Observable Plot.

js
const population = await d3.csv("/data/population-pyramid.csv", d3.autoType);

const renderer = cm.svg({width: 500, height: 500});
const root = cm.flow().renderer(renderer);

root
  .data(population)
  .transform(cm.encode, {
    x: (d) => d.population * (d.sex === "M" ? -1 : 1),
    y: "age",
    z: (d) => `${d.sex},${d.category}`,
    fill: "category",
  })
  .transform(cm.stackX)
  .transform(cm.plane, {
    fill: {
      domain: ["ledig", "verheiratet", "geschieden", "verwitwet"],
      range: ["#69AEBE", "#F1757A", "#F2B678", "#989D9E"],
    },
    x: {label: "← men · population (thousands) · women →", tickFormat: Math.abs},
    y: {grid: true},
  })
  .add(cm.areaX);

root.render();
document.body.append(renderer.node());