{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A population pyramid for the US in 2000.", "background": "white", "padding": 5, "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" } ] }, { "name": "data_1", "source": "source_0", "transform": [ {"type": "filter", "expr": "datum[\"gender\"]===\"Female\""} ] }, { "name": "data_2", "source": "data_1", "transform": [ { "type": "aggregate", "groupby": ["age", "gender"], "ops": ["sum"], "fields": ["people"], "as": ["sum_people"] }, { "type": "stack", "groupby": ["age"], "field": "sum_people", "sort": {"field": ["gender"], "order": ["ascending"]}, "as": ["sum_people_start", "sum_people_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"sum_people\"]) && isFinite(+datum[\"sum_people\"])" } ] }, { "name": "data_3", "source": "source_0", "transform": [{"type": "filter", "expr": "datum[\"gender\"]===\"Male\""}] }, { "name": "data_4", "source": "data_3", "transform": [ { "type": "aggregate", "groupby": ["age", "gender"], "ops": ["sum"], "fields": ["people"], "as": ["sum_people"] }, { "type": "stack", "groupby": ["age"], "field": "sum_people", "sort": {"field": ["gender"], "order": ["ascending"]}, "as": ["sum_people_start", "sum_people_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"sum_people\"]) && isFinite(+datum[\"sum_people\"])" } ] } ], "signals": [ {"name": "concat_0_width", "value": 200}, {"name": "concat_0_y_step", "value": 20}, { "name": "concat_0_height", "update": "bandspace(domain('concat_0_y').length, 0.1, 0.05) * concat_0_y_step" }, {"name": "concat_1_width", "value": 20}, {"name": "concat_1_y_step", "value": 20}, { "name": "concat_1_height", "update": "bandspace(domain('concat_1_y').length, 1, 0.5) * concat_1_y_step" }, {"name": "concat_2_width", "value": 200}, {"name": "concat_2_y_step", "value": 20}, { "name": "concat_2_height", "update": "bandspace(domain('concat_2_y').length, 0.1, 0.05) * concat_2_y_step" } ], "layout": {"padding": 0, "bounds": "full", "align": "each"}, "marks": [ { "type": "group", "name": "concat_0_group", "title": {"text": "Female", "frame": "group"}, "style": "cell", "encode": { "update": { "width": {"signal": "concat_0_width"}, "height": {"signal": "concat_0_height"} } }, "marks": [ { "name": "concat_0_marks", "type": "rect", "style": ["bar"], "from": {"data": "data_2"}, "encode": { "update": { "fill": {"scale": "color", "field": "gender"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"population: \" + (format(datum[\"sum_people\"], \"s\")) + \"; age: \" + (isValid(datum[\"age\"]) ? datum[\"age\"] : \"\"+datum[\"age\"]) + \"; gender: \" + (isValid(datum[\"gender\"]) ? datum[\"gender\"] : \"\"+datum[\"gender\"])" }, "x": {"scale": "concat_0_x", "field": "sum_people_end"}, "x2": {"scale": "concat_0_x", "field": "sum_people_start"}, "y": {"scale": "concat_0_y", "field": "age"}, "height": {"scale": "concat_0_y", "band": 1} } } } ], "axes": [ { "scale": "concat_0_x", "orient": "bottom", "grid": false, "title": "population", "format": "s", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(concat_0_width/40)"}, "zindex": 0 } ] }, { "type": "group", "name": "concat_1_group", "style": "cell", "encode": { "update": { "width": {"signal": "concat_1_width"}, "height": {"signal": "concat_1_height"}, "stroke": {"value": null} } }, "marks": [ { "name": "concat_1_marks", "type": "text", "style": ["text"], "from": {"data": "source_0"}, "encode": { "update": { "align": {"value": "center"}, "fill": {"value": "black"}, "description": { "signal": "\"age: \" + (isValid(datum[\"age\"]) ? datum[\"age\"] : \"\"+datum[\"age\"])" }, "x": {"signal": "concat_1_width", "mult": 0.5}, "y": {"scale": "concat_1_y", "field": "age"}, "text": {"signal": "format(datum[\"age\"], \"\")"}, "baseline": {"value": "middle"} } } } ] }, { "type": "group", "name": "concat_2_group", "title": {"text": "Male", "frame": "group"}, "style": "cell", "encode": { "update": { "width": {"signal": "concat_2_width"}, "height": {"signal": "concat_2_height"} } }, "marks": [ { "name": "concat_2_marks", "type": "rect", "style": ["bar"], "from": {"data": "data_4"}, "encode": { "update": { "fill": {"scale": "color", "field": "gender"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"population: \" + (format(datum[\"sum_people\"], \"s\")) + \"; age: \" + (isValid(datum[\"age\"]) ? datum[\"age\"] : \"\"+datum[\"age\"]) + \"; gender: \" + (isValid(datum[\"gender\"]) ? datum[\"gender\"] : \"\"+datum[\"gender\"])" }, "x": {"scale": "concat_2_x", "field": "sum_people_end"}, "x2": {"scale": "concat_2_x", "field": "sum_people_start"}, "y": {"scale": "concat_2_y", "field": "age"}, "height": {"scale": "concat_2_y", "band": 1} } } } ], "axes": [ { "scale": "concat_2_x", "orient": "bottom", "grid": false, "title": "population", "format": "s", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(concat_2_width/40)"}, "zindex": 0 } ] } ], "scales": [ { "name": "color", "type": "ordinal", "domain": { "fields": [ {"data": "data_2", "field": "gender"}, {"data": "data_4", "field": "gender"} ], "sort": true }, "range": ["#675193", "#ca8861"] }, { "name": "concat_0_x", "type": "linear", "domain": { "data": "data_2", "fields": ["sum_people_start", "sum_people_end"] }, "range": [0, {"signal": "concat_0_width"}], "reverse": true, "nice": true, "zero": true }, { "name": "concat_0_y", "type": "band", "domain": { "data": "data_1", "field": "age", "sort": {"order": "descending"} }, "range": {"step": {"signal": "concat_0_y_step"}}, "paddingInner": 0.1, "paddingOuter": 0.05 }, { "name": "concat_1_y", "type": "point", "domain": { "data": "source_0", "field": "age", "sort": {"order": "descending"} }, "range": {"step": {"signal": "concat_1_y_step"}}, "padding": 0.5 }, { "name": "concat_2_x", "type": "linear", "domain": { "data": "data_4", "fields": ["sum_people_start", "sum_people_end"] }, "range": [0, {"signal": "concat_2_width"}], "nice": true, "zero": true }, { "name": "concat_2_y", "type": "band", "domain": { "data": "data_3", "field": "age", "sort": {"order": "descending"} }, "range": {"step": {"signal": "concat_2_y_step"}}, "paddingInner": 0.1, "paddingOuter": 0.05 } ], "config": {"axis": {"grid": false}, "style": {"cell": {"stroke": null}}} }