scales Property

The Vega scales property maps visually encoded data values to pixel positions with attributes, such as color. See the D3 scales documentation for additional background information about scales.

General scales property JSON format:

"scales": [
  {
    "name": <scaleID>,
    "type": <scaleType>,
    "domain": <inputValues>,
    "range": <outputValues>"
    "accumulator": <accumulatorType>
    "default": <defaultOutputValue>,
    "nullValue": <nullDataValue>
  },
  {
     ...
  }
],

The scales specification is one or more arrays with the following properties:

Property Field Data Type Required Description
name string X User-defined scale name.
type string  

Scale type, which specifies the domain-to-range transform:

  • linear: Quantitative, continuous scale that preserves proportion among data items.
  • log: Quantitative scale that applies a logarithmic transform to the data.
  • ordinal: Discrete domain and range scale.
  • pow: Quantitative scale that applies an exponential transform to the input data.
  • quantize: Quantitative, discrete scale that divides input data into segments.
  • sqrt: Quantitative scale that applies an square root transform to the input data.
  • threshold: Discrete scale that maps arbitrary domain subsets to discrete range values.
domain array   Domain. Array of input interval data values.
range string or array   Range. Array of output interval visual data values.
default number   Default output value to use when domain value does not map to range value.
accumulator string  

Accumulation rendering type:

blend: Blends colors by category. Works only for discrete output scales (ordinal, quantize, and threshold).

density: Performs count aggregation per pixel and applies the supplied color based on the normalization of the per-pixel aggregated counts over a specified range. The range is determined by the required minDensityCnt and maxDensityCnt properties. minDensityCnt and maxDensityCnt can be explicit integer values or one of the following keywords that automatically compute statistical information about the per-pixel counts:

  • min
  • max
  • -1stStdDev
  • -2ndStdDev
  • 1stStdDev
  • 2ndStdDev

pct: Apply a color range based on percentage accumulation for a specific category.

nullValue number   Specify the output value to use when the input value is null.

Note: As a general rule, limit the total number of domain and range values used to a maximum of 1000. Exceeding this limit can cause an error.

Example

Define two scales, x and y. For the x scale, linearly transform input data values between -100 and 999 to the visualization area width. For the y scale, linearly transform input data values between 0 and 500 to the visualization area height. The width and height range values are pre-defined literals that reference the width and height properties.

vegaSpec = {
    width: 384,
    height: 564,
    data: [ ... elided ... ],
    scales: [
        {
            name: "x",
            type: "linear",
            domain: [ -100, 999 ],
            range: "width"
        },
        {
            name: "y",
            type: "linear",
            domain: [ 0, 500 ],
            range: "height"
        }
    ],
    marks: [ ... elided ... ]
};

Scales Properties

name

The name property uniquely identifies the scale for reference by other properties.

type

The type property specifies how to transform the input, domain data to output, range visual values. Vega supports the following transforms, categorized by quantitative, discrete, and discretizing scales:

Quantitative Scales

Type Description Additional Information
linear Preserves proportional differences, where range value y can be expressed as a linear function of the domain value x: y = mx + b. D3 linear scale
log

Applies a logarithmic transform to the input domain value before the output range value is computed. The mapping to the range value y can be expressed as a logarithmic function of the domain value x: y = m log(x) + b.

As log(0) = -∞, a log scale domain must be strictly-positive or strictly-negative. The domain must not include or cross zero. A log scale with a positive domain has a well-defined behavior for positive values. A log scale with a negative domain has a well-defined behavior for negative values. For a negative domain, input and output values are implicitly multiplied by -1. The behavior of the scale is undefined if you compute a negative value for a log scale with a positive domain, and vice versa.

log scale values must be positive. Default = base 10.

D3 logarithmic scale
pow

Applies an exponential transform to the input domain value before the output range value is computed. Range value y can be expressed as a polynomial function of the domain value x: y = mx^k + b, where k is the exponent. Power scales also support negative domain values, and input value and resulting output value are then multiplied by -1.

Default exponent = 1.

D3 power scale
sqrt

A shorthand for power scales with an exponent of 0.5, indicating a square root transform.

sqrt scale values must be positive.

D3 sqrt scale

Discrete Scales

Type Description Resource
ordinal

Applies a discrete domain-to-range transform, and functions as a lookup table from a domain value to a range value.

Specify a default value for domain values that do not map to a range.

D3 ordinal scale

Discretizing Scales

Type Description Resource
quantize Divides input domain values into uniform segments based on the number of values in, or the cardinality of, the output range, where range value y can be expressed as a quantized linear function of the domain value x: y = m round(x) + b. D3 quantize scale
threshold Maps arbitrary, non-uniform subsets of the domain to discrete range values. The input domain is continuous but divided into slices based on a set of domain threshold values. The range must have N+1 elements, where N is the number of domain threshold boundaries. D3 threshold scale

domain

The domain field specifies the domain of input data values. For quantitative data, this can take the form of a two-element array.

Example:

Specify minimum and maximum input values.

domain: [ -100, 999 ]

For ordinal or categorical data, the domain can be an array of valid input values.

Example

Specify valid input data languages.

"domain": ["en",  "es", "fr"]

range

Scale range specifies the set of visual values. For numeric values, the range can take the form of a two-element array with minimum and maximum values. For ordinal or quantized data, the range can be an array of desired output values, which are mapped to elements in the specified domain.

Scale ranges can be specified in the following ways:

  • As an array of static values: "range": [0, 500] or "range": ['a', 'b', 'c'].
  • Using pre-defined literals: "range": "width" or "range": "height".

Example

Specify a color scale that quantizes input values between 0 and 100 among five visual output colors.

{
  name: "color",
  type: "quantize",
  domain: [ 0, 100 ],
  range: [ "#115f9a", "#1984c5", "#c9e52f", "#d0ee11", "#d0f400"
  ]
}

Scale ranges can accept width and height string literals that map to the Width and Height Properties.

Value Description
width A spatial range that is the value of t``width``.
height A spatial range that is the value of height. The direction of the range, top-to-bottom or bottom-to-top, is determined by to the scale type.

Example

Specify a y scale that linearly maps input values between 0 and 500 to the height of the visualization area.

{
    name: "y",
    type: "linear",
    domain: [ 0, 500 ],
    range: "height"
}

default

The default scales property specifies the output value to use when the input domain value does not map to the range.

The default property is not applicable to the threshold scale type, which maps domain values outside of the range to either the lowest or highest range value.

accumulator

The accumulator property enables you to identify regional density of data in a layer of a backend render and apply pixel coloring based on the accumulation mode that you have defined. Each data point is rendered individually, providing an accurate representation of data distribution in a spatial setting.

Mode Description
density

Perform count aggregation per pixel and define a color for a pixel by normalizing the count and applying a color to it based on a color scale.

You can activate density accumulation for any scale that takes as input a continuous domain (linear, sqrt, pow, log, threshold scales) and outputs a color range. The range is determined by the required minDensityCnt and maxDensityCnt properties. minDensityCnt and maxDensityCnt can be explicit integer values or one of the following keywords that automatically compute statistical information about the per-pixel counts:

  • min
  • max
  • -1stStdDev
  • -2ndStdDev
  • 1stStdDev
  • 2ndStdDev

Note: Domain values of density accumulators must be between 0 and 1 inclusive.

blend Blend by category (ultimately an ordinal scale). You can provide a color to a category and blend those colors to show the density of the distinct categorical values at a pixel.
pct For a specific category, apply color based on the percentage of the category in a region.

Example

Apply a density accumulator to a linear scale named pointcolor:

{
  "name": "pointcolor",
  "type": "linear",
  "domain": [0.0,1.0],
  "range": ["blue","red"],
  "clamp": true,
  "accumulator": "density",
  "minDensityCnt": 1,
  "maxDensityCnt": 100
}

The color at a pixel is determined by normalizing per-pixel aggregated counts and using that value in the scale function to calculate a color. Normalization is performed according to the required minDensityCnt and maxDensityCnt properties. After normalization, minDensityCnt == 0 and maxDensityCnt == 1.

minDensityCnt and maxDensityCnt can have explicit integer values or use one of the following keywords to compute statistical information about per-pixel counts: min, max, -1stStdDev, -2ndStdDev, 1stStdDev, 2ndStdDev.

For more detailed examples of using accumulators, see Tutorial: Vega Accumulator.