Vega-Lite: build a bar chart of totals by category with a specification
Vega-Lite is a visualization grammar in which you declare the relationship between data and a chart in JSON. Put rows in `data`, choose a shape with `mark`, and connect fields to visual channels in `encoding`; Vega-Lite compiles the specification and supplies chart components such as axes and scales by default. This guide turns a small illustrative sales table into a bar chart of totals by category and explains how each property affects the result. The official Vega-Lite v6 documentation was checked on 2026-11-18.
Vega-Lite: build a bar chart of totals by category with a specification
Vega-Lite is a visualization grammar in which you declare the relationship between data and a chart in JSON. Put rows in data, choose a shape with mark, and connect fields to visual channels in encoding; Vega-Lite compiles the specification and supplies chart components such as axes and scales by default. This guide turns a small illustrative sales table into a bar chart of totals by category and explains how each property affects the result. The official Vega-Lite v6 documentation was checked on 2026-11-18.
What does a specification declare?
Ordinary chart code often spells out how to read data, calculate totals, position bars, and draw tick marks. In Vega-Lite, you describe which data fields map to which visual roles. A specification is closer to a structured description of what to show than a step-by-step list of drawing commands. The Vega-Lite compiler translates it to a lower-level Vega specification, which the Vega runtime renders.
| Property | Question it answers | This example |
|---|---|---|
data | Which rows should the chart use? | Six category and sales rows |
mark | Which shape should represent the data? | Bars by category |
encoding | Which visual channel represents each field? | Category on x; summed sales on y |
aggregate | How should multiple rows be summarized? | sum by category |
The compiler can add axes and scales because it interprets these declarations and applies defaults. Defaults are convenient, but they do not infer the meaning of your analysis. State what to aggregate and what to group by.
A specification for sales totals by category
The six rows below are invented so you can check the result by hand. The sales unit is arbitrary and illustrative, not a real company result or measurement.
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"data": {
"values": [
{"category": "Alpha", "sales": 18},
{"category": "Alpha", "sales": 12},
{"category": "Beta", "sales": 9},
{"category": "Beta", "sales": 15},
{"category": "Gamma", "sales": 21},
{"category": "Gamma", "sales": 14}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "category", "type": "nominal", "title": "Category"},
"y": {
"aggregate": "sum",
"field": "sales",
"type": "quantitative",
"title": "Total sales (illustrative units)"
}
}
}Paste the JSON into the official Vega-Lite online editor to render it. Expect three bars: Alpha is 18+12=30, Beta is 9+15=24, and Gamma is 21+14=35. Check that their relative heights follow those totals and that the axis titles describe the intended quantities. The editor is linked from the official getting-started tutorial; this article's specification was not rendered in the editor during drafting.
How to read fields, types, and channels
data.values represents each row as one object. This inline-data form is handy for small examples or fixed datasets. For larger or frequently changing data, use a supported external source or supply data through your application.
mark: "bar" chooses bars. Inside encoding, x and y are visual channels: properties on screen, such as position or length, that data can affect. Each channel definition names a data field with field and describes its meaning with type. Here, category is a label, so it is nominal; sales is a measurable number, so it is quantitative. A numeric-looking code or postal code is not necessarily a quantity—choose types according to what the values mean.
With suitable types, Vega-Lite creates default scales and axes: categories on x and numeric totals on y. Swapping x and y can change the chart to horizontal bars. Set the channel's axis property when units, tick spacing, or titles could be misunderstood.
How does sum group the rows?
aggregate: "sum" adds sales. The unaggregated encoded field category becomes the grouping field, so the six input rows become one summed result for each of the three categories. This is the channel-field aggregation pattern described in the official aggregation documentation.
Choose the summary for the question: sum can answer “total by category,” average can answer “mean transaction value,” and count can answer “number of transactions.” A sum can be higher for a category simply because it has more records; do not confuse totals and averages. Adding more unaggregated fields to the encoding can split groups further, so check that the resulting granularity is intended.
Common mistakes and how to check them
**Only one bar appears:** check that a non-aggregated field distinguishes categories. Here, `category` on x is the grouping field.
**Values look wrong when a category has several rows:** confirm whether the intended operation is `sum` or `average`, then compare against a hand calculation. Also distinguish an aggregate chart from one that should show every raw row.
**The quantitative axis is empty or unexpected:** confirm that `sales` is numeric and the spelling matches the objects in the data. If input includes `null` or numeric strings, clean or explicitly type it instead of relying on inference.
**The chart renders but the axis meaning is unclear:** use a title that includes the measure and unit, and map `nominal` categories and `quantitative` measures to appropriate channels.
**A basic bar specification cannot express the desired behavior:** Vega-Lite supports transformations such as aggregation, filtering, and sorting, plus layers, multiple views, and selection-based interaction. For highly custom rendering procedures or interactions that do not fit the declarative model, lower-level Vega or application code may be more suitable.
Vega-Lite's advantage is that it leaves routine scale and axis construction to the compiler while keeping the relationship between data and visual meaning readable. The trade-off is that you must understand the defaults and verify that field types and aggregation answer the question.
Sources
[Vega-Lite Documentation: Overview](https://vega.github.io/vega-lite/docs/)
[Vega-Lite Tutorial: Getting started](https://vega.github.io/vega-lite/tutorials/getting_started.html)
[Vega-Lite Documentation: Aggregation](https://vega.github.io/vega-lite/docs/aggregate.html)
[Vega-Lite Documentation: Transformation](https://vega.github.io/vega-lite/docs/transform.html)
[Vega-Lite Documentation: Axis](https://vega.github.io/vega-lite/docs/axis.html)
[Vega-Lite Online Editor](https://vega.github.io/editor/)