Skip to content

Instantly share code, notes, and snippets.

@bkamapantula
Last active June 16, 2020 05:41
Show Gist options
  • Save bkamapantula/30a39e134578c7b5bbd5e2f3786c90c6 to your computer and use it in GitHub Desktop.
Save bkamapantula/30a39e134578c7b5bbd5e2f3786c90c6 to your computer and use it in GitHub Desktop.
Heatmap spec for google mobility data
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "Mobility changes for different aspects in Indian regions",
"width": 750,
"height": 700,
"padding": 5,
"title": {
"text": "India regional mobility changes",
"anchor": "middle",
"fontSize": 16,
"font": "Monospace",
"offset": 4
},
"data": [
{
"name": "mobility",
"url": "https://gist.githubusercontent.com/bkamapantula/2b15fec42c4971ea02cc45167a4847ab/raw/b218004907f237a85b65cbee29555253e478e48f/google-mobility-data.json",
"format": {
"parse": "auto",
"type": "json"
},
"transform": [
{"type": "filter", "expr": "datum[Property] != null"}
]
}, {
"name": "annotation",
"values": [
{
"start": "2020-03-25",
"end": "2020-04-14",
"text": "Lockdown 1"
}, {
"start": "2020-04-15",
"end": "2020-05-03",
"text": "Lockdown 2"
}, {
"start": "2020-05-04",
"end": "2020-05-17",
"text": "Lockdown 3"
}, {
"start": "2020-05-18",
"end": "2020-05-31",
"text": "Lockdown 4"
}
],
"format": {
"parse": {"start": "date", "end": "date"}
}
}
],
"signals": [
{
"name": "Property",
"value": "residential_percent_change_from_baseline",
"bind": {
"input": "select",
"options": [
"retail_and_recreation_percent_change_from_baseline",
"grocery_and_pharmacy_percent_change_from_baseline",
"parks_percent_change_from_baseline",
"transit_stations_percent_change_from_baseline",
"workplaces_percent_change_from_baseline",
"residential_percent_change_from_baseline"
]
}
},
{
"name": "Color",
"value": "RedYellowGreen",
"bind": {
"input": "select",
"options": [
"BlueOrange",
"BrownBlueGreen",
"PurpleGreen",
"PinkYellowGreen",
"PurpleOrange",
"RedBlue",
"RedGrey",
"RedYellowBlue",
"RedYellowGreen"
]
}
},
{
"name": "colorReverse", "value": false, "bind": {"input": "checkbox"}
}
],
"scales": [
{
"name": "x",
"type": "time",
"domain": {"data": "mobility", "field": "date"},
"range": "width"
},
{
"name": "y",
"type": "band",
"domain": {"data": "mobility", "field": "sub_region_1"},
"range": "height"
},
{
"name": "color",
"type": "linear",
"range": {"scheme": {"signal": "Color"}},
"domain": {"data": "mobility", "field": {"signal": "Property"}},
"reverse": {"signal": "colorReverse"}
},
{
"name": "acolor",
"type": "ordinal",
"domain": {"data": "annotation", "field": "text"},
"range": ["black"]
}
],
"axes": [
{"orient": "bottom", "scale": "x", "domain": false, "title": "Day",
"labelFont": "monospace", "titleFont": "monospace"},
{"orient": "left", "scale": "y", "domain": false, "title": "Region",
"labelFont": "monospace", "titleFont": "monospace"}
],
"legends": [
{
"fill": "color",
"type": "gradient",
"title": {"signal": "Property"},
"titleFontSize": 12,
"titlePadding": 4,
"gradientLength": {"signal": "height - 16"},
"titleFont": "monospace",
"titleOrient": "right",
"orient": "left"
}
],
"marks": [
{
"type": "rect",
"from": {"data": "mobility"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "date"},
"y": {"scale": "y", "field": "sub_region_1"},
"width": {"value": 5},
"height": {"scale": "y", "band": 1},
"tooltip": {
"signal": "timeFormat(datum.date, '%b %d') + ',' + datum.sub_region_1 + ' - ' + Property + ': '+ datum[Property]"
}
},
"update": {
"fill": {
"scale": "color",
"field": {"signal": "Property"}
},
"tooltip": {
"signal": "timeFormat(datum.date, '%b %d') + ',' + datum.sub_region_1 + ' - ' + Property + ': '+ datum[Property]"
}
}
},
"zindex": 9
},
{
"type": "rect",
"from": {"data": "annotation"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "start"},
"x2": {"scale": "x", "field": "end"},
"y": {"value": 0},
"y2": {"signal": "height + 50"},
"fill": {"scale": "acolor", "field": "text"},
"opacity": {"value": 0.09}
}
}
},
{
"type": "text",
"from": {
"data": "annotation"
},
"encode": {
"enter": {
"x": {"scale": "x", "field": "start"},
"y": {"signal": "height + 60"},
"text": {"field": "text"}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment