Plot stacked Area chart with two categories columns

Im new in Observable Plot and have a problem.
How to stack two categories in data correctly in Plot so that stacked area chart does not break?

The issue in this chart is that the dataset has two series (for females and males), so it’s not expressable as a simple stacked area chart. You need to:

  • add M and F together for a total population analysis, or
  • filter on M or F (`filter: d => d.sex === “female”`), or
  • facet by sex (`fx: “sex”`)

The last option gives you this:

Yes, you’re right. I think it may be possible to add M and F series with Plot.stack and after that make stacked area chart with another Plot.stack with fill by category?

You could use `z` to make distinct areas for each {sex, category} combination like so:

Plot.plot({
  marks: [
    Plot.areaY(
      data,
      Plot.stackY(
        {
          order: "category",
          reverse: true
        },
        {
          x: "age",
          y: "population",
          fill: "category",
          z: (d) => `${d.sex} ${d.category}`
        }
      )
    )
  ]
})

1 Like

Thanks!