Scale
Scales are functions that transform a domain of data values (numbers, dates, strings, etc) to a range of visual values (pixels, colors, sizes).
Vega-Lite automatically creates scales for fields that are mapped to mark properties. Supported scale types are quantitative, time, and ordinal. Default scale properties are determined based on a set of rules for each scale type, but scale property of the channel definition can be provided to customize the scale’s properties.
{
"data": ... ,
"mark": ... ,
"encoding": {
"x": {
"field": ...,
"type": ...,
"scale": { // scale
"type": ...
},
...
},
"y": ...,
...
},
...
}
The rest of this page describes properties of a scale and their default behavior.
Scale Type
Vega-Lite supports the following scale types:
- Quantitative Scale
- A quantitative scales takes continuous, quantitative data as its input domain. There are multiple types of quantitative scales.
linear,power, andlogscales output continuous ranges. Meanwhilequantizeandquantilescales output discrete ranges.
linearscale expresses each range value y as a linear function of the domain value x: y = mx + b. This is the default scale for a quantitative field (field withtype="quantitative").powscale expresses each range value y as a power (exponential) function of the domain value x: y = mx^k + b, where k is the exponent value. (k can be customized usingexponentproperty.)logscale expresses each range value y as a logarithmic function of the domain value x: y = mlog(x) + b. As log(0) = -∞, a log scale domain must be strictly-positive or strictly-negative; the domain must not include or cross zero. Vega-Lite automatically filters zero values from the field mapped to a log scale.quantizescale maps continuous value to a discrete range by dividing the domain into uniform segments based on the number of values in (i.e., the cardinality of) the output range. Each range value y can be expressed as a quantized linear function of the domain value x: y = m round(x) + b.quantilescale maps a sampled input domain to a discrete range by sorting the domain and compute the quantiles. The cardinality of the output range determines the number of quantiles that will be computed.
- Time Scale
- A
timescale is similar to a linear quantitative scale but takes date as input. In general, a temporal field hastimescale by default. The exceptions are temporal fields withhours,day,date,monthas time unit; they haveordinalscales by default. - Ordinal Scale
- An ordinal scale (
ordinal) takes discrete domain as their input domain. Ordinal (ordered) and nominal (unordered/categorical) data always useordinalscale.
- An ordinal
colorscale withnominaldata outputs categorical color palette while an ordinalcolorscale withordinaldata outputs sequential color ramp. (See example.) - An ordinal
shapescale always produces a categorical range since shape cannot convey order. - Ordinal scales for other channels (
x,y,size) always output sequential range. The default order for nominal data is determined by Javascript’s natural order. For example,"a"<"b".
| Property | Type | Description |
|---|---|---|
| type | String | The type of scale. • For a quantitative field, supported quantitative scale types are "linear" (default), "log", "pow", "sqrt", "quantile", "quantize", and "threshold". • For a temporal field without timeUnit, the scale type should be time (default) or ordinal. • For ordinal and nominal fields, the type is always ordinal. Unsupported values will be ignored. |
Note: For more information about scale types, please see d3 scale documentation.
Example: Log Scale
The following example has a logarithmic y-scale.
Scale Domain
By default, a scale draws domain values directly from the channel field.
Custom domain values can be specified via the scale’s domain property.
| Property | Type | Description |
|---|---|---|
| domain | Array | Custom domain values. • For quantitative data, this can take the form of a two-element array with minimum and maximum values. • For temporal data, this can, this can be a two-element array with minimum and maximum values in the form of either timestamp numbers or the DateTime definition object. • For ordinal or nominal data, this is an array representing all values and their orders. |
Note: To sort the order mapping between the domain values and range, please use the channel definition’s sort property.
Scale Range
The range of the scale represents the set of output visual values. Vega-Lite automatically determines appropriate range based on the scale’s channel and type, but range property can be provided to customize range values.
xandyScales- For continuous
xandyscales (quantitative and time), the range are always[0, cellWidth]and[0, cellHeight](See config.cell for customizing cell width and height). For ordinalxandyscales, the maximum range is a product of the field’s cardinality andbandSize. Not Customizable: specifiedrangewill be ignored. - Nominal
colorScales - A
colorscale of a nominal field has a categorical color palette as its range. Customized categorical colorrangecan be either a string literal for a palette name or an array of desired output values. Default value: derived from scale config’snominalColorRange("category10"by default). - Sequential
colorScales - A
colorscale for ordinal, temporal, and quantitative fields have a sequential color ramp as its range. Currently, Vega-Lite only supports color ramp that interpolate between two color values. Customized sequential colorrangetakes a two-element array of color values for interpolation. Default value: derived from scale config’ssequentialColorRange(green ramp between["#AFC6A3", "#09622A"]by default). shapeScales- A
shapescale has a list of shape type names as its range. A customized shaperangeis an array of supported shapes. Default value: derived from scale config’sshapeRangeThis is, by default, the"shape"palette, which is equivalent to["circle", "cross", "diamond", "square", "triangle-down", "triangle-up"]. sizeScales- A
sizescale has a sequential range. Customized sizerangecan be either a two-element array of size values for the interpolation or (for ordinal size scale only) an array of desired output size for each domain value. Default value:
• forbar: derived from scale config’sbarSizeRange. If both scale’srangeand the scale config’sbarSizeRangeare unspecified (default), the default size range is a range from mark config’sthinBarWidthto the scale’sbandSize.
• forpoint,square, andcircle: derived from scale config’spointSizeRange. If both scale’srangeand the scale config’spointSizeRangeare unspecified (default), the default size range is a range from 9 to the square of the scale’sbandSize(bandSize^2).
• fortext: derived from scale config’sfontSizeRange([8, 40]by default).
• fortick: derived from scale config’stickSizeRange([1, 20]by default).
| Property | Type | Description |
|---|---|---|
| range | Array | String | Customized scale range. |
Built-in Color Palettes
The following built-in palettes can be used as a customized categorical color range value.
| Name | Description |
|---|---|
"category10" |
Set the scale range to a 10-color categorical palette: #1f77b4 #ff7f0e #2ca02c #d62728 #9467bd #8c564b #e377c2 #7f7f7f #bcbd22 #17becf |
"category20" |
Set the scale range to a 20-color categorical palette: #1f77b4 #aec7e8 #ff7f0e #ffbb78 #2ca02c #98df8a #d62728 #ff9896 #9467bd #c5b0d5 #8c564b #c49c94 #e377c2 #f7b6d2 #7f7f7f #c7c7c7 #bcbd22 #dbdb8d #17becf #9edae5 |
"category20b" |
Set the scale range to a 20-color categorical palette: #393b79 #5254a3 #6b6ecf #9c9ede #637939 #8ca252 #b5cf6b #cedb9c #8c6d31 #bd9e39 #e7ba52 #e7cb94 #843c39 #ad494a #d6616b #e7969c #7b4173 #a55194 #ce6dbd #de9ed6 |
"category20c" |
Set the scale range to a 20-color categorical palette: #3182bd #6baed6 #9ecae1 #c6dbef #e6550d #fd8d3c #fdae6b #fdd0a2 #31a354 #74c476 #a1d99b #c7e9c0 #756bb1 #9e9ac8 #bcbddc #dadaeb #636363 #969696 #bdbdbd #d9d9d9 |
Example: Default Color Ranges based on Data Types
A color scale of a nominal field outputs a categorical color palette.
Meanwhile, a color scale an ordinal field and a quantitative field outputs a sequential color ramp.
Example: Custom Color Range
We can customize the color range of the scatterplot above by providing scale’s range property. For a nominal color field, range can be an array describing the desired palette.
For ordinal, quantitative, and time fields, range can be a two-element array describing the two colors for interpolation.
Piecewise Scale Example: Diverging Color Scale
We can also create diverging color graph by specify range with multiple elements. Number of elements of range should match with the number of elements in domain. Though continuous scale normally contains 2 elements in both domain and range, having 3 or more elements will create a piecewise scale. For more detail about a piecewise scale, please refer to D3 documentation.
Other Scale Properties
General Scale Properties
| Property | Type | Description |
|---|---|---|
| round | Boolean | If true, rounds numeric output values to integers. This can be helpful for snapping to the pixel grid (only available for x, y, size, row, and column scales). Default value: derived from scale config (true by default). |
Quantitative Scale Properties
| Property | Type | Description |
|---|---|---|
| clamp | Boolean | If true, values that exceed the data domain are clamped to either the minimum or maximum range value. Default value: derived from scale config (true by default)Supported types: only linear, pow, sqrt, and log |
| exponent | Number | Sets the exponent of the scale transformation. (For pow scale types only, otherwise ignored.) |
| nice | Boolean | If true, modifies the scale domain to use a more human-friendly number range (e.g., 7 instead of 6.96). Default value: true only for quantitative x and y scales and false otherwise. |
| zero | Boolean | If true, ensures that a zero baseline value is included in the scale domain. Default value: true for x and y channel if the quantitative field is not binned and no custom domain is provided; false otherwise.Note: This property is always false for log scale. |
| useRawDomain | Boolean | If true, set scale domain to the raw data domain. If false, use the aggregated data domain for scale. Default value: falseOnly valid for certain aggregations: This property only works with aggregate functions that produce values within the raw data domain ( "mean", "average", "stdev", "stdevp", "median", "q1", "q3", "min", "max"). For other aggregations that produce values outside of the raw data domain (e.g. "count", "sum"), this property is ignored. Note: This property is ignored when the scale’s domain is specified. |
Time Scale Properties
| Property | Type | Description |
|---|---|---|
| clamp | Boolean | If true, values that exceed the data domain are clamped to either the minimum or maximum range value. (Not applicable for quantile, quantize, and threshold scales as they output discrete ranges.) |
| nice | String | If specified, modifies the scale domain to use a more human-friendly value range. For time and utc scale types only, the nice value should be a string indicating the desired time interval; legal values are "second", "minute", "hour", "day", "week", "month", or "year". |
Ordinal Scale Properties
| Property | Type | Description |
|---|---|---|
| bandSize | Integer | String | Width for each x or y ordinal band. This can be an integer value or a string "fit". For "fit", the band size will be automatically adjusted to fit the scale for the specified width (for x-axis) or height (for y-axis). Default value: for x ordinal scale of a text mark, derived from scale config’s textBandWidth; otherwise, derived from scale config’s bandSize. Warning: 1) Numeric bandSize will be applied only if the top-level width (for x-scale) or height (for y-scale) is not specified. If width (for x-scale) or height (for y-scale) is specified, bandWidth will always be "fit". 2) If the cardinality of the scale domain is too high, the bandSize might become less than one pixel and the mark might not appear correctly. |
| padding | Number | • For x and y channels, the padding is a multiple of the spacing between points. A reasonable value is 1.0, such that the first and last point will be offset from the minimum and maximum value by half the distance between points. (See D3’s ordinalRangePoints() for illustration.) • Default value: derived from scale config’s padding • For row and column, padding is a pixel value for padding between cells in the trellis plots. •Default value: derived from facet scale config’s padding. |
Example: Custom Band Width
Given a bar chart:
We can make the band for each bar smaller by providing scale’s bandSize.
For more information about adjusting size of a visualization, please see this page.
#1f77b4
#ff7f0e
#2ca02c
#d62728
#9467bd
#8c564b
#e377c2
#7f7f7f
#bcbd22
#17becf
#aec7e8
#ffbb78
#98df8a
#ff9896
#c5b0d5
#c49c94
#f7b6d2
#c7c7c7
#dbdb8d
#9edae5
#393b79
#5254a3
#6b6ecf
#9c9ede
#637939
#8ca252
#b5cf6b
#cedb9c
#8c6d31
#bd9e39
#e7ba52
#e7cb94
#843c39
#ad494a
#d6616b
#e7969c
#7b4173
#a55194
#ce6dbd
#de9ed6
#3182bd
#6baed6
#9ecae1
#c6dbef
#e6550d
#fd8d3c
#fdae6b
#fdd0a2
#31a354
#74c476
#a1d99b
#c7e9c0
#756bb1
#9e9ac8
#bcbddc
#dadaeb
#636363
#969696
#bdbdbd
#d9d9d9