Calculate Difference from Annual Average

View this example in the online editor

Vega-Lite JSON Specification

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "description": "Bar graph showing the best films for the year they were produced, where best is defined by at least 2.5 points above average for that year. The red point shows the average rating for a film in that year, and the bar is the rating that the film recieved.",
  "data": {
    "url": "data/movies.json",
    "format": {
      "parse": {"Release_Date": "date:'%d-%b-%y'"}
    }
  },
  "transform": [
      {"filter": "datum.IMDB_Rating != null"},
      {"timeUnit": "year", "field": "Release_Date", "as": "year"},
      {
        "window": [{
            "op": "mean",
            "field": "IMDB_Rating",
            "as": "AverageYearRating"
        }],
        "groupby": [
            "year"
        ],
        "frame": [null, null]
      },
      {
        "filter": "(datum.IMDB_Rating - datum.AverageYearRating) > 2.5"
      }
  ],
  "layer": [{
      "mark": {"type": "bar", "clip": true},
      "encoding": {
        "x": {
          "field": "IMDB_Rating",
          "type": "quantitative",
          "axis": {"title": "IMDB Rating"}
        },
        "y": {
          "field": "Title",
          "type": "ordinal"
        }
      }
    },
    {
      "mark": "tick",
      "encoding": {
        "x": {
          "field": "AverageYearRating",
          "type": "quantitative"
        },
        "y": {
          "field": "Title",
          "type": "ordinal"
        },
        "color": {"value": "red"}
      }
    }
  ]
}