# Transforms

> Filter, aggregate, bin, fold, calculate and window data inside the spec, so published numbers trace back to source rows.

`transform` is an ordered list of steps run on `data` before anything is drawn, so a spec can carry its source rows unchanged and derive what it plots.

```json
{
  "mark": {
    "type": "line",
    "strokeWidth": 2,
    "point": true
  },
  "data": [
    {
      "month": "Jan",
      "low": 3.5,
      "high": 9.3
    },
    {
      "month": "Feb",
      "low": 0.9,
      "high": 9.2
    },
    {
      "month": "Mar",
      "low": 3.2,
      "high": 10.8
    },
    {
      "month": "Apr",
      "low": 9.4,
      "high": 19
    },
    {
      "month": "May",
      "low": 12,
      "high": 22.1
    },
    {
      "month": "Jun",
      "low": 16.8,
      "high": 25.4
    },
    {
      "month": "Jul",
      "low": 22.1,
      "high": 30.2
    },
    {
      "month": "Aug",
      "low": 20.2,
      "high": 27.6
    },
    {
      "month": "Sep",
      "low": 17.3,
      "high": 24.3
    },
    {
      "month": "Oct",
      "low": 12.4,
      "high": 19.3
    },
    {
      "month": "Nov",
      "low": 4.5,
      "high": 11.8
    },
    {
      "month": "Dec",
      "low": 4.2,
      "high": 9.8
    }
  ],
  "transform": [
    {
      "fold": [
        "high",
        "low"
      ],
      "as": [
        "measure",
        "temperature"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "ordinal",
      "sort": null,
      "axis": {
        "title": "2023"
      }
    },
    "y": {
      "field": "temperature",
      "type": "quantitative",
      "axis": {
        "format": ".0f",
        "labelSuffix": "°C",
        "grid": true,
        "title": "Temperature"
      }
    },
    "color": {
      "field": "measure",
      "type": "nominal",
      "scale": {
        "domain": [
          "high",
          "low"
        ],
        "range": [
          "#c2410c",
          "#0e7490"
        ]
      }
    }
  },
  "endpointLabels": {
    "content": "label"
  },
  "chrome": {
    "title": "Central Park's daily highs and lows both peaked in July 2023",
    "subtitle": "Monthly average of each day's high and low temperature, Central Park, New York, 2023 (°C)"
  }
}
```

## Fold wide columns into rows

The source has 12 rows, one per month, each with a `low` and a `high` column. A line chart with one color per series needs one row per value instead. `{ fold: ["high", "low"], as: ["measure", "temperature"] }` turns the 12 rows into 24, each with a `measure` of "high" or "low" and its `temperature`. The chart then puts `measure` on `color`.

## Calculate a new field

```json
{
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "month": "Jan",
      "low": 3.5,
      "high": 9.3
    },
    {
      "month": "Feb",
      "low": 0.9,
      "high": 9.2
    },
    {
      "month": "Mar",
      "low": 3.2,
      "high": 10.8
    },
    {
      "month": "Apr",
      "low": 9.4,
      "high": 19
    },
    {
      "month": "May",
      "low": 12,
      "high": 22.1
    },
    {
      "month": "Jun",
      "low": 16.8,
      "high": 25.4
    },
    {
      "month": "Jul",
      "low": 22.1,
      "high": 30.2
    },
    {
      "month": "Aug",
      "low": 20.2,
      "high": 27.6
    },
    {
      "month": "Sep",
      "low": 17.3,
      "high": 24.3
    },
    {
      "month": "Oct",
      "low": 12.4,
      "high": 19.3
    },
    {
      "month": "Nov",
      "low": 4.5,
      "high": 11.8
    },
    {
      "month": "Dec",
      "low": 4.2,
      "high": 9.8
    }
  ],
  "transform": [
    {
      "calculate": {
        "op": "-",
        "field": "high",
        "field2": "low"
      },
      "as": "swing"
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "ordinal",
      "sort": null,
      "axis": {
        "title": "2023"
      }
    },
    "y": {
      "field": "swing",
      "type": "quantitative",
      "axis": {
        "format": ".0f",
        "labelSuffix": "°C",
        "grid": true,
        "title": "High minus low"
      }
    }
  },
  "labels": {
    "format": ".1f"
  },
  "chrome": {
    "title": "Central Park's gap between daily high and low was widest in May 2023, at 10.1°C",
    "subtitle": "Average daily high minus average daily low by month, Central Park, New York, 2023 (°C)"
  }
}
```

`calculate` adds a field to every row. `{ op: "-", field: "high", field2: "low" }` subtracts the two columns, and `as: "swing"` names the result. The same 12 rows now give the gap between each month's average high and low, which peaks at 10.1°C in May. The ops are arithmetic only: `+`, `-`, `*`, `/`, plus `abs`, `round`, `floor`, `ceil`, `log` and `sqrt` on one field.

## Filter and aggregate

```json
{
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "year": "2016",
      "source": "Coal",
      "share": 31.2
    },
    {
      "year": "2016",
      "source": "Gas",
      "share": 34.8
    },
    {
      "year": "2016",
      "source": "Nuclear",
      "share": 20.3
    },
    {
      "year": "2016",
      "source": "Hydro",
      "share": 6.6
    },
    {
      "year": "2016",
      "source": "Wind",
      "share": 5.7
    },
    {
      "year": "2016",
      "source": "Solar",
      "share": 1.4
    },
    {
      "year": "2017",
      "source": "Coal",
      "share": 30.7
    },
    {
      "year": "2017",
      "source": "Gas",
      "share": 33
    },
    {
      "year": "2017",
      "source": "Nuclear",
      "share": 20.5
    },
    {
      "year": "2017",
      "source": "Hydro",
      "share": 7.5
    },
    {
      "year": "2017",
      "source": "Wind",
      "share": 6.5
    },
    {
      "year": "2017",
      "source": "Solar",
      "share": 2
    },
    {
      "year": "2018",
      "source": "Coal",
      "share": 28.2
    },
    {
      "year": "2018",
      "source": "Gas",
      "share": 36
    },
    {
      "year": "2018",
      "source": "Nuclear",
      "share": 19.8
    },
    {
      "year": "2018",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2018",
      "source": "Wind",
      "share": 6.7
    },
    {
      "year": "2018",
      "source": "Solar",
      "share": 2.3
    },
    {
      "year": "2019",
      "source": "Coal",
      "share": 23.9
    },
    {
      "year": "2019",
      "source": "Gas",
      "share": 39.2
    },
    {
      "year": "2019",
      "source": "Nuclear",
      "share": 20
    },
    {
      "year": "2019",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2019",
      "source": "Wind",
      "share": 7.3
    },
    {
      "year": "2019",
      "source": "Solar",
      "share": 2.6
    },
    {
      "year": "2020",
      "source": "Coal",
      "share": 19.6
    },
    {
      "year": "2020",
      "source": "Gas",
      "share": 41.3
    },
    {
      "year": "2020",
      "source": "Nuclear",
      "share": 20.1
    },
    {
      "year": "2020",
      "source": "Hydro",
      "share": 7.1
    },
    {
      "year": "2020",
      "source": "Wind",
      "share": 8.6
    },
    {
      "year": "2020",
      "source": "Solar",
      "share": 3.3
    },
    {
      "year": "2021",
      "source": "Coal",
      "share": 22.2
    },
    {
      "year": "2021",
      "source": "Gas",
      "share": 39
    },
    {
      "year": "2021",
      "source": "Nuclear",
      "share": 19.3
    },
    {
      "year": "2021",
      "source": "Hydro",
      "share": 6.1
    },
    {
      "year": "2021",
      "source": "Wind",
      "share": 9.3
    },
    {
      "year": "2021",
      "source": "Solar",
      "share": 4.1
    },
    {
      "year": "2022",
      "source": "Coal",
      "share": 19.9
    },
    {
      "year": "2022",
      "source": "Gas",
      "share": 40.4
    },
    {
      "year": "2022",
      "source": "Nuclear",
      "share": 18.5
    },
    {
      "year": "2022",
      "source": "Hydro",
      "share": 6
    },
    {
      "year": "2022",
      "source": "Wind",
      "share": 10.4
    },
    {
      "year": "2022",
      "source": "Solar",
      "share": 4.9
    },
    {
      "year": "2023",
      "source": "Coal",
      "share": 16.2
    },
    {
      "year": "2023",
      "source": "Gas",
      "share": 43.5
    },
    {
      "year": "2023",
      "source": "Nuclear",
      "share": 18.6
    },
    {
      "year": "2023",
      "source": "Hydro",
      "share": 5.8
    },
    {
      "year": "2023",
      "source": "Wind",
      "share": 10.1
    },
    {
      "year": "2023",
      "source": "Solar",
      "share": 5.8
    }
  ],
  "transform": [
    {
      "filter": {
        "field": "source",
        "oneOf": [
          "Wind",
          "Solar"
        ]
      }
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "share",
          "as": "windSolar"
        }
      ],
      "groupby": [
        "year"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "windSolar",
      "type": "quantitative",
      "axis": {
        "title": "Wind plus solar",
        "format": ".0f",
        "labelSuffix": "%"
      }
    }
  },
  "labels": {
    "format": ".1f"
  },
  "chrome": {
    "title": "Wind and solar went from 7.1% of generation from the six main sources in 2016 to 15.9% in 2023",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%), wind and solar combined"
  }
}
```

Transforms run in order, each on the output of the one before. The source has 48 rows, six sources for each of eight years. `filter` with `oneOf: ["Wind", "Solar"]` keeps 16 of them. `aggregate` with `op: "sum"` and `groupby: ["year"]` collapses those to eight rows, one per year, holding the two shares added together: 5.7% plus 1.4% gives the 7.1% for 2016.

## Bin values into ranges

```json
{
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "state": "AL",
      "rate": 2.7
    },
    {
      "state": "AK",
      "rate": 4.8
    },
    {
      "state": "AZ",
      "rate": 4.7
    },
    {
      "state": "AR",
      "rate": 4.2
    },
    {
      "state": "CA",
      "rate": 5.3
    },
    {
      "state": "CO",
      "rate": 3.8
    },
    {
      "state": "CT",
      "rate": 4.8
    },
    {
      "state": "DE",
      "rate": 5.4
    },
    {
      "state": "DC",
      "rate": 5.7
    },
    {
      "state": "FL",
      "rate": 4.4
    },
    {
      "state": "GA",
      "rate": 3.5
    },
    {
      "state": "HI",
      "rate": 2.4
    },
    {
      "state": "ID",
      "rate": 3.6
    },
    {
      "state": "IL",
      "rate": 5.1
    },
    {
      "state": "IN",
      "rate": 3.6
    },
    {
      "state": "IA",
      "rate": 3.3
    },
    {
      "state": "KS",
      "rate": 3.8
    },
    {
      "state": "KY",
      "rate": 4.2
    },
    {
      "state": "LA",
      "rate": 4.4
    },
    {
      "state": "ME",
      "rate": 3.3
    },
    {
      "state": "MD",
      "rate": 4.3
    },
    {
      "state": "MA",
      "rate": 4.7
    },
    {
      "state": "MI",
      "rate": 5.1
    },
    {
      "state": "MN",
      "rate": 4.9
    },
    {
      "state": "MS",
      "rate": 3.8
    },
    {
      "state": "MO",
      "rate": 4.2
    },
    {
      "state": "MT",
      "rate": 3.6
    },
    {
      "state": "NE",
      "rate": 3.1
    },
    {
      "state": "NV",
      "rate": 5.1
    },
    {
      "state": "NH",
      "rate": 3
    },
    {
      "state": "NJ",
      "rate": 4.9
    },
    {
      "state": "NM",
      "rate": 4.8
    },
    {
      "state": "NY",
      "rate": 4.4
    },
    {
      "state": "NC",
      "rate": 3.7
    },
    {
      "state": "ND",
      "rate": 2.8
    },
    {
      "state": "OH",
      "rate": 4
    },
    {
      "state": "OK",
      "rate": 3.9
    },
    {
      "state": "OR",
      "rate": 5.2
    },
    {
      "state": "PA",
      "rate": 4.2
    },
    {
      "state": "RI",
      "rate": 4.9
    },
    {
      "state": "SC",
      "rate": 4.2
    },
    {
      "state": "SD",
      "rate": 2.3
    },
    {
      "state": "TN",
      "rate": 3.3
    },
    {
      "state": "TX",
      "rate": 4.3
    },
    {
      "state": "UT",
      "rate": 3.8
    },
    {
      "state": "VT",
      "rate": 2.6
    },
    {
      "state": "VA",
      "rate": 3.8
    },
    {
      "state": "WA",
      "rate": 5.1
    },
    {
      "state": "WV",
      "rate": 4.5
    },
    {
      "state": "WI",
      "rate": 4
    },
    {
      "state": "WY",
      "rate": 3.8
    }
  ],
  "transform": [
    {
      "bin": {
        "step": 0.5,
        "extent": [
          2,
          6
        ]
      },
      "field": "rate",
      "as": [
        "rateFrom",
        "rateTo"
      ]
    },
    {
      "aggregate": [
        {
          "op": "count",
          "field": "state",
          "as": "states"
        }
      ],
      "groupby": [
        "rateFrom",
        "rateTo"
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "rateFrom",
      "type": "quantitative",
      "axis": {
        "title": "Unemployment rate",
        "format": ".1f",
        "labelSuffix": "%"
      }
    },
    "x2": {
      "field": "rateTo",
      "type": "quantitative"
    },
    "y": {
      "field": "states",
      "type": "quantitative",
      "axis": {
        "title": "States",
        "tickCount": 4
      }
    }
  },
  "labels": {
    "format": ".0f"
  },
  "chrome": {
    "title": "Unemployment was between 3% and 5% in 38 of the 50 states and DC",
    "subtitle": "Number of states by unemployment rate, in half-point ranges. Seasonally adjusted, latest month."
  }
}
```

A histogram counts how many rows fall in each range. `bin` with `step: 0.5` writes each state's range start and end into `rateFrom` and `rateTo`, and `aggregate` with `op: "count"` grouped by both fields turns 51 rows into one count per range. Putting `rateTo` on `x2` makes each bar span its range on a continuous axis. `mark: "histogram"` does the same two steps for you.

## Fields that matter

| Transform | What it does |
| --- | --- |
| `fold` | Wide to long: one output row per listed column, with key and value fields. |
| `calculate` | Adds a field from arithmetic on one or two fields, or a field and a constant. |
| `filter` | Keeps rows matching a predicate: `equal`, `oneOf`, `lt`, `gte`, `range` and more, combined with `and`, `or` and `not`. |
| `aggregate` | Collapses each group to one row: `sum`, `mean`, `median`, `count`, `min`, `max` and others. |
| `joinaggregate` | Same ops, but keeps every row and writes the group summary onto each. |
| `bin` | Writes the range a value falls in, by `step` or `maxbins`. |
| `window` | Running values in sort order: `cumsum`, `rank`, `lag`, `diff`, `pct_change`. |
| `timeUnit` | Truncates a date to a unit such as year or month. |

## Related

- [Histogram](/docs/distribution/histogram/) uses binning through `mark: "histogram"`.
- [Stacked area](/docs/change-over-time/stacked-area/) plots long rows like the folded ones here.
- [Transforms reference](/docs/spec/transforms/) lists every transform and its fields.
