Data 304
To define a layered chart, put multiple specifications into an array under the layer property.
{ ...,
"data": ... // shared data (if any)
..., // other shared properties
"layer": [ // array of layer specs
{ ... }, // first layer spec
{ ... }, // second layer spec
... // etc
]
}
Vega-Lite will (by default) expand the domains of the scales to include the data from all the layers.
Each subsequent layer sits “on top of” the previous layers
"data": {"url": "data/cars.json"},
"layer": [
{"mark": {"type": "line", "opacity": 0.8},
"encoding": {
"y": {"field": "Miles_per_Gallon", "type": "quantitative", "aggregate": "mean"},
"color": {"field": "Origin"},
"detail": {"field": "Cylinders"},
"x": {"field": "Year", "type": "temporal"}
}
},
{"mark": "point",
"encoding": {
"y": {"field": "Miles_per_Gallon", "type": "quantitative", "aggregate": "mean"},
"color": {"field": "Origin"},
"detail": {"field": "Cylinders"},
"x": {"field": "Year", "type": "temporal"}
}
}]
Elements of the specification that are shared across all layers can be placed outside the layers property.
Here we can reduce the layer part to just specifying the mark.
"encoding": {
"y": {"field": "Miles_per_Gallon", "type": "quantitative", "aggregate": "mean"},
"color": {"field": "Origin"},
"detail": {"field": "Cylinders"},
"x": {"field": "Year", "type": "temporal"}
},
"layer": [
{"mark": {"type": "line", "opacity": 0.7}},
{"mark": "point"}
]
To combine layers and facets, we must use the facet operator.