Skip to content

Instantly share code, notes, and snippets.

@PBI-DataVizzle
Created December 22, 2024 16:59
Show Gist options
  • Save PBI-DataVizzle/056a4aff1897afcd23ea42bd7294aeb3 to your computer and use it in GitHub Desktop.
Save PBI-DataVizzle/056a4aff1897afcd23ea42bd7294aeb3 to your computer and use it in GitHub Desktop.
uk_elections
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{
"party": "LAB",
"seats": 412,
"change": 211,
"share": 0.63,
"img_url": "https://i.ibb.co/93h9jqD/labour.png"
},
{
"party": "CON",
"seats": 121,
"change": -251,
"share": 0.18,
"img_url": "https://i.ibb.co/C2cFKw5/conservative.png"
},
{
"party": "LD",
"seats": 72,
"change": 64,
"share": 0.11,
"img_url": "https://i.ibb.co/4ZNCptd/libdem.png"
},
{
"party": "SNP",
"seats": 9,
"change": -39,
"share": 0.01,
"img_url": "https://i.ibb.co/jW7wLHr/snp.png"
},
{
"party": "SF",
"seats": 7,
"change": 0,
"share": 0.01,
"img_url": "https://i.ibb.co/kD2mG8H/sinnfein.png"
},
{
"party": "OTH",
"seats": 29,
"change": 15,
"share": 0.04,
"img_url": "https://i.ibb.co/ypTDC4Y/ind.png"
}
]
},
"transform": [
{
"calculate": "datum.change >= 0 ? '▲ +' + datum.change : '▼' + datum.change",
"as": "change_text"
}
],
"spacing": 15,
"hconcat": [
{
"title": {"text": ""},
"height": 350,
"layer": [
{
"mark": {"type": "image", "width": 50, "height": 50},
"encoding": {
"y": {
"field": "party",
"type": "nominal",
"sort": {"field": "seats", "op": "sum", "order": "descending"},
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false
}
},
"url": {"field": "img_url", "type": "nominal"}
}
}
]
},
{
"title": {
"text": "Party",
"fontSize": 25,
"align": "left",
"anchor": "start",
"offset": 10
},
"height": 350,
"layer": [
{
"mark": {"type": "text", "fontSize": 20, "align": "left"},
"encoding": {
"text": {"field": "party", "type": "nominal"},
"y": {
"field": "party",
"type": "nominal",
"sort": {"field": "seats", "op": "sum", "order": "descending"},
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false,
"ticks": false
}
}
}
}
]
},
{
"height": 350,
"layer": [
{"mark": {"type": "bar", "width": 50, "height": 50}},
{
"mark": {"type": "tick", "thickness": 2},
"encoding": {"color": {"value": "#000"}, "x": {"datum": 0}}
}
],
"encoding": {
"color": {
"legend": null,
"field": "party",
"type": "nominal",
"scale": {
"domain": ["LAB", "CON", "LD", "SNP", "SF", "OTH"],
"range": [
"rgb(234,29,12)",
"rgba(5,117,201,255)",
"rgb(251,177,20)",
"rgba(255,208,44,255)",
"rgba(36,170,131,255)",
"rgba(186,186,186,255)"
]
}
},
"x": {
"field": "seats",
"type": "quantitative",
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false,
"ticks": false
}
},
"y": {
"field": "party",
"type": "nominal",
"sort": {"field": "seats", "op": "sum", "order": "descending"},
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false,
"ticks": false
}
},
"url": {"field": "img_url", "type": "nominal"}
}
},
{
"title": {
"text": "Share",
"fontSize": 25,
"align": "right",
"dx": 0,
"anchor": "end",
"offset": 10
},
"height": 350,
"layer": [
{
"mark": {"type": "text", "fontSize": 25, "align": "center"},
"encoding": {
"text": {
"field": "share",
"type": "quantitative",
"format": "0.0%"
},
"y": {
"field": "party",
"type": "nominal",
"sort": {"field": "seats", "op": "sum", "order": "descending"},
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false,
"ticks": false
}
}
}
}
]
},
{
"title": {
"text": "Seats",
"fontSize": 25,
"align": "right",
"dx": -10,
"anchor": "end",
"offset": 10
},
"height": 350,
"layer": [
{
"mark": {
"type": "text",
"fontSize": 25,
"align": "center",
"xOffset": -15
},
"encoding": {
"text": {"field": "seats", "type": "quantitative"},
"y": {
"field": "party",
"type": "nominal",
"sort": {"field": "seats", "op": "sum", "order": "descending"},
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false,
"ticks": false
}
}
}
}
]
},
{
"title": {
"text": "Change",
"fontSize": 25,
"align": "right",
"dx": -15,
"anchor": "end",
"offset": 10
},
"height": 350,
"layer": [
{
"mark": {"type": "text", "fontSize": 25, "align": "right"},
"encoding": {
"text": {"field": "change_text", "type": "nominal"},
"y": {
"field": "party",
"type": "nominal",
"sort": {"field": "seats", "op": "sum", "order": "descending"},
"axis": {
"title": null,
"labels": false,
"domain": false,
"grid": false,
"ticks": false
}
}
}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment