Published
Edited
Apr 4, 2018
1 fork
4 stars
Insert cell
Insert cell
vegalite({
data: {values: cars},
"width": 700,
"height": 300,
"mark": "bar",
"config": {
"axis": {
"titleFontSize": 16
},
"legend": {
}
},
// adds new column to define order - maybe there is a better way?. Change order here to change order of bars
// some other sorting options, if you want to sort by metric (count, median, etc): // https://vega.github.io/vega-lite/docs/sort.html
"transform": [{
"calculate": "if(datum.Origin === 'Japan', 0, if(datum.Origin === 'USA', 1, 2))",
"as": "OriginOrder"
}],
"encoding": {
"x": {
"bin": {"maxbins":20},
"field": "Miles_per_Gallon",
"axis": {
"title": "MPG (binned)"
},
"type": "quantitative",
},
"y": {
"aggregate": "count",
"type": "quantitative"
},
// order bars based on newly created OriginOrder field
"order": {
"field": "OriginOrder",
"type": "quantitative",
},
"color": {
"field": "Origin",
"type": "nominal",
"scale": {
"domain": ["USA","Japan","Europe"],
"range": ["#aaa", "steelblue", "#ccc"]
},
"legend": {"title": "Car Origin"}
}
}
})
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more