Public
Edited
Jun 22, 2023
Fork of D3 world map
Insert cell
md`# D3 choropleth map - life expectancy`
Insert cell
Insert cell
borders = topojson.mesh(world, world.objects.countries, (a, b) => a !== b)
Insert cell
world = FileAttachment("countries-50m.json").json()
Insert cell
Insert cell
Insert cell
countries = topojson.feature(world, world.objects.countries)
Insert cell
key = Legend(chart.scales.color, {title: "Health life expectancy (years) - data source: WHO"})
Insert cell
chart = Choropleth(hale, {
id: d => d.name, // country name, e.g. Zimbabwe
value: d => d.hale, // health-adjusted life expectancy
range: d3.schemeBlues[0],
features: countries,
featureId: d => d.properties.name, // i.e., not ISO 3166-1 numeric
borders: borders,
projection: d3proj.geoGilbert(),
width
})
Insert cell
hale = (await FileAttachment("hale.csv").csv()).map(d => ({name: rename.get(d.country) || d.country, hale: +d.hale}))
Insert cell
rename = new Map([
["Antigua and Barbuda", "Antigua and Barb."],
["Bolivia (Plurinational State of)", "Bolivia"],
["Bosnia and Herzegovina", "Bosnia and Herz."],
["Brunei Darussalam", "Brunei"],
["Central African Republic", "Central African Rep."],
["Cook Islands", "Cook Is."],
["Democratic People's Republic of Korea", "North Korea"],
["Democratic Republic of the Congo", "Dem. Rep. Congo"],
["Dominican Republic", "Dominican Rep."],
["Equatorial Guinea", "Eq. Guinea"],
["Iran (Islamic Republic of)", "Iran"],
["Lao People's Democratic Republic", "Laos"],
["Marshall Islands", "Marshall Is."],
["Micronesia (Federated States of)", "Micronesia"],
["Republic of Korea", "South Korea"],
["Republic of Moldova", "Moldova"],
["Russian Federation", "Russia"],
["Saint Kitts and Nevis", "St. Kitts and Nevis"],
["Saint Vincent and the Grenadines", "St. Vin. and Gren."],
["Sao Tome and Principe", "São Tomé and Principe"],
["Solomon Islands", "Solomon Is."],
["South Sudan", "S. Sudan"],
["Swaziland", "eSwatini"],
["Syrian Arab Republic", "Syria"],
["The former Yugoslav Republic of Macedonia", "Macedonia"],
// ["Tuvalu", ?],
["United Republic of Tanzania", "Tanzania"],
["Venezuela (Bolivarian Republic of)", "Venezuela"],
["Viet Nam", "Vietnam"]
])
Insert cell
import {Choropleth, Legend} from "@d3/choropleth"
Insert cell
d3proj = require("d3-geo-projection@4")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more