{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "Top-K plot with \"others\" by Trevor Manz, adapted from https://observablehq.com/@manzt/top-k-plot-with-others-vega-lite-example.", "background": "white", "padding": 5, "width": 200, "title": { "text": "Top Directors by Average Worldwide Gross", "frame": "group" }, "style": "cell", "data": [ { "name": "source_0", "url": "data/movies.json", "format": {"type": "json"}, "transform": [ { "type": "aggregate", "groupby": ["Director"], "ops": ["mean"], "fields": ["Worldwide Gross"], "as": ["aggregate_gross"] }, { "type": "window", "params": [null], "as": ["rank"], "ops": ["row_number"], "fields": [null], "sort": {"field": ["aggregate_gross"], "order": ["descending"]} }, { "type": "formula", "expr": "datum.rank < 10 ? datum.Director : 'All Others'", "as": "ranked_director" } ] }, { "name": "data_0", "source": "source_0", "transform": [ { "type": "aggregate", "groupby": ["ranked_director"], "ops": ["mean"], "fields": ["aggregate_gross"], "as": ["mean_aggregate_gross"] }, { "type": "filter", "expr": "isValid(datum[\"mean_aggregate_gross\"]) && isFinite(+datum[\"mean_aggregate_gross\"])" } ] } ], "signals": [ {"name": "y_step", "value": 20}, { "name": "height", "update": "bandspace(domain('y').length, 0.1, 0.05) * y_step" } ], "marks": [ { "name": "marks", "type": "rect", "style": ["bar"], "from": {"data": "data_0"}, "encode": { "update": { "fill": {"value": "#4c78a8"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"Mean of aggregate_gross: \" + (format(datum[\"mean_aggregate_gross\"], \"\")) + \"; ranked_director: \" + (isValid(datum[\"ranked_director\"]) ? datum[\"ranked_director\"] : \"\"+datum[\"ranked_director\"])" }, "x": {"scale": "x", "field": "mean_aggregate_gross"}, "x2": {"scale": "x", "value": 0}, "y": {"scale": "y", "field": "ranked_director"}, "height": {"scale": "y", "band": 1} } } } ], "scales": [ { "name": "x", "type": "linear", "domain": {"data": "data_0", "field": "mean_aggregate_gross"}, "range": [0, {"signal": "width"}], "nice": true, "zero": true }, { "name": "y", "type": "band", "domain": { "data": "source_0", "field": "ranked_director", "sort": { "op": "mean", "field": "aggregate_gross", "order": "descending" } }, "range": {"step": {"signal": "y_step"}}, "paddingInner": 0.1, "paddingOuter": 0.05 } ], "axes": [ { "scale": "x", "orient": "bottom", "gridScale": "y", "grid": true, "tickCount": {"signal": "ceil(width/40)"}, "domain": false, "labels": false, "aria": false, "maxExtent": 0, "minExtent": 0, "ticks": false, "zindex": 0 }, { "scale": "x", "orient": "bottom", "grid": false, "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/40)"}, "zindex": 0 }, {"scale": "y", "orient": "left", "grid": false, "zindex": 0} ] }