{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "Cars from Japan compared to all cars. Demonstrates independent legends in layers.", "background": "white", "padding": 5, "width": 200, "height": 200, "style": "cell", "data": [ { "name": "source_0", "url": "data/cars.json", "format": {"type": "json"}, "transform": [ { "type": "extent", "field": "Acceleration", "signal": "layer_1_bin_maxbins_10_Acceleration_extent" }, { "type": "bin", "field": "Acceleration", "as": [ "bin_maxbins_10_Acceleration", "bin_maxbins_10_Acceleration_end" ], "signal": "layer_1_bin_maxbins_10_Acceleration_bins", "extent": {"signal": "layer_1_bin_maxbins_10_Acceleration_extent"}, "maxbins": 10 }, { "type": "extent", "field": "Horsepower", "signal": "layer_1_bin_maxbins_10_Horsepower_extent" }, { "type": "bin", "field": "Horsepower", "as": ["bin_maxbins_10_Horsepower", "bin_maxbins_10_Horsepower_end"], "signal": "layer_1_bin_maxbins_10_Horsepower_bins", "extent": {"signal": "layer_1_bin_maxbins_10_Horsepower_extent"}, "maxbins": 10 } ] }, { "name": "data_0", "source": "source_0", "transform": [ {"type": "filter", "expr": "datum.Origin === 'Japan'"}, { "type": "aggregate", "groupby": [ "bin_maxbins_10_Acceleration", "bin_maxbins_10_Acceleration_end", "bin_maxbins_10_Horsepower", "bin_maxbins_10_Horsepower_end" ], "ops": ["count"], "fields": [null], "as": ["__count"] }, { "type": "filter", "expr": "isValid(datum[\"bin_maxbins_10_Acceleration\"]) && isFinite(+datum[\"bin_maxbins_10_Acceleration\"]) && isValid(datum[\"bin_maxbins_10_Horsepower\"]) && isFinite(+datum[\"bin_maxbins_10_Horsepower\"])" } ] }, { "name": "data_1", "source": "source_0", "transform": [ { "type": "aggregate", "groupby": [ "bin_maxbins_10_Acceleration", "bin_maxbins_10_Acceleration_end", "bin_maxbins_10_Horsepower", "bin_maxbins_10_Horsepower_end" ], "ops": ["count"], "fields": [null], "as": ["__count"] }, { "type": "filter", "expr": "isValid(datum[\"bin_maxbins_10_Acceleration\"]) && isFinite(+datum[\"bin_maxbins_10_Acceleration\"]) && isValid(datum[\"bin_maxbins_10_Horsepower\"]) && isFinite(+datum[\"bin_maxbins_10_Horsepower\"])" } ] } ], "marks": [ { "name": "layer_0_marks", "type": "symbol", "style": ["circle"], "from": {"data": "data_1"}, "encode": { "update": { "fill": {"value": "#4c78a8"}, "ariaRoleDescription": {"value": "circle"}, "description": { "signal": "\"Acceleration (binned): \" + (!isValid(datum[\"bin_maxbins_10_Acceleration\"]) || !isFinite(+datum[\"bin_maxbins_10_Acceleration\"]) ? \"null\" : format(datum[\"bin_maxbins_10_Acceleration\"], \"\") + \" – \" + format(datum[\"bin_maxbins_10_Acceleration_end\"], \"\")) + \"; Horsepower (binned): \" + (!isValid(datum[\"bin_maxbins_10_Horsepower\"]) || !isFinite(+datum[\"bin_maxbins_10_Horsepower\"]) ? \"null\" : format(datum[\"bin_maxbins_10_Horsepower\"], \"\") + \" – \" + format(datum[\"bin_maxbins_10_Horsepower_end\"], \"\")) + \"; All Cars Count: \" + (format(datum[\"__count\"], \"\"))" }, "x": { "signal": "scale(\"x\", 0.5 * datum[\"bin_maxbins_10_Acceleration\"] + 0.5 * datum[\"bin_maxbins_10_Acceleration_end\"])" }, "y": { "signal": "scale(\"y\", 0.5 * datum[\"bin_maxbins_10_Horsepower\"] + 0.5 * datum[\"bin_maxbins_10_Horsepower_end\"])" }, "size": {"scale": "size", "field": "__count"}, "shape": {"value": "circle"} } } }, { "name": "layer_1_marks", "type": "symbol", "style": ["circle"], "from": {"data": "data_0"}, "encode": { "update": { "fill": {"value": "firebrick"}, "ariaRoleDescription": {"value": "circle"}, "description": { "signal": "\"Acceleration (binned): \" + (!isValid(datum[\"bin_maxbins_10_Acceleration\"]) || !isFinite(+datum[\"bin_maxbins_10_Acceleration\"]) ? \"null\" : format(datum[\"bin_maxbins_10_Acceleration\"], \"\") + \" – \" + format(datum[\"bin_maxbins_10_Acceleration_end\"], \"\")) + \"; Horsepower (binned): \" + (!isValid(datum[\"bin_maxbins_10_Horsepower\"]) || !isFinite(+datum[\"bin_maxbins_10_Horsepower\"]) ? \"null\" : format(datum[\"bin_maxbins_10_Horsepower\"], \"\") + \" – \" + format(datum[\"bin_maxbins_10_Horsepower_end\"], \"\")) + \"; Cars from Japan Count: \" + (format(datum[\"__count\"], \"\"))" }, "x": { "signal": "scale(\"x\", 0.5 * datum[\"bin_maxbins_10_Acceleration\"] + 0.5 * datum[\"bin_maxbins_10_Acceleration_end\"])" }, "y": { "signal": "scale(\"y\", 0.5 * datum[\"bin_maxbins_10_Horsepower\"] + 0.5 * datum[\"bin_maxbins_10_Horsepower_end\"])" }, "size": {"scale": "size", "field": "__count"}, "shape": {"value": "circle"} } } } ], "scales": [ { "name": "x", "type": "linear", "domain": { "signal": "[layer_1_bin_maxbins_10_Acceleration_bins.start, layer_1_bin_maxbins_10_Acceleration_bins.stop]" }, "range": [0, {"signal": "width"}], "bins": {"signal": "layer_1_bin_maxbins_10_Acceleration_bins"}, "zero": false }, { "name": "y", "type": "linear", "domain": { "signal": "[layer_1_bin_maxbins_10_Horsepower_bins.start, layer_1_bin_maxbins_10_Horsepower_bins.stop]" }, "range": [{"signal": "height"}, 0], "bins": {"signal": "layer_1_bin_maxbins_10_Horsepower_bins"}, "zero": false }, { "name": "size", "type": "linear", "domain": { "fields": [ {"data": "data_1", "field": "__count"}, {"data": "data_0", "field": "__count"} ] }, "range": [ 0, { "signal": "pow(0.95 * min(width / ((layer_1_bin_maxbins_10_Acceleration_bins.stop - layer_1_bin_maxbins_10_Acceleration_bins.start) / layer_1_bin_maxbins_10_Acceleration_bins.step), height / ((layer_1_bin_maxbins_10_Horsepower_bins.stop - layer_1_bin_maxbins_10_Horsepower_bins.start) / layer_1_bin_maxbins_10_Horsepower_bins.step)), 2)" } ], "zero": true } ], "axes": [ { "scale": "x", "orient": "bottom", "grid": false, "title": "Acceleration (binned)", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/10)"}, "zindex": 0 }, { "scale": "y", "orient": "left", "grid": false, "title": "Horsepower (binned)", "labelOverlap": true, "tickCount": {"signal": "ceil(height/10)"}, "zindex": 0 } ], "legends": [ { "title": "All Cars Count", "size": "size", "symbolType": "circle", "encode": { "symbols": { "update": { "fill": {"value": "#4c78a8"}, "stroke": {"value": "transparent"} } } } }, { "title": "Cars from Japan Count", "size": "size", "symbolType": "circle", "encode": { "symbols": { "update": { "fill": {"value": "firebrick"}, "stroke": {"value": "transparent"} } } } } ] }