This website is for Vega-Lite v3. Go to the main Vega-Lite homepage for the latest release.

Top-K plot with Others

By @manzt, adapted from https://observablehq.com/@manzt/top-k-plot-with-others-vega-lite-example.

View this example in the online editor

Vega-Lite JSON Specification

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "description": "Top-K plot with \"others\" by Trevor Manz, adapted from https://observablehq.com/@manzt/top-k-plot-with-others-vega-lite-example.",
  "title": "Top Directors by Average Worldwide Gross",
  "data": {"url": "data/movies.json"},
  "mark": "bar",
  "transform": [
    {
      "aggregate": [{"op": "mean", "field": "Worldwide_Gross", "as": "aggregate_gross"}],
      "groupby": ["Director"]
    },
    {
      "window": [{"op": "row_number", "as": "rank"}],
      "sort": [{ "field": "aggregate_gross", "order": "descending" }]
    },
    {
      "calculate": "datum.rank < 10 ? datum.Director : 'All Others'", "as": "ranked_director"
    }
  ],
  "encoding": {
    "x": {
      "aggregate": "mean",
      "field": "aggregate_gross",
      "type": "quantitative",
      "title": null
    },
    "y": {
      "sort": {"op": "mean", "field": "aggregate_gross", "order": "descending"},
      "field": "ranked_director",
      "type": "ordinal",
      "title": null
    }
  }
}