# Bar

> Compare one value across named categories with horizontal bars, then sort, group, stack, label and highlight them.

A bar chart compares one number across a set of categories, and the reader judges each value by the length of its bar. Bars run horizontally, so category names sit on the left at full length, however long they are.

Use a column chart instead when the categories are time periods or have a natural left-to-right order, such as months or years. The spec is the same; only the channels swap. OpenChart draws horizontal bars when `y` is the category and `x` is the number, and vertical columns when `x` is the category and `y` is the number.

## A basic bar chart

Set `mark` to `"bar"`, put the number on `x` and the category on `y`. Value labels show by default on the bars where they fit.

```json
{
  "mark": "bar",
  "data": [
    {
      "country": "India",
      "population": 1463000000
    },
    {
      "country": "China",
      "population": 1410000000
    },
    {
      "country": "United States",
      "population": 347000000
    },
    {
      "country": "Indonesia",
      "population": 285000000
    },
    {
      "country": "Pakistan",
      "population": 255000000
    },
    {
      "country": "Nigeria",
      "population": 240000000
    },
    {
      "country": "Brazil",
      "population": 217000000
    },
    {
      "country": "Bangladesh",
      "population": 175000000
    },
    {
      "country": "Russia",
      "population": 144000000
    },
    {
      "country": "Ethiopia",
      "population": 135000000
    }
  ],
  "encoding": {
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2~s"
      }
    },
    "y": {
      "field": "country",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "The ten most populous countries",
    "subtitle": "Population by country, 2025 estimates"
  }
}
```

## Sorted bars

A ranking reads fastest when the longest bar is first. `sort: "-x"` on the category channel orders the categories by their `x` value, largest at the top. `sort: null` keeps the order of the data, and an array of names sets the order by hand.

```json
{
  "mark": "bar",
  "data": [
    {
      "country": "India",
      "population": 1463000000
    },
    {
      "country": "China",
      "population": 1410000000
    },
    {
      "country": "United States",
      "population": 347000000
    },
    {
      "country": "Indonesia",
      "population": 285000000
    },
    {
      "country": "Pakistan",
      "population": 255000000
    },
    {
      "country": "Nigeria",
      "population": 240000000
    },
    {
      "country": "Brazil",
      "population": 217000000
    },
    {
      "country": "Bangladesh",
      "population": 175000000
    },
    {
      "country": "Russia",
      "population": 144000000
    },
    {
      "country": "Ethiopia",
      "population": 135000000
    }
  ],
  "encoding": {
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2~s"
      }
    },
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "chrome": {
    "title": "India and China each have about four times the US population",
    "subtitle": "Population by country, 2025 estimates"
  }
}
```

## Grouped bars

Add a `color` channel to split each category into series. Bars stack by default; `stack: null` on the value channel places the series side by side instead, which suits comparing series against each other rather than reading totals. This example uses columns because the categories are 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": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Solar added more capacity than wind and hydro combined in every year",
    "subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
  }
}
```

## Stacked bars

With `stack: "zero"` the segments stack end to end, so each bar shows its total and the share of each part. Here every bracket sums to 100%, which makes the housing segment comparable across incomes.

```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"
    }
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
}
```

## Value labels

`labels` controls the numbers printed on the bars. They show by default wherever they fit, and a label that would collide with another is left out. `format` takes a d3-format string: `"+.1f"` prints a sign and one decimal. `density: "none"` turns the labels off. A negative value grows left from zero, and a `refline` at `x: 0` marks the baseline. The color channel maps a derived `direction` field to two colors so the loss stands out.

```json
{
  "mark": "bar",
  "data": [
    {
      "sector": "Communication Services",
      "return": 39.7,
      "direction": "Gain"
    },
    {
      "sector": "Information Technology",
      "return": 37.6,
      "direction": "Gain"
    },
    {
      "sector": "Consumer Discretionary",
      "return": 29.5,
      "direction": "Gain"
    },
    {
      "sector": "Financials",
      "return": 28.9,
      "direction": "Gain"
    },
    {
      "sector": "Utilities",
      "return": 20.1,
      "direction": "Gain"
    },
    {
      "sector": "Industrials",
      "return": 16.2,
      "direction": "Gain"
    },
    {
      "sector": "Consumer Staples",
      "return": 12.2,
      "direction": "Gain"
    },
    {
      "sector": "Real Estate",
      "return": 2,
      "direction": "Gain"
    },
    {
      "sector": "Energy",
      "return": 1.9,
      "direction": "Gain"
    },
    {
      "sector": "Health Care",
      "return": 1.1,
      "direction": "Gain"
    },
    {
      "sector": "Materials",
      "return": -1.2,
      "direction": "Loss"
    }
  ],
  "encoding": {
    "x": {
      "field": "return",
      "type": "quantitative",
      "axis": {
        "title": "Total return (%)",
        "format": "+.0f"
      }
    },
    "y": {
      "field": "sector",
      "type": "nominal",
      "sort": "-x"
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Gain",
          "Loss"
        ],
        "range": [
          "#0e7490",
          "#d1495b"
        ]
      }
    }
  },
  "legend": {
    "show": false
  },
  "labels": {
    "format": "+.1f"
  },
  "annotations": [
    {
      "type": "refline",
      "x": 0,
      "style": "solid",
      "strokeWidth": 1.5
    }
  ],
  "chrome": {
    "title": "Only materials ended 2024 in the red",
    "subtitle": "S&P 500 total return by sector, full year 2024"
  }
}
```

## Highlighting a bar

`highlight` on the `color` channel keeps the named categories in color and greys out the rest. A `refline` annotation draws a reference value in data units, here 1 billion people, and its label sits outside the plot.

```json
{
  "mark": "bar",
  "data": [
    {
      "country": "India",
      "population": 1463000000
    },
    {
      "country": "China",
      "population": 1410000000
    },
    {
      "country": "United States",
      "population": 347000000
    },
    {
      "country": "Indonesia",
      "population": 285000000
    },
    {
      "country": "Pakistan",
      "population": 255000000
    },
    {
      "country": "Nigeria",
      "population": 240000000
    },
    {
      "country": "Brazil",
      "population": 217000000
    },
    {
      "country": "Bangladesh",
      "population": 175000000
    },
    {
      "country": "Russia",
      "population": 144000000
    },
    {
      "country": "Ethiopia",
      "population": 135000000
    }
  ],
  "encoding": {
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population",
        "format": ".2~s"
      }
    },
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "color": {
      "field": "country",
      "type": "nominal",
      "highlight": [
        "India",
        "China"
      ]
    }
  },
  "legend": {
    "show": false
  },
  "annotations": [
    {
      "type": "refline",
      "x": 1000000000,
      "label": "1 billion",
      "labelPlacement": "outside",
      "style": "dashed"
    }
  ],
  "chrome": {
    "title": "India and China are the only countries above 1 billion people",
    "subtitle": "Population by country, 2025 estimates"
  }
}
```

## Mirrored bars

`mark: { type: "bar", mirror: true }` draws two groups back to back from a center column of category labels, the layout of a population pyramid. The `color` field must have exactly two values, and the first goes on the left. The [population pyramid](/docs/comparison/pyramid/) page covers the layout and how to play it through the years.

## Related

- [Column](/docs/comparison/column/) for categories that run along time.
- [Grouped and stacked bars](/docs/comparison/grouped-stacked/) for splitting each bar into series.
- [Population pyramid](/docs/comparison/pyramid/) for two groups back to back.
- [Stacked to 100%](/docs/part-to-whole/stacked-100/) when the share of each part matters more than the totals.
- [Marks reference](/docs/spec/marks/) lists the channels and options each mark type takes.
