# Stacked area

> Show how a total divides into parts over time with stacked areas, then normalize each period to 100% or center the stack as a streamgraph.

A stacked area chart adds series into a running total, so the top edge is the total and each band is one part. It answers how a whole is made up over time, and the reader can compare the total across periods but only judge a band's size by its thickness.

Use a [line chart](/docs/change-over-time/line/) when the reader needs to compare individual series, since only the bottom band sits on a flat baseline. Use [100% stacked bars](/docs/part-to-whole/stacked-100/) when the periods are few and shares matter more than totals.

## A basic stacked area

Put a nominal `color` channel on an `area` mark and set `stack: "zero"` on the y channel to stack the series from a zero baseline. Multi-series areas stack by default, so the setting makes the intent explicit. The x channel is ordinal here because the years are labels, not a time scale.

```json
{
  "mark": "area",
  "data": [
    {
      "year": "2015",
      "energy": 33.1,
      "source": "Oil"
    },
    {
      "year": "2016",
      "energy": 33.4,
      "source": "Oil"
    },
    {
      "year": "2017",
      "energy": 33.7,
      "source": "Oil"
    },
    {
      "year": "2018",
      "energy": 33.7,
      "source": "Oil"
    },
    {
      "year": "2019",
      "energy": 33.1,
      "source": "Oil"
    },
    {
      "year": "2020",
      "energy": 31.4,
      "source": "Oil"
    },
    {
      "year": "2021",
      "energy": 31.7,
      "source": "Oil"
    },
    {
      "year": "2022",
      "energy": 31.8,
      "source": "Oil"
    },
    {
      "year": "2015",
      "energy": 23.7,
      "source": "Natural Gas"
    },
    {
      "year": "2016",
      "energy": 23.9,
      "source": "Natural Gas"
    },
    {
      "year": "2017",
      "energy": 23.9,
      "source": "Natural Gas"
    },
    {
      "year": "2018",
      "energy": 24.1,
      "source": "Natural Gas"
    },
    {
      "year": "2019",
      "energy": 24.4,
      "source": "Natural Gas"
    },
    {
      "year": "2020",
      "energy": 24.7,
      "source": "Natural Gas"
    },
    {
      "year": "2021",
      "energy": 24.4,
      "source": "Natural Gas"
    },
    {
      "year": "2022",
      "energy": 23.8,
      "source": "Natural Gas"
    },
    {
      "year": "2015",
      "energy": 28.7,
      "source": "Coal"
    },
    {
      "year": "2016",
      "energy": 27.7,
      "source": "Coal"
    },
    {
      "year": "2017",
      "energy": 27.3,
      "source": "Coal"
    },
    {
      "year": "2018",
      "energy": 27,
      "source": "Coal"
    },
    {
      "year": "2019",
      "energy": 26.8,
      "source": "Coal"
    },
    {
      "year": "2020",
      "energy": 26.2,
      "source": "Coal"
    },
    {
      "year": "2021",
      "energy": 26.9,
      "source": "Coal"
    },
    {
      "year": "2022",
      "energy": 26.5,
      "source": "Coal"
    },
    {
      "year": "2015",
      "energy": 9.9,
      "source": "Renewables"
    },
    {
      "year": "2016",
      "energy": 10.4,
      "source": "Renewables"
    },
    {
      "year": "2017",
      "energy": 10.7,
      "source": "Renewables"
    },
    {
      "year": "2018",
      "energy": 10.9,
      "source": "Renewables"
    },
    {
      "year": "2019",
      "energy": 11.5,
      "source": "Renewables"
    },
    {
      "year": "2020",
      "energy": 13.2,
      "source": "Renewables"
    },
    {
      "year": "2021",
      "energy": 12.8,
      "source": "Renewables"
    },
    {
      "year": "2022",
      "energy": 13.9,
      "source": "Renewables"
    },
    {
      "year": "2015",
      "energy": 4.6,
      "source": "Nuclear"
    },
    {
      "year": "2016",
      "energy": 4.6,
      "source": "Nuclear"
    },
    {
      "year": "2017",
      "energy": 4.4,
      "source": "Nuclear"
    },
    {
      "year": "2018",
      "energy": 4.3,
      "source": "Nuclear"
    },
    {
      "year": "2019",
      "energy": 4.2,
      "source": "Nuclear"
    },
    {
      "year": "2020",
      "energy": 4.5,
      "source": "Nuclear"
    },
    {
      "year": "2021",
      "energy": 4.2,
      "source": "Nuclear"
    },
    {
      "year": "2022",
      "energy": 4,
      "source": "Nuclear"
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "energy",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of primary energy (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal"
    }
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Fossil fuels still supplied over 80% of primary energy in 2022",
    "subtitle": "Global primary energy mix by source, 2015-2022 (% of total)"
  }
}
```

## Normalized to 100%

`stack: "normalize"` rescales each period to a fraction of its total, so every column of the chart is the same height and the bands show shares. The axis format `".0%"` prints the fractions as percentages. The total is no longer visible, so state it elsewhere if it matters. This demo uses capacity added each year, where the total grows from 168 to 477 gigawatts.

```json
{
  "mark": "area",
  "data": [
    {
      "year": "2019",
      "capacity": 98,
      "type": "Solar"
    },
    {
      "year": "2019",
      "capacity": 58,
      "type": "Wind"
    },
    {
      "year": "2019",
      "capacity": 12,
      "type": "Hydro"
    },
    {
      "year": "2020",
      "capacity": 127,
      "type": "Solar"
    },
    {
      "year": "2020",
      "capacity": 90,
      "type": "Wind"
    },
    {
      "year": "2020",
      "capacity": 20,
      "type": "Hydro"
    },
    {
      "year": "2021",
      "capacity": 167,
      "type": "Solar"
    },
    {
      "year": "2021",
      "capacity": 93,
      "type": "Wind"
    },
    {
      "year": "2021",
      "capacity": 15,
      "type": "Hydro"
    },
    {
      "year": "2022",
      "capacity": 222,
      "type": "Solar"
    },
    {
      "year": "2022",
      "capacity": 75,
      "type": "Wind"
    },
    {
      "year": "2022",
      "capacity": 22,
      "type": "Hydro"
    },
    {
      "year": "2023",
      "capacity": 346,
      "type": "Solar"
    },
    {
      "year": "2023",
      "capacity": 107,
      "type": "Wind"
    },
    {
      "year": "2023",
      "capacity": 24,
      "type": "Hydro"
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of capacity added",
        "format": ".0%",
        "grid": true
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Normalizing turns growing totals into shares",
    "subtitle": "Global renewable capacity additions by source, 2019-2023 (share of gigawatts)"
  }
}
```

## Streamgraph

`stack: "center"` centers the stack around zero instead of resting it on a baseline. Each band keeps its thickness, so the chart still reads as composition. Here the total grows each year, so the outline widens from left to right around the middle.

```json
{
  "mark": "area",
  "data": [
    {
      "year": "2019",
      "capacity": 98,
      "type": "Solar"
    },
    {
      "year": "2019",
      "capacity": 58,
      "type": "Wind"
    },
    {
      "year": "2019",
      "capacity": 12,
      "type": "Hydro"
    },
    {
      "year": "2020",
      "capacity": 127,
      "type": "Solar"
    },
    {
      "year": "2020",
      "capacity": 90,
      "type": "Wind"
    },
    {
      "year": "2020",
      "capacity": 20,
      "type": "Hydro"
    },
    {
      "year": "2021",
      "capacity": 167,
      "type": "Solar"
    },
    {
      "year": "2021",
      "capacity": 93,
      "type": "Wind"
    },
    {
      "year": "2021",
      "capacity": 15,
      "type": "Hydro"
    },
    {
      "year": "2022",
      "capacity": 222,
      "type": "Solar"
    },
    {
      "year": "2022",
      "capacity": 75,
      "type": "Wind"
    },
    {
      "year": "2022",
      "capacity": 22,
      "type": "Hydro"
    },
    {
      "year": "2023",
      "capacity": 346,
      "type": "Solar"
    },
    {
      "year": "2023",
      "capacity": 107,
      "type": "Wind"
    },
    {
      "year": "2023",
      "capacity": 24,
      "type": "Hydro"
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "center",
      "axis": {
        "title": "Capacity added (GW)",
        "format": ".0f"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "A streamgraph centers a growing total around zero",
    "subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `mark` | `"area"`. Several series on one area mark stack by default. |
| `encoding.y.stack` | `"zero"` stacks from zero, `"normalize"` scales each period to a fraction of its total, `"center"` centers the stack, and `null` overlaps the series. |
| `encoding.color` | One band per value of this field. |
| `encoding.x` | The period. `"ordinal"` for named years, `"temporal"` for dates. |
| `legend.position` | Where the legend sits. `"top"` puts it above the plot. |

## Related

- [Area](/docs/change-over-time/area/) for a single series or overlapping series.
- [Stacked 100%](/docs/part-to-whole/stacked-100/) for normalized bars and columns.
- [Grouped and stacked bars](/docs/comparison/grouped-stacked/) for the same stacking on bars.
- [Line](/docs/change-over-time/line/) for comparing individual series.
- [Marks reference](/docs/spec/marks/) lists the channels and options each mark type takes.
