Skip to content

Instantly share code, notes, and snippets.

@Finterly
Last active April 29, 2020 21:21
Show Gist options
  • Save Finterly/f4a3c63264d0aff42f02bc055f3d634b to your computer and use it in GitHub Desktop.
Save Finterly/f4a3c63264d0aff42f02bc055f3d634b to your computer and use it in GitHub Desktop.
UShatecrime Cartogram
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A Dorling cartogram depicting U.S. state hate crime rates.",
"width": 900,
"height": 520,
"autosize": "none",
"config": {
"legend": {
"gradientDirection": "horizontal",
"gradientLength": 120,
"gradientThickness": 10
}
},
"data": [
{
"name": "states",
"url": "data/us-10m.json",
"format": {
"type": "topojson",
"feature": "states"
}
},
{
"name": "2018_hatecrime",
"url": "https://raw.githubusercontent.com/Finterly/Datasets_to_play_with/master/trial3",
"transform": [
{
"type": "lookup",
"from": "states",
"key": "id",
"fields": [
"id"
],
"as": [
"geo"
]
},
{
"type": "filter",
"expr": "datum.geo"
},
{
"type": "formula",
"as": "centroid",
"expr": "geoCentroid('projection', datum.geo)"
}
]
}
],
"projections": [
{
"name": "projection",
"type": "albersUsa",
"scale": 1100,
"translate": [
{
"signal": "width / 2"
},
{
"signal": "height / 2"
}
]
}
],
"scales": [
{
"name": "size",
"domain": {
"data": "2018_hatecrime",
"field": "n"
},
"zero": false,
"range": [
400,
5000
]
},
{
"name": "color",
"type": "linear",
"nice": true,
"domain": {
"data": "2018_hatecrime",
"field": "dense"
},
"range": "ramp"
}
],
"legends": [
{
"title": "Density of hate crime",
"orient": "bottom-right",
"type": "symbol",
"size": "size",
"fill": "color",
"clipHeight": 16
}
],
"marks": [
{
"name": "circles",
"type": "symbol",
"from": {
"data": "2018_hatecrime"
},
"encode": {
"enter": {
"size": {
"scale": "size",
"field": "n"
},
"fill": {
"scale": "color",
"field": "dense"
},
"stroke": {
"value": "white"
},
"strokeWidth": {
"value": 1.5
},
"x": {
"field": "centroid[0]"
},
"y": {
"field": "centroid[1]"
},
"tooltip": {
"signal": "'Hate crime: ' + datum.n"
}
}
},
"transform": [
{
"type": "force",
"static": true,
"forces": [
{
"force": "collide",
"radius": {
"expr": "1 + sqrt(datum.size) / 2"
}
},
{
"force": "x",
"x": "datum.centroid[0]"
},
{
"force": "y",
"y": "datum.centroid[1]"
}
]
}
]
},
{
"type": "text",
"interactive": false,
"from": {
"data": "circles"
},
"encode": {
"enter": {
"align": {
"value": "center"
},
"baseline": {
"value": "middle"
},
"fontSize": {
"value": 13
},
"fontWeight": {
"value": "bold"
},
"text": {
"field": "datum.state"
}
},
"update": {
"x": {
"field": "x"
},
"y": {
"field": "y"
}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment