# Encoding channels

> Map fields to x, y, color and size, and see the same data read differently as the channels change.

`encoding` decides which field drives which visual property. A bar chart puts the category on `y` and the number on `x`.

```json
{
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "country": "India",
      "population": 1463
    },
    {
      "country": "China",
      "population": 1410
    },
    {
      "country": "United States",
      "population": 347
    },
    {
      "country": "Indonesia",
      "population": 285
    },
    {
      "country": "Pakistan",
      "population": 255
    },
    {
      "country": "Nigeria",
      "population": 240
    },
    {
      "country": "Brazil",
      "population": 217
    },
    {
      "country": "Bangladesh",
      "population": 175
    },
    {
      "country": "Russia",
      "population": 144
    },
    {
      "country": "Ethiopia",
      "population": 135
    }
  ],
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population (millions)",
        "format": ",.0f"
      }
    }
  },
  "labels": false,
  "chrome": {
    "title": "India and China each have more than four times as many people as the US",
    "subtitle": "Population of the ten most populous countries, 2025 (millions)"
  }
}
```

## Swapping x and y

```json
{
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "country": "India",
      "population": 1463
    },
    {
      "country": "China",
      "population": 1410
    },
    {
      "country": "United States",
      "population": 347
    },
    {
      "country": "Indonesia",
      "population": 285
    },
    {
      "country": "Pakistan",
      "population": 255
    },
    {
      "country": "Nigeria",
      "population": 240
    },
    {
      "country": "Brazil",
      "population": 217
    },
    {
      "country": "Bangladesh",
      "population": 175
    },
    {
      "country": "Russia",
      "population": 144
    },
    {
      "country": "Ethiopia",
      "population": 135
    }
  ],
  "encoding": {
    "x": {
      "field": "country",
      "type": "nominal",
      "sort": "-y"
    },
    "y": {
      "field": "population",
      "type": "quantitative",
      "axis": {
        "title": "Population (millions)",
        "format": ",.0f"
      }
    }
  },
  "labels": false,
  "chrome": {
    "title": "India and China each have more than four times as many people as the US",
    "subtitle": "Population of the ten most populous countries, 2025 (millions)"
  }
}
```

The data and mark are unchanged; only the two channels trade places. With the category on `x` and the number on `y`, the same `mark: "bar"` draws columns. `sort: "-x"` becomes `sort: "-y"`, so the largest value still comes first. Long names like "United States" fit better down the side of a bar chart than under a column, which is why bars suit ranked countries.

## Color

```json
{
  "mark": "bar",
  "data": [
    {
      "country": "India",
      "population": 1438.069596,
      "gdpPerCapita": 9818,
      "lifeExpectancy": 68.3
    },
    {
      "country": "China",
      "population": 1410.71,
      "gdpPerCapita": 23846,
      "lifeExpectancy": 76.1
    },
    {
      "country": "United States",
      "population": 336.806231,
      "gdpPerCapita": 75489,
      "lifeExpectancy": 79.2
    },
    {
      "country": "Indonesia",
      "population": 281.190067,
      "gdpPerCapita": 14470,
      "lifeExpectancy": 69
    },
    {
      "country": "Brazil",
      "population": 211.140729,
      "gdpPerCapita": 19652,
      "lifeExpectancy": 75.3
    },
    {
      "country": "Mexico",
      "population": 129.739759,
      "gdpPerCapita": 22040,
      "lifeExpectancy": 76.9
    },
    {
      "country": "Japan",
      "population": 124.51665,
      "gdpPerCapita": 46107,
      "lifeExpectancy": 83.6
    },
    {
      "country": "Germany",
      "population": 83.287273,
      "gdpPerCapita": 63676,
      "lifeExpectancy": 80.8
    },
    {
      "country": "United Kingdom",
      "population": 68.492,
      "gdpPerCapita": 53139,
      "lifeExpectancy": 81.4
    },
    {
      "country": "France",
      "population": 68.372286,
      "gdpPerCapita": 54799,
      "lifeExpectancy": 82.4
    },
    {
      "country": "South Korea",
      "population": 51.712619,
      "gdpPerCapita": 55071,
      "lifeExpectancy": 81.9
    },
    {
      "country": "Canada",
      "population": 40.083484,
      "gdpPerCapita": 58321,
      "lifeExpectancy": 82.2
    }
  ],
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": "-x"
    },
    "x": {
      "field": "lifeExpectancy",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "color": {
      "condition": {
        "test": {
          "field": "gdpPerCapita",
          "gte": 40000
        },
        "value": "#0e7490"
      },
      "value": "#7a858f"
    }
  },
  "labels": {
    "format": ".1f"
  },
  "chrome": {
    "title": "The seven countries here with GDP per person above $40,000 also have the longest lives",
    "subtitle": "Life expectancy at birth (years). Teal bars: GDP per person above $40,000 (PPP, constant international $)."
  }
}
```

`color` brings in a third field. Here a `condition` tests each row's `gdpPerCapita`: rows at $40,000 or more get the accent color and the rest get grey, so the chart shows income and life expectancy together. A `nominal` field on `color` gives one hue per category instead, which is how a line chart gets one color per series.

## Size

```json
{
  "mark": {
    "type": "point",
    "fill": "#0e7490",
    "opacity": 0.7,
    "trendline": false
  },
  "data": [
    {
      "country": "India",
      "population": 1438.069596,
      "gdpPerCapita": 9818,
      "lifeExpectancy": 68.3
    },
    {
      "country": "China",
      "population": 1410.71,
      "gdpPerCapita": 23846,
      "lifeExpectancy": 76.1
    },
    {
      "country": "United States",
      "population": 336.806231,
      "gdpPerCapita": 75489,
      "lifeExpectancy": 79.2
    },
    {
      "country": "Indonesia",
      "population": 281.190067,
      "gdpPerCapita": 14470,
      "lifeExpectancy": 69
    },
    {
      "country": "Brazil",
      "population": 211.140729,
      "gdpPerCapita": 19652,
      "lifeExpectancy": 75.3
    },
    {
      "country": "Mexico",
      "population": 129.739759,
      "gdpPerCapita": 22040,
      "lifeExpectancy": 76.9
    },
    {
      "country": "Japan",
      "population": 124.51665,
      "gdpPerCapita": 46107,
      "lifeExpectancy": 83.6
    },
    {
      "country": "Germany",
      "population": 83.287273,
      "gdpPerCapita": 63676,
      "lifeExpectancy": 80.8
    },
    {
      "country": "United Kingdom",
      "population": 68.492,
      "gdpPerCapita": 53139,
      "lifeExpectancy": 81.4
    },
    {
      "country": "France",
      "population": 68.372286,
      "gdpPerCapita": 54799,
      "lifeExpectancy": 82.4
    },
    {
      "country": "South Korea",
      "population": 51.712619,
      "gdpPerCapita": 55071,
      "lifeExpectancy": 81.9
    },
    {
      "country": "Canada",
      "population": 40.083484,
      "gdpPerCapita": 58321,
      "lifeExpectancy": 82.2
    }
  ],
  "encoding": {
    "x": {
      "field": "gdpPerCapita",
      "type": "quantitative",
      "axis": {
        "title": "GDP per person (PPP, constant international $)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "lifeExpectancy",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "size": {
      "field": "population",
      "type": "quantitative",
      "title": "Population (millions)",
      "format": ",.0f",
      "scale": {
        "domain": [
          0,
          1500
        ],
        "range": [
          0,
          22
        ]
      }
    },
    "tooltip": [
      {
        "field": "country",
        "type": "nominal",
        "title": "Country"
      },
      {
        "field": "population",
        "type": "quantitative",
        "title": "Population (millions)",
        "format": ",.0f"
      }
    ]
  },
  "annotations": [
    {
      "type": "text",
      "x": 9818,
      "y": 68.3,
      "text": "India",
      "anchor": "top",
      "offset": {
        "dy": -30
      },
      "connector": false,
      "responsive": false
    },
    {
      "type": "text",
      "x": 23846,
      "y": 76.1,
      "text": "China",
      "anchor": "right",
      "offset": {
        "dx": 34
      },
      "connector": false,
      "responsive": false
    }
  ],
  "chrome": {
    "title": "India and China hold two-thirds of these 12 countries' people, and India has the lowest income",
    "subtitle": "GDP per person against life expectancy at birth. Circle area is population."
  }
}
```

`size` scales each point by a field, which turns a scatter into a bubble chart. By default radii run from 3 px for the smallest value to 30 px for the largest, which keeps small countries visible but makes area only roughly track the value. This demo overrides that with `scale: { domain: [0, 1500], range: [0, 22] }`: with both ends at zero, a country twice as populous gets a circle twice the area. `trendline: false` on the mark turns off the regression line scatter charts draw by default.

## Time

`time` turns a chart into a playback. Each distinct value of its field is one period, and the chart plays through them in order with play, pause and a scrubber. Temporal and quantitative fields sort by value. Ordinal fields play in the order their values first appear in the data. Set `sort` to a list of values to choose the order, or to `"descending"` to reverse it. `format` sets how each period reads in the large period label and the player.

```json title="time.json"
"encoding": {
  "x": { "field": "gdp", "type": "quantitative", "scale": { "type": "log" } },
  "y": { "field": "life", "type": "quantitative" },
  "key": { "field": "country", "type": "nominal" },
  "time": { "field": "year", "type": "temporal" }
}
```

`time` works on point, circle and bar marks, on choropleths and tile maps, and as `points.time` on a map's points layer. It is an error on any other mark, with `facet`, inside a layer and with `youDrawIt`. The [bubble playback](/docs/correlation/bubble-playback/), [population pyramid](/docs/comparison/pyramid/) and [time-lapse map](/docs/maps/time-lapse/) pages walk through full examples, and the [playback](/docs/change-over-time/playback/) page covers the timing options.

Playback matches each mark in one period to the same mark in the next by its identity:

| Mark or map | Identity | Required |
| --- | --- | --- |
| `point`, `circle` | The `key` field. | Yes. Without it the spec fails validation. |
| `bar`, mirrored or not | The category field together with the `color` field, the same pair that identifies a bar without `time`. | No. Every category and color pair is already one bar. |
| Choropleth | The `key` field, which also joins rows to features. | Yes, as on any choropleth. |
| Tile map | The `state` field. | Yes, as on any tile map with rows. |
| Map points layer | `points.key`. | Yes, once the layer has `points.time`. |

Each identity can have one row per period. Two rows with the same identity and period fail validation with a message naming both values; aggregate them first.

## Fields that matter

| Field | What it does |
| --- | --- |
| `x`, `y` | Position. The channel holding the category sets the orientation of bars. |
| `color` | A field (one hue per category for `nominal` fields), a `condition` on a field, or a fixed `value`. `highlight` greys out every series but the named ones. |
| `size` | Point area from a quantitative field. `scale.range` is in pixels of radius. |
| `theta` | The slice value on pie and donut charts. |
| `sort` | Category order. `"-x"` on a bar chart's `y` and `"-y"` on a column chart's `x` put the largest value first. |
| `tooltip` | Extra fields shown on hover, independent of the other channels. |
| `time` | The period field a chart or map plays through. Point, circle and bar marks, choropleths and tile maps. |
| `key` | The field that identifies a point across periods. Required on point and circle charts with `time`. |

## Related

- [Bar](/docs/comparison/bar/) and [Column](/docs/comparison/column/) cover ranked categories in each orientation.
- [Bubble](/docs/correlation/bubble/) adds color to a sized scatter.
- [Field types and scales](/docs/the-spec/scales/) explains what `type` does to each channel.
- [Encoding reference](/docs/spec/encoding/) lists every channel and option.
