{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A trellis bar chart showing the US population distribution of age groups and gender 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": "column_domain", "source": "source_0", "transform": [ { "type": "aggregate", "groupby": ["gender"], "fields": ["age"], "ops": ["distinct"], "as": ["distinct_age"] } ] }, { "name": "data_1", "source": "source_0", "transform": [ { "type": "aggregate", "groupby": ["age", "gender"], "ops": ["sum"], "fields": ["people"], "as": ["sum_people"] } ] }, { "name": "data_2", "source": "data_1", "transform": [ { "type": "filter", "expr": "isValid(datum[\"sum_people\"]) && isFinite(+datum[\"sum_people\"])" } ] } ], "signals": [ {"name": "child_x_step", "value": 17}, {"name": "child_height", "value": 200} ], "layout": { "padding": 20, "offset": {"columnTitle": 10}, "columns": {"signal": "length(data('column_domain'))"}, "bounds": "full", "align": "none" }, "marks": [ { "name": "column-title", "type": "group", "role": "column-title", "title": {"text": "gender", "style": "guide-title", "offset": 10} }, { "name": "row_header", "type": "group", "role": "row-header", "encode": {"update": {"height": {"signal": "child_height"}}}, "axes": [ { "scale": "y", "orient": "left", "grid": false, "title": "population", "labelOverlap": true, "tickCount": {"signal": "ceil(child_height/40)"}, "zindex": 0 } ] }, { "name": "column_header", "type": "group", "role": "column-header", "from": {"data": "column_domain"}, "sort": {"field": "datum[\"gender\"]", "order": "ascending"}, "title": { "text": { "signal": "isValid(parent[\"gender\"]) ? parent[\"gender\"] : \"\"+parent[\"gender\"]" }, "style": "guide-label", "frame": "group", "offset": 10 }, "encode": { "update": { "width": { "signal": "bandspace(datum[\"distinct_age\"], 1, 0.5) * child_x_step" } } } }, { "name": "cell", "type": "group", "style": "cell", "from": { "facet": { "name": "facet", "data": "source_0", "groupby": ["gender"], "aggregate": { "fields": ["age"], "ops": ["distinct"], "as": ["distinct_age"] } } }, "sort": {"field": ["datum[\"gender\"]"], "order": ["ascending"]}, "data": [ { "source": "facet", "name": "data_0", "transform": [ { "type": "aggregate", "groupby": ["age", "gender"], "ops": ["sum"], "fields": ["people"], "as": ["sum_people"] } ] }, { "name": "data_1", "source": "data_0", "transform": [ { "type": "filter", "expr": "isValid(datum[\"sum_people\"]) && isFinite(+datum[\"sum_people\"])" } ] } ], "encode": { "update": { "width": { "signal": "bandspace(datum[\"distinct_age\"], 1, 0.5) * child_x_step" }, "height": {"signal": "child_height"} } }, "marks": [ { "name": "child_layer_0_pathgroup", "type": "group", "from": { "facet": { "name": "faceted_path_child_layer_0_main", "data": "data_0", "groupby": ["gender"] } }, "encode": { "update": { "width": {"field": {"group": "width"}}, "height": {"field": {"group": "height"}} } }, "marks": [ { "name": "child_layer_0_marks", "type": "line", "style": ["line"], "sort": {"field": "datum[\"age\"]"}, "from": {"data": "faceted_path_child_layer_0_main"}, "encode": { "update": { "stroke": {"scale": "color", "field": "gender"}, "description": { "signal": "\"age: \" + (isValid(datum[\"age\"]) ? datum[\"age\"] : \"\"+datum[\"age\"]) + \"; population: \" + (format(datum[\"sum_people\"], \"\")) + \"; gender: \" + (isValid(datum[\"gender\"]) ? datum[\"gender\"] : \"\"+datum[\"gender\"])" }, "x": {"scale": "child_x", "field": "age"}, "y": {"scale": "y", "field": "sum_people"}, "defined": { "signal": "isValid(datum[\"sum_people\"]) && isFinite(+datum[\"sum_people\"])" } } } } ] }, { "name": "child_layer_1_marks", "type": "symbol", "style": ["point"], "from": {"data": "data_1"}, "encode": { "update": { "fill": {"value": "transparent"}, "stroke": {"scale": "color", "field": "gender"}, "ariaRoleDescription": {"value": "point"}, "description": { "signal": "\"age: \" + (isValid(datum[\"age\"]) ? datum[\"age\"] : \"\"+datum[\"age\"]) + \"; population: \" + (format(datum[\"sum_people\"], \"\")) + \"; gender: \" + (isValid(datum[\"gender\"]) ? datum[\"gender\"] : \"\"+datum[\"gender\"])" }, "x": {"scale": "child_x", "field": "age"}, "y": {"scale": "y", "field": "sum_people"} } } } ], "scales": [ { "name": "child_x", "type": "point", "domain": { "fields": [ {"data": "data_0", "field": "age"}, {"data": "data_1", "field": "age"} ], "sort": true }, "range": {"step": {"signal": "child_x_step"}}, "padding": 0.5 } ], "axes": [ { "scale": "y", "orient": "left", "gridScale": "child_x", "grid": true, "tickCount": {"signal": "ceil(child_height/40)"}, "domain": false, "labels": false, "aria": false, "maxExtent": 0, "minExtent": 0, "ticks": false, "zindex": 0 }, { "scale": "child_x", "orient": "bottom", "grid": false, "title": "age", "labelAlign": "right", "labelAngle": 270, "labelBaseline": "middle", "zindex": 0 } ] } ], "scales": [ { "name": "y", "type": "linear", "domain": { "fields": [ {"data": "data_1", "field": "sum_people"}, {"data": "data_2", "field": "sum_people"} ] }, "range": [{"signal": "child_height"}, 0], "nice": true, "zero": true }, { "name": "color", "type": "ordinal", "domain": { "fields": [ {"data": "data_1", "field": "gender"}, {"data": "data_2", "field": "gender"} ], "sort": true }, "range": ["#675193", "#ca8861"] } ], "legends": [ { "stroke": "color", "symbolType": "circle", "title": "gender", "encode": {"symbols": {"update": {"fill": {"value": "transparent"}}}} } ], "config": {"style": {"cell": {"fill": "yellow"}}} }