Movatterモバイル変換


[0]ホーム

URL:


  • Overview
  • View Specification
  • Data / Datasets
  • Transform
  • Mark
  • Encoding
  • Projection
  • View Composition
  • Parameter
  • Config

  • Property Types
  • Tooltip
  • Invalid Data
  • Binning

    Edit this page

    Binning discretizes numeric values into a set of bins. A common use case is tocreate a histogram.

    There are two ways to define binning in Vega-Lite:thebin property in encoding field definitions andthebin transform.

    Documentation Overview

    Binning in Encoding Field Definition

    // A Single View or a Layer Specification{  ...,  "mark/layer": ...,  "encoding": {    "x": {      "bin": ..., // bin      "field": ...,      "type": "quantitative",      ...    },    "y": ...,    ...  },  ...}

    You can directly bin anencoding field by using thebin property in afield definition.

    PropertyTypeDescription
    binBoolean |BinParams | String | Null

    A flag for binning aquantitative field,an object defining binning parameters, or indicating that the data forx ory channel are binned before they are imported into Vega-Lite ("binned").

    • Iftrue, defaultbinning parameters will be applied.

    • If"binned", this indicates that the data for thex (ory) channel are already binned. You can map the bin-start field tox (ory) and the bin-end field tox2 (ory2). The scale and axis will be formatted similar to binning in Vega-Lite. To adjust the axis ticks based on the bin step, you can also set the axis’stickMinStep property.

    Default value:false

    See also:bin documentation.

    Example: Histogram

    Mapping binned values and its count to abar mark produces a histogram.

    Example: Histogram with Ordinal Scale

    While binned field has"quantitative" type by default, setting the binned field’stype to"ordinal" produces a histogram with an ordinal scale.

    Example: Binned color

    You can use binning to discretize color scales. Vega-Lite automatically creates legends with range labels.

    Example: Using Vega-Lite with Binned data

    If you have data that is already binned outside of Vega-Lite, setting thebin property to"binned" will trigger Vega-Lite to render scales and axes similar to setting thebin property in encoding field definitions. Note that you have to specify field names that encode the start and end of each bin. To adjust the axis ticks based on the bin step, you can setbin to e.g.{"binned": true, "step": 2}.

    Bin Transform

    // Any View Specification{  ...  "transform": [    {"bin": ..., "field": ..., "as" ...} // Bin Transform     ...  ],  ...}

    Thebin transform in thetransform array has the following properties:

    PropertyTypeDescription
    binBoolean |BinParams

    Required. An object indicating bin properties, or simplytrue for using default bin parameters.

    fieldString

    Required. The data field to bin.

    asString | String[]

    Required. The output fields at which to write the start and end bin values. This can be either a string or an array of strings with two elements denoting the name for the fields for bin start and bin end respectively. If a single string (e.g.,"val") is provided, the end field will be"val_end".

    Example: Histogram with Bin Transform

    Instead of using thebin property of a field definition, you can also use a bin transform to derive a new field (e.g.,bin_IMDB_Rating), and encode the new field with bin property of a field definition set tobinned instead.

    While binning intransform is more verbose than inencoding, it can be useful if you want to perform additional calculation before encoding the data.

    Bin Parameters

    Ifbin istrue, default binning properties are used. To customize binning properties, you can setbin to a bin definition object, which can have the following properties:

    PropertyTypeDescription
    anchorNumber

    A value in the binned domain at which to anchor the bins, shifting the bin boundaries if necessary to ensure that a boundary aligns with the anchor value.

    Default value: the minimum bin extent value

    baseNumber

    The number base to use for automatic bin determination (default is base 10).

    Default value:10

    divideNumber[]

    Scale factors indicating allowable subdivisions. The default value is [5, 2], which indicates that for base 10 numbers (the default base), the method may consider dividing bin sizes by 5 and/or 2. For example, for an initial step size of 10, the method can check if bin sizes of 2 (= 10/5), 5 (= 10/2), or 1 (= 10/(5*2)) might also satisfy the given constraints.

    Default value:[5, 2]

    extentArray

    A two-element ([min, max]) array indicating the range of desired bin values.

    maxbinsNumber

    Maximum number of bins.

    Default value:6 forrow,column andshape channels;10 for other channels

    minstepNumber

    A minimum allowable step size (particularly useful for integer values).

    niceBoolean

    If true, attempts to make the bin boundaries use human-friendly boundaries, such as multiples of ten.

    Default value:true

    stepNumber

    An exact step size to use between bins.

    Note: If provided, options such as maxbins will be ignored.

    stepsNumber[]

    An array of allowable step sizes to choose from.

    Example: Customizing Max Bins

    Setting themaxbins parameter changes the maximum number of output bins. There will often be fewer bins since the domain get sliced at “nicely-rounded” values.

    Ordinal Bin

    Usually, you should set the type of binned encodings to be quantitative. Vega-Lite automatically creates axes and legends that best represent binned data. However, if you want to sort the bins or skip empty bins, you can set the type to ordinal.

    For example, this following plot shows binned values sort by count.


[8]ページ先頭

©2009-2025 Movatter.jp