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 facet
, 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 | FacetFieldDef | FacetMapping |
Required. Definition for how to facet the data. One of: 1) a field definition for faceting the plot by one field 2) An object that maps |
spec | LayerSpec | UnitSpec |
Required. A specification of the view that gets faceted. |
columns | Number |
The number of columns to include in the view composition layout. Default value: Note: 1) This property is only for: - the general (wrappable) 2) Setting the |
Facet Field Definition
A facet field definition has the following properties:
Property | Type | Description |
---|---|---|
bin | Boolean | BinParams | Null |
A flag for binning a
Default value: See also: |
field | Field |
Required. A string defining the name of the field from which to pull a data value or an object defining iterated values from the See also: Notes: 1) Dots ( |
timeUnit | TimeUnit | TimeUnitParams |
Time unit (e.g., Default value: See also: |
type | String |
The type of measurement ( Vega-Lite automatically infers data types in many cases as discussed below. However, type is required for a field if: (1) the field is not nominal and the field encoding has no specified Default value: 1) For a data 2) For a constant value in data domain ( Note: - Data See also: |
header | Header |
An object defining properties of a facet’s header. |
Note
- Unlike a positional field definition, a facet field definition has the
header
property instead ofscale
andaxis
. - Since
facet
,row
andcolumn
represent actual data fields that are used to partition the data, they cannot encode a constantvalue
. In addition, you should not facet by quantitative fields unless they are binned, or temporal fields unless you usetimeUnit
.
Row/Column 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 |
A field definition for the horizontal facet of trellis plots. |
row | FacetFieldDef |
A field definition for the vertical facet of trellis plots. |
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.
Example
Here are examples of full row-facet and wrapped facet specifications. For more example, see the example gallery.
Row-Facet
The following specification uses the facet
operator to vertically facet the histograms for the “horsepower” of cars by “origin” (using "row"
). 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.
Wrapped Facet
Facet, Row, and Column Encoding Channels
The facet channels (facet
, row
, and column
) are encoding channels that serves as macros for a facet specification. Vega-Lite automatically translates this shortcut to use the facet operator.
Facet Field Definition
In addition to field
, type
, bin
, and timeUnit
, field definitions for row
, column
and facet
channels may also include these properties:
Property | Type | Description |
---|---|---|
align | String |
The alignment to apply to row/column facet’s subplot. The supported string values are
Default value: |
center | Boolean |
Boolean flag indicating if facet’s subviews should be centered relative to their respective rows or columns. Default value: |
spacing | Number |
The spacing in pixels between facet’s sub-views. Default value: Depends on |
In addition, the facet
channel should include the columns
property:
Property | Type | Description |
---|---|---|
columns | Number |
The number of columns to include in the view composition layout. Default value: Note: 1) This property is only for: - the general (wrappable) 2) Setting the |
Meanwhile, the row
and column
channel could include the following properties:
Property | Type | Description |
---|---|---|
align | String |
The alignment to apply to row/column facet’s subplot. The supported string values are
Default value: |
center | Boolean |
Boolean flag indicating if facet’s subviews should be centered relative to their respective rows or columns. Default value: |
spacing | Number |
The spacing in pixels between facet’s sub-views. Default value: Depends on |
Examples
Here are examples of row-facet and wrapped facet plots that use encoding to specify the faceted fields. For more example, see the example gallery.
Row Facet (with Row Encoding)
Under the hood, Vega-Lite translates this spec with "row"
channel to the more flexible specs with the facet operator above.
Grid Facet (with Row and column Encoding)
Using both "row"
and "column"
channels produce a grid of small multiples.
Wrapped Facet (with Facet Encoding)
Under the hood, Vega-Lite translates this spec with "facet"
channel to the more flexible specs with the facet operator above.
Resolve
The default resolutions for row/column facet are shared scales, axes, and legends.
To override this behavior, you can set resolve
to "independent"
:
Facet Configuration
// Top-level View Specification
{
...,
"config": { // Configuration Object
"facet": { // - Facet Configuration
"spacing": ...,
"columns": ...,
},
...
}
}
The facet configuration supports the following properties:
Property | Type | Description |
---|---|---|
columns | Number |
The number of columns to include in the view composition layout. Default value: Note: 1) This property is only for: - the general (wrappable) 2) Setting the |
spacing | Number |
The default spacing in pixels between composed sub-views. Default value: |