Skip to content

Instantly share code, notes, and snippets.

@keckelt
Created May 19, 2021 16:53
Show Gist options
  • Save keckelt/683de233f932398acb40ef590cbbae88 to your computer and use it in GitHub Desktop.
Save keckelt/683de233f932398acb40ef590cbbae88 to your computer and use it in GitHub Desktop.
Histogram Sort Criteria
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": { "values": [
{"Cylinders": 3, "Origin": "Europe"},
{"Cylinders": 3, "Origin": "Europe"},
{"Cylinders": 3, "Origin": "Europe"},
{"Cylinders": 4, "Origin": "Europe"},
{"Cylinders": 4, "Origin": "Europe"},
{"Cylinders": 5, "Origin": "Europe"},
{"Cylinders": 6, "Origin": "Europe"},
{"Cylinders": 6, "Origin": "Europe"},
{"Cylinders": 3, "Origin": "USA"},
{"Cylinders": 3, "Origin": "USA"},
{"Cylinders": 3, "Origin": "USA"},
{"Cylinders": 3, "Origin": "USA"},
{"Cylinders": 4, "Origin": "USA"},
{"Cylinders": 5, "Origin": "USA"},
{"Cylinders": 5, "Origin": "USA"},
{"Cylinders": 6, "Origin": "USA"},
{"Cylinders": 6, "Origin": "USA"},
{"Cylinders": 6, "Origin": "USA"},
{"Cylinders": 3, "Origin": "Japan"},
{"Cylinders": 3, "Origin": "Japan"},
{"Cylinders": 4, "Origin": "Japan"},
{"Cylinders": 5, "Origin": "Japan"},
{"Cylinders": 4, "Origin": "Japan"}
]},
"transform": [{
"aggregate": [{"op": "count", "as": "Count"}],
"groupby": ["Cylinders", "Origin"]
}, {
"impute": "Count",
"value": 0,
"key": "Origin",
"groupby": ["Cylinders"]
}, {
"joinaggregate": [{"op": "sum", "field": "Count", "as": "OriginCount"}],
"groupby": ["Origin"]
},{
"joinaggregate": [{"op": "sum", "field": "Count", "as": "CategoryItemSum"}],
"groupby": ["Cylinders"]
}, {
"calculate": "datum.Count/datum.OriginCount", "as": "PercentOfOrigin"
}, {
"joinaggregate": [{"op": "sum", "field": "PercentOfOrigin", "as": "CategoryPercentSum"}],
"groupby": ["Cylinders"]
}, {
"joinaggregate": [{"op": "max", "field": "PercentOfOrigin", "as": "maxPercent"}],
"groupby": ["Cylinders"]
}, {
"joinaggregate": [{"op": "average", "field": "PercentOfOrigin", "as": "avgPercent"}],
"groupby": ["Cylinders"]
}, {
"calculate": "pow(datum.avgPercent-datum.PercentOfOrigin,2)", "as": "squaredError"
}, {
"joinaggregate": [{"op": "average", "field": "squaredError", "as": "meanSquaredError"}],
"groupby": ["Cylinders"]
}
],
"mark": {"type": "bar", "tooltip": true},
"encoding": {
"y": {
"field": "Origin",
"type": "nominal",
"axis": {
"title": null,
"labels": false,
"ticks": false
}
},
"x": {
"field": "PercentOfOrigin",
"type": "quantitative",
"axis": {
"title": "Relative Frequency (per Origin)",
"format": ".1~%"
}
},
"row": {
"field": "Cylinders",
"type": "nominal",
"spacing": -2,
"sort": {
"field": "meanSquaredError",
"order": "descending"
}
},
"color": {
"field": "Origin",
"type": "nominal"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment