{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A population pyramid for the US in 2000, created using stack. See https://vega.github.io/vega-lite/examples/concat_population_pyramid.html for a variant of this created using concat.", "background": "white", "padding": 5, "width": 300, "height": 200, "style": "cell", "data": [ { "name": "source_0", "url": "data/population.json", "format": {"type": "json"}, "transform": [ {"type": "filter", "expr": "datum.year == 2000"}, { "type": "formula", "expr": "datum.sex == 2 ? 'Female' : 'Male'", "as": "gender" }, { "type": "formula", "expr": "datum.sex == 2 ? -datum.people : datum.people", "as": "signed_people" } ] }, { "name": "data_0", "source": "source_0", "transform": [ { "type": "aggregate", "groupby": ["age", "gender"], "ops": ["sum"], "fields": ["signed_people"], "as": ["sum_signed_people"] }, { "type": "stack", "groupby": ["age"], "field": "sum_signed_people", "sort": {"field": ["gender"], "order": ["ascending"]}, "as": ["sum_signed_people_start", "sum_signed_people_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"sum_signed_people\"]) && isFinite(+datum[\"sum_signed_people\"])" } ] } ], "marks": [ { "name": "marks", "type": "rect", "style": ["bar"], "from": {"data": "data_0"}, "encode": { "update": { "fill": {"scale": "color", "field": "gender"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"population: \" + (format(datum[\"sum_signed_people\"], \"s\")) + \"; age: \" + (isValid(datum[\"age\"]) ? datum[\"age\"] : \"\"+datum[\"age\"]) + \"; gender: \" + (isValid(datum[\"gender\"]) ? datum[\"gender\"] : \"\"+datum[\"gender\"])" }, "x": {"scale": "x", "field": "sum_signed_people_end"}, "x2": {"scale": "x", "field": "sum_signed_people_start"}, "y": {"scale": "y", "field": "age"}, "height": {"scale": "y", "band": 1} } } } ], "scales": [ { "name": "x", "type": "linear", "domain": { "data": "data_0", "fields": ["sum_signed_people_start", "sum_signed_people_end"] }, "range": [0, {"signal": "width"}], "nice": true, "zero": true }, { "name": "y", "type": "band", "domain": { "data": "source_0", "field": "age", "sort": {"order": "descending"} }, "range": [0, {"signal": "height"}], "paddingInner": 0.1, "paddingOuter": 0.05 }, { "name": "color", "type": "ordinal", "domain": {"data": "data_0", "field": "gender", "sort": true}, "range": ["#675193", "#ca8861"] } ], "axes": [ { "scale": "x", "orient": "bottom", "grid": false, "title": "population", "format": "s", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/40)"}, "zindex": 0 } ], "legends": [ { "orient": "top", "fill": "color", "direction": "horizontal", "symbolType": "square" } ], "config": {"axis": {"grid": false}, "style": {"cell": {"stroke": null}}} }