Vega-Lite order Channel
Use the order encoding channel to control how Vega-Lite draws marks. It is most useful for:
- stacked marks (
bar,area,arc) - path marks (
line,trail,area)
{
"encoding": {
"order": {"field": "sort_key", "type": "quantitative"}
}
}order vs sort
These two are related but solve different problems.
| Property | What it controls | Typical place |
|---|---|---|
sort | axis/legend/domain ordering | inside a channel, e.g. x.sort, color.sort |
order | mark draw sequence / stack sequence | encoding.order |
If your axis looks right but stacks are still in the wrong sequence, you usually need order, not sort.
Example 1: Control Stacking Sequence
The left chart uses default stack order. The right chart uses encoding.order with tier_order.
Vega-Lite spec used in the right chart:
{
"mark": "bar",
"encoding": {
"x": {"field": "quarter", "type": "nominal"},
"y": {"field": "amount", "type": "quantitative"},
"color": {"field": "tier", "type": "nominal"},
"order": {"field": "tier_order", "type": "quantitative"}
}
}Example 2: Order Points in a Line Path
For path marks, order controls the connection sequence of points.
{
"mark": "line",
"encoding": {
"x": {"field": "month", "type": "ordinal", "sort": ["Jan", "Feb", "Mar", "Apr"]},
"y": {"field": "value", "type": "quantitative"},
"detail": {"field": "city", "type": "nominal"},
"order": {"field": "month_index", "type": "quantitative"}
}
}In this example:
x.sortcontrols axis label order.ordercontrols the point connection sequence for each line.
FAQ
When should I use order?
Use it when the sequence of drawing matters, especially for stacked marks and line/trail paths.
Why doesn't sort fix my stack sequence?
sort affects domain ordering (axis/legend). Stack drawing sequence is controlled by encoding.order.
Can I use a calculated field for order?
Yes. A common pattern is to create a numeric rank with calculate, then map that field to encoding.order.