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 or column chart. When only the split matters and not the totals, a 100% stacked chart 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.
{
"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"
},
// … 9 more rows; Copy includes them
],
"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)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
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.
{
"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"
},
// … 9 more rows; Copy includes them
],
"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)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
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.
{
"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"
},
// … 19 more rows; Copy includes them
],
"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 (%)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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 (%)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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 (%)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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 (%)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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 (%)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
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.
{
"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"
},
// … 19 more rows; Copy includes them
],
"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"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 25 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"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"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
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 and Column for a single series.
- Stacked to 100% for part-to-whole comparisons.
- Stacked area for stacked series over continuous time.
- Marks reference lists the channels and options each mark type takes.