Stack
Edit this pageThe stack
property of a position field definition
determines type of stacking offset if the field should be stacked.
Property | Type | Description |
---|---|---|
stack | String | Null |
Type of stacking offset if the field should be stacked.
Default value: |
// Specification of a Single View
{
...,
"encoding": { // Encoding
"x" or "y": {
"field": ...,
"type": "quantitative",
"stack": ..., // Stack
...
},
...
},
...
}
Documentation Overview
- Stack Bar Chart
- Stack Area Chart
- Normalized Stacked Bar and Area Charts
- Streamgraph
- Layered Bar Chart
- Sorting Stack Order
- Layering Lines on top of Stacked Area Chart
Stack Bar Chart
Adding a color field to a bar chart also creates stacked bar chart by default.
Stack Area Chart
Similarly, adding a color field to area chart also creates stacked area chart by default.
Normalized Stacked Bar and Area Charts
You can set stack
to "normalize"
to create normalized (or percentage) stacked bar and area charts.
Streamgraph
Setting stack
to "center"
for a stacked area chart creates a streamgraph:
Layered Bar Chart
If stack
is null
, the marks will be layered on top of each other.
In this example, setting the mark’s opacity
to be semi-transparent (0.6
) creates a layered bar chart.
Sorting Stack Order
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 to order
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 the calculate
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 have a better syntax for customized sort order in the future.
Layering Lines on top of Stacked Area Chart
Since line
marks are not stacked by default, to layer lines on top of stacked area charts, you have to manually set the stack
offset for the lines.