# Grouped and stacked bars

> Split each bar or column into series, placed side by side to compare series or stacked end to end to read totals and parts.

Grouped and stacked bars answer different questions about the same data. Grouping places the series side by side, so the reader compares one series against another. Stacking puts the series end to end, so the reader sees each total and how it divides.

Both add a `color` channel to a [bar](/docs/comparison/bar/) or [column](/docs/comparison/column/) chart. When only the split matters and not the totals, a [100% stacked chart](/docs/part-to-whole/stacked-100/) is the better form.

## Grouped columns

Bars stack by default. `stack: null` on the value channel places the series side by side instead. Solar's lead over wind and hydro is easy to read here because every series starts at the same baseline.

```json
{
  "mark": "bar",
  "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": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Solar additions more than tripled from 2019 to 2023",
    "subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
  }
}
```

## Stacked columns

`stack: "zero"` stacks the series from zero, so each column's height is the total for that year. The cost is that only the bottom series shares a baseline, so the upper series are harder to compare across years.

```json
{
  "mark": "bar",
  "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": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Total additions more than doubled in four years",
    "subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
  }
}
```

## Stacked bars with labels

On a horizontal stack, `labels.density: "all"` prints a value on every segment. Here the five parts of each income bracket sum to 100, so the housing segment is comparable across brackets.

```json
{
  "mark": "bar",
  "data": [
    {
      "bracket": "Under $30K",
      "pct": 40,
      "category": "Housing"
    },
    {
      "bracket": "Under $30K",
      "pct": 18,
      "category": "Food"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Transport"
    },
    {
      "bracket": "Under $30K",
      "pct": 12,
      "category": "Healthcare"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Other"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 35,
      "category": "Housing"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 16,
      "category": "Food"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 18,
      "category": "Transport"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 10,
      "category": "Healthcare"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 21,
      "category": "Other"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 32,
      "category": "Housing"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 14,
      "category": "Food"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 19,
      "category": "Transport"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 9,
      "category": "Healthcare"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 26,
      "category": "Other"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 30,
      "category": "Housing"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 12,
      "category": "Food"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 17,
      "category": "Transport"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 8,
      "category": "Healthcare"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 33,
      "category": "Other"
    },
    {
      "bracket": "Over $120K",
      "pct": 27,
      "category": "Housing"
    },
    {
      "bracket": "Over $120K",
      "pct": 10,
      "category": "Food"
    },
    {
      "bracket": "Over $120K",
      "pct": 14,
      "category": "Transport"
    },
    {
      "bracket": "Over $120K",
      "pct": 6,
      "category": "Healthcare"
    },
    {
      "bracket": "Over $120K",
      "pct": 43,
      "category": "Other"
    }
  ],
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of spending (%)"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
}
```

## Normalized stacks

`stack: "normalize"` scales every bar to the same full length, so the chart shows each part's share instead of its size. Set the axis `format` to `".0%"` to print the ticks as percentages. Use this when the totals differ or do not matter.

```json
{
  "mark": "bar",
  "data": [
    {
      "bracket": "Under $30K",
      "pct": 40,
      "category": "Housing"
    },
    {
      "bracket": "Under $30K",
      "pct": 18,
      "category": "Food"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Transport"
    },
    {
      "bracket": "Under $30K",
      "pct": 12,
      "category": "Healthcare"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Other"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 35,
      "category": "Housing"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 16,
      "category": "Food"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 18,
      "category": "Transport"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 10,
      "category": "Healthcare"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 21,
      "category": "Other"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 32,
      "category": "Housing"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 14,
      "category": "Food"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 19,
      "category": "Transport"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 9,
      "category": "Healthcare"
    },
    {
      "bracket": "$50K - $80K",
      "pct": 26,
      "category": "Other"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 30,
      "category": "Housing"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 12,
      "category": "Food"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 17,
      "category": "Transport"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 8,
      "category": "Healthcare"
    },
    {
      "bracket": "$80K - $120K",
      "pct": 33,
      "category": "Other"
    },
    {
      "bracket": "Over $120K",
      "pct": 27,
      "category": "Housing"
    },
    {
      "bracket": "Over $120K",
      "pct": 10,
      "category": "Food"
    },
    {
      "bracket": "Over $120K",
      "pct": 14,
      "category": "Transport"
    },
    {
      "bracket": "Over $120K",
      "pct": 6,
      "category": "Healthcare"
    },
    {
      "bracket": "Over $120K",
      "pct": 43,
      "category": "Other"
    }
  ],
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of spending",
        "format": ".0%"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Housing and food shrink as a share of spending as income rises",
    "subtitle": "Household expenditure normalized to 100% per income bracket, 2022"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `encoding.color` | Splits each category into series by the field's values. |
| `encoding.y.stack` | On a column chart, `null` groups the series side by side, `"zero"` stacks them from zero and `"normalize"` stacks them to full length. |
| `encoding.x.stack` | The same setting on a horizontal bar chart, where the value is on `x`. |
| `encoding.*.axis` | `title` names the axis and `format` sets how its ticks print. |
| `labels.density` | `"all"` labels every segment and `"none"` turns the labels off. |

## Related

- [Bar](/docs/comparison/bar/) and [Column](/docs/comparison/column/) for a single series.
- [Stacked to 100%](/docs/part-to-whole/stacked-100/) for part-to-whole comparisons.
- [Stacked area](/docs/change-over-time/stacked-area/) for stacked series over continuous time.
- [Marks reference](/docs/spec/marks/) lists the channels and options each mark type takes.
