Vega visualization grammar: design from data to marks, channels, and interaction
Vega is a visualization grammar that specifies data, transforms, marks, scales, and interaction in JSON. Vega-Lite expresses common charts with a shorter declarative specification and compiles it into a more detailed Vega specification. Start with Vega-Lite, then consider Vega when you need finer control at a lower level.
How do Vega and Vega-Lite differ?
A grammar of graphics assembles a chart from decisions about turning data into visual elements, rather than treating a chart type as a finished black box. A Vega specification describes data and transforms as well as marks, scales, axes, legends, signals, and details such as view size in JSON. Vega-Lite states analytical intent declaratively through data, marks, and encodings; its compiler supplies details such as scales and axes to produce Vega. For common charts, a shorter Vega-Lite specification is easier to read and maintain. Vega gives more direct control when a layout, event, or display behavior is hard to express in the higher-level grammar. Shorter is not always more suitable: check whether Vega-Lite can express the required behavior.
A specification turns data into an interpretable view
| Stage | Decision | Check |
|---|---|---|
| Data | What does one row represent? | Grain and field types |
| Transform | How should filtering, calculation, aggregation, or sorting work? | Did row count or grain change? |
| Mark | Should the chart draw bars, points, lines, or rules? | Does the geometry fit the question? |
| Encoding channel | Which fields map to position, color, size, or other properties? | Does each channel carry its intended meaning? |
| Scale and guide | How do values map to position or color, and how do axes or legends explain them? | Are units, range, order, and labels readable? |
| Interaction | Does a click selection or drag range help? | Are the action and result clear? |
A scale converts a data domain into visual values such as pixel positions, colors, or sizes. Axes help interpret positional scales; legends explain color, shape, or size scales. A visible mark does not guarantee that its analytical meaning is correct. The same field can support different comparisons in different channels, so first define the reader’s question and what a row means.
Marks and visual variables play different roles
A mark is the geometric shape representing a data item. A visual variable is a visible property of that shape, such as position, length, color, size, or shape. An encoding channel connects a data field to one of these properties. For example, x and y encode position; color encodes color; size and shape encode size and shape; text adds words; detail distinguishes groups.
| Mark | Good fit | Watch for |
|---|---|---|
| point | How are individual observations distributed, or how are two quantities related? | Overlapping points can hide density. |
| bar | How large are values or differences across categories? | Check baseline and units when length represents magnitude. |
| line | How does an ordered sequence of observations change? | Check connection order and gaps from missing values. |
| rule | Where is a reference value, threshold, or interval boundary? | Only y can draw a horizontal rule across the view; only x can draw a vertical one. |
The Vega-Lite point mark draws one symbol per data item and is common in scatterplots. A rule mark draws a line segment: with only y it can span the view’s width, and with only x it can span the height. Pair x with x2 or y with y2 to show a range. A point mark is a shape; a point selection below is an interaction type that selects data by clicking. They share a name but have different roles.
Color helps distinguish categories, but should not be the only way to communicate information; add a cue such as text, shape, or line style. Size needs particular care. In Vega-Lite, size for point, circle, and square marks represents symbol area in pixels, not its diameter. Check that the legend and explanation do not imply a different relationship.
A field’s semantic type is not its storage format
Vega-Lite type describes measurement meaning, not only a programming format such as number or string. Quantitative means an amount, temporal means time, ordinal means ordered categories, and nominal means unordered categories. A postal code stored as a number may be a category rather than a quantity; ratings 1, 2, and 3 have an order but not necessarily equal intervals, so they are closer to ordinal. If a date is stored as a string, check its format and type so it is interpreted as time.
The combination of type and channel affects scales and axes. Month names on x may need an explicit calendar sort because alphabetical order differs. Encoding categories with a quantitative scale or treating dates as nominal may still draw a chart while changing what readers infer. State important field types explicitly instead of relying only on automatic inference.
Transform order changes the unit of analysis
A filter removes rows, a calculation creates a field, and an aggregation combines rows into a smaller set of summaries. If row grain is unclear, a chart may show individual orders when the reader expects monthly totals, or sum values that were already aggregated. In Vega-Lite, a view-level transform array runs in its written order. If field transforms inside encodings are also present, view-level transforms run first, followed by bin, timeUnit, aggregate, sort, and stack. This order can change the meaning of the result.
For example, if monthly order rows repeat across regions, filter to the region of interest before deciding whether to sum or average by month. The type of an aggregated value should reflect its meaning after transformation. Recording the source grain and the grain after transforms in a brief or review helps catch accidental double aggregation.
Example: monthly orders in the North region
Suppose each hypothetical input row contains the order count for one region in one month, and several regions have rows for the same month. The Vega-Lite specification below filters to North, then compares the sum of orders by month with bars. Values are illustrative; the specification was not run or rendered.
{
"data": {"values": [
{"month": "Jan", "region": "North", "orders": 12},
{"month": "Jan", "region": "North", "orders": 8},
{"month": "Feb", "region": "North", "orders": 10},
{"month": "Feb", "region": "South", "orders": 11},
{"month": "Mar", "region": "North", "orders": 15}
]},
"transform": [{"filter": "datum.region === 'North'"}],
"mark": "bar",
"encoding": {
"x": {"field": "month", "type": "ordinal", "sort": ["Jan", "Feb", "Mar"]},
"y": {"aggregate": "sum", "field": "orders", "type": "quantitative"}
}
}Read the specification in order. The transform keeps only North rows. The x encoding defines month categories and calendar order. The y aggregation sums orders by month, giving Jan 20, Feb 10, and Mar 15; South is excluded. If real data has one row per month, this aggregation may be unnecessary; a different question may call for an average or median. The results can be checked by calculating from the example data, but that does not replace validation or rendering in a Vega-Lite runtime.
Selection parameters define how a chart responds
A Vega-Lite selection parameter turns direct manipulation, such as a click or drag, into a data query. A point selection chooses a discrete value by clicking; by default, shift-click can add more values. An interval selection chooses a continuous range by dragging. Connecting a selection to a filter or conditional color or opacity can support actions such as “show only the selected month” or “highlight the selected range.”
Before adding interaction, define which fields or channels are selected, how to clear a selection, and whether selections across multiple views are combined or kept separate. A selection parameter does not guarantee click support in every user environment. Check keyboard and touch input, focus, and a text alternative for selection state in the target application. If an interaction does not help answer the question, it only adds complexity.
Check before sharing
What does one input row represent, and what is the grain after transforms?
Does the mark fit the reader’s question, and does each field’s measurement type match its channel?
Do aggregation, sorting, missing values, scale ranges, and axis or legend labels lead to the expected interpretation?
Is information distinguished by more than color alone, and does a size legend explain area accurately?
What can users do through interaction, and are selection clearing plus keyboard and touch alternatives clear?
Was the specification validated against the schema version of the Vega/Vega-Lite runtime, with errors and actual rendering checked?
A documentation example is not guaranteed to work unchanged in a runtime using another version. Check the target schema, resolve validation errors, and inspect the resulting chart and interaction before implementation. This article’s JSON is illustrative and was not run, validated, or rendered. The comparison table and reproducible specification explain the grammar relationships, so no decorative image is included.
References
[Vega specification](https://vega.github.io/vega/docs/specification/) and [Vega documentation overview](https://vega.github.io/vega/docs/)
[Vega-Lite overview](https://vega.github.io/vega-lite/docs/) and [encoding channels](https://vega.github.io/vega-lite/docs/encoding.html)
[Vega-Lite data types](https://vega.github.io/vega-lite/docs/type.html) and [scales](https://vega.github.io/vega-lite/docs/scale.html)
[Vega-Lite marks](https://vega.github.io/vega-lite/docs/mark.html), [point mark](https://vega.github.io/vega-lite/docs/point.html), and [rule mark](https://vega.github.io/vega-lite/docs/rule.html)
[Vega-Lite transformations](https://vega.github.io/vega-lite/docs/transform.html) and [selection parameters](https://vega.github.io/vega-lite/docs/selection.html)
[W3C WCAG: Use of Color](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)