Faceting a Plot into a Trellis Plot
Edit this pageA Trellis plot (or small multiple) is a series of similar plots that displays different subsets of the same data, facilitating comparison across subsets.
There are two ways to facet views in Vega-Lite:
First, the facet
operator is one of Vega-Lite’s view composition operators. This is the most flexible way to create faceted plots and allows composition with other operators.
Second, as a shortcut you can use the column
or row
encoding channels.
Documentation Overview
Facet Operator
To create a faceted view, define how the data should be faceted in facet
and how each facet should be displayed in the spec
.
{
"facet": {
... // Facet definition
},
"spec": ... // Specification
}
In addition to common properties of a view specification, a facet specification has the following properties:
Property | Type | Description |
---|---|---|
facet | FacetMapping |
Required. An object that describes mappings between |
spec | LayerSpec | SingleViewSpec |
Required. A specification of the view that gets faceted. |
align | VgLayoutAlign | RowCol |
The alignment to apply to grid rows and columns.
The supported string values are
Alternatively, an object value of the form Default value: |
bounds | String |
The bounds calculation method to use for determining the extent of a sub-plot. One of
Default value: |
center | Boolean | RowCol |
Boolean flag indicating if subviews should be centered relative to their respective rows or columns. An object value of the form Default value: |
spacing | Number | RowCol |
The spacing in pixels between sub-views of the composition operator.
An object of the form Default value: |
resolve | Resolve |
Scale, axis, and legend resolutions for facets. |
Facet Mapping
The facet
property of a faceted view specification describes mappings between row
and column
and their field definitions:
Property | Type | Description |
---|---|---|
column | FacetFieldDef |
Horizontal facets for trellis plots. |
row | FacetFieldDef |
Vertical facets for trellis plots. |
Facet Field Definition
A FacetFieldDef
is a field definition that has header
(instead of scale
and axis
).
Property | Type | Description |
---|---|---|
aggregate | Aggregate |
Aggregation function for the field
(e.g., Default value: |
bin | Boolean | BinParams |
A flag for binning a Default value: |
field | String | RepeatRef |
Required. A string defining the name of the field from which to pull a data value
or an object defining iterated values from the Note: Dots ( Note: |
timeUnit | TimeUnit |
Time unit (e.g., Default value: |
type | Type |
Required. The encoded field’s type of measurement ( |
header | Header |
An object defining properties of a facet’s header. |
Note Since row
and column
represent actual data fields that are used to partition the data, they cannot encode a constant value
. In addition, you should not facet by quantitative fields unless they are binned, or temporal fields unless you use timeUnit
.
Example
Below are three histograms for the horsepower of cars. Each chart shows the histogram for one origin (Europe, Japan, and USA).
This is the same example as below but the facet operator is more flexible as it allows composition and more customization such as overriding scale, axis, and legend resolution.
You can find more examples in the example gallery.
Facet Headers
Similar to axes of position channels, a header of a facet channel provides guides to convey the data value that each row and column represent.
You can find more about facet headers in the header documentation.
Row & Column Encoding Channels
The row
and column
encoding channels.
The facet channels are encoding channels, which produce a trellis plot that facets a plot into columns or rows respectively. Vega-Lite automatically translates this shortcut to use the facet operator.
Property | Type | Description |
---|---|---|
row | FacetFieldDef |
Vertical facets for trellis plots. |
column | FacetFieldDef |
Horizontal facets for trellis plots. |
Example
Vega-Lite translates this spec to the more flexible spec with the facet operator above.
You can find more examples in the example gallery.
Resolve
The default resolutions for facet are shared scales, axes, and legends.