To stack fields in Vega-Lite, users can either use thestack
property of anencoding field definition or astack
transform inside thetransform
array.
// A Single View or a Layer Specification{ ..., "mark/layer": ..., "encoding": { // Encoding "x" or "y": { "field": ..., "type": "quantitative", "stack": ..., // Stack ... }, ... }, ...}
Thestack
property of aposition field definition determines type of stacking offset if the field should be stacked.
Property | Type | Description |
---|---|---|
stack | String | Null | Boolean | Type of stacking offset if the field should be stacked.
Default value: See also: |
Adding a color field to a bar chart also creates stacked bar chart by default.
Similarly, adding a color field to area chart also creates stacked area chart by default.
You can setstack
to"normalize"
to create normalized (or percentage) stacked bar and area charts.
Settingstack
to"center"
for a stacked area chart creates a streamgraph:
Ifstack
isnull
, the marks will be layered on top of each other. In this example, setting the mark’sopacity
to be semi-transparent (0.6
) creates a layered bar chart.
The stack transform can also handle negative values by creating a diverging stacked bar chart.
Note: that the stack transform cannot handle if there should be items stacked in the middle like in the“Diverging Stacked Bar Chart with Neutral Parts” example.
You can use the order channel to sort the order of stacked marks.
For example, given a stacked bar chart for the barley dataset:
By default, the stacked bar are sorted by the stack grouping fields (color
in this example).
Mapping the sum of yield toorder
channel will sort the layer of stacked bar by the sum of yield instead.
Here we can see that site with higher yields for each type of barley are put on the top of the stack (rightmost).
If you want to define custom sort order, you can derive a new field using thecalculate
transform and sort by that field instead. For example, the following plot makes “University Farm” and “Grand Rapids” be the first (0
) and second values in the stack order:
Note: we plan to havea better syntax for customized sort order in the future.
Sinceline
marks are not stacked by default, to layer lines on top of stacked area charts, you have to manually set thestack
offset for the lines.
// Any View Specification{ ... "transform": [ // Stack Transform { "stack": ..., "groupby": ..., "offset": ..., "sort": ..., "as": ... } ... ], ...}
For example, here is the samenormalized stacked bar chart of the"population"
, grouped by"age"
and colored by"gender"
, but this time using thestack
property oftransform
.
Thestack
transform in thetransform
array has the following properties:
Property | Type | Description |
---|---|---|
stack | String | Required. The field which is stacked. |
groupby | String[] | Required. The data fields to group by. |
offset | String | Mode for stacking marks. One of Default value: |
sort | SortField[] | Field that determines the order of leaves in the stacked charts. |
as | String | String[] | Required. Output field names. This can be either a string or an array of strings with two elements denoting the name for the fields for stack start and stack end respectively. If a single string(e.g., |
We can usestack
transform in conjunction with other transforms to create more complicated charts.
Here we initiallystack
by"question"
and then usewindow
transform to offset each stack.
To create a mosaic chart westack
twice, once in each direction along withwindow
transform.
To add labels to this chart, consultthis example.