Carbon Dioxide in the Atmosphere

Note: When accessing aggregated argmax or argmin fields, the aggregated fields must be flattened using calculate (see https://github.com/vega/vega-lite/issues/3361)

View this example in the online editor

Vega-Lite JSON Specification

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "data": {
    "url": "data/co2-concentration.csv",
    "format": {
      "parse": {"Date": "utc:'%Y-%m-%d'"}
    }
  },
  "width": 800,
  "height": 500,
  "transform": [
    {
      "calculate": "year(datum.Date)",
      "as": "year"
    },
    {
      "calculate": "month(datum.Date)",
      "as": "month"
    },
    {
      "calculate": "floor(datum.year / 10) + 'x'",
      "as": "decade"
    },
    {
      "calculate": "(datum.year % 10) + (datum.month/12)",
      "as": "scaled_date"
    }
  ],
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "x": {
          "field": "scaled_date",
          "type": "quantitative",
          "axis": {
            "title": "Year into Decade",
            "tickCount": 11
          }
        },
        "y": {
          "field": "CO2",
          "axis": {
            "title": "CO2 concentration in ppm"
          },
          "type": "quantitative",
          "scale": {
            "zero": false
          }
        },
        "detail": {
          "field": "decade",
          "type": "ordinal"
        },
        "color": {
          "field": "decade",
          "type": "nominal",
          "legend": {
            "offset": 40
          }
        }
      }
    },
    {
      "transform": [
        {
          "aggregate": [{
            "op": "argmin",
            "field": "scaled_date",
            "as": "start"
          }, {
            "op": "argmax",
            "field": "scaled_date",
            "as": "end"
          }],
          "groupby": ["decade"]
        },
        {
          "calculate": "datum.start.scaled_date",
          "as": "scaled_date_start"
        },
        {
          "calculate": "datum.start.CO2",
          "as": "CO2_start"
        },
        {
          "calculate": "datum.start.year",
          "as": "year_start"
        },
        {
          "calculate": "datum.end.scaled_date",
          "as": "scaled_date_end"
        },
        {
          "calculate": "datum.end.CO2",
          "as": "CO2_end"
        },
        {
          "calculate": "datum.end.year",
          "as": "year_end"
        }
      ],
      "layer": [{
        "mark": {
          "type": "text",
          "align": "left",
          "baseline": "top",
          "dx": 3,
          "dy": 1
        },
        "encoding": {
          "x": {
            "field": "scaled_date_start",
            "type": "quantitative"
          },
          "y": {
            "field": "CO2_start",
            "type": "quantitative"
          },
          "text": {
            "field": "year_start",
            "type": "nominal"
          }
        }
      }, {
        "mark": {
          "type": "text",
          "align": "left",
          "baseline": "bottom",
          "dx": 3,
          "dy": 1
        },
        "encoding": {
          "x": {
            "field": "scaled_date_end",
            "type": "quantitative"
          },
          "y": {
            "field": "CO2_end",
            "type": "quantitative"
          },
          "text": {
            "field": "year_end",
            "type": "nominal"
          }
        }
      }]
    }
  ]
}