Public
Edited
Mar 29, 2024
Insert cell
md`# Second Exercise on Observable: Classification and Colors`
Insert cell
d3 = require("d3@5")
Insert cell
topojson = require("topojson-client@3")
Insert cell
schooldistricts = FileAttachment("Iowa_School_Districts_2023-2024.json").json()
Insert cell
schooldistricts_features = topojson.feature(schooldistricts, schooldistricts.objects.Iowa_School_Districts_2023-2024)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Iowa_School_Districts_2023-2024 (1).csv").text(),({SchoolDist, Pop2020, SqMiles}) => [SchoolDist, [+Pop2020/+SqMiles]])
Insert cell
csv_data_objects = Object.assign((d3.csvParse(await FileAttachment("Iowa_School_Districts_2023-2024 (1).csv").text(), d3.autoType)).map(({SchoolDist, Pop2020, SqMiles}) => ({SchoolDist: +SchoolDist, popdensity: +Pop2020/+SqMiles})))
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(csv_data_objects, Plot.binX({y: "count"}, {x: "popdensity", thresholds: bins})),
Plot.ruleY([0])
]
})
Insert cell
popdensity = Array.from(csv_data.values(), d => d[1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Iowa School Districts 2023-2024", "Population Density"]})
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
linear = d3.scaleLinear()
.domain(d3.extent(popdensity))
.range(["white", "red"])
Insert cell
chart(numericSort(popdensity), linear)
Insert cell
md`# Quantile Classification`
Insert cell
quantile = d3.scaleQuantile()
.domain(popdensity)
.range(["white", "#fb6a4a", "red"])
Insert cell
chart(numericSort(popdensity), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
naturalbreaks = simple.ckmeans(popdensity, 3).map(v => v.pop())
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["white", "#fb6a4a", "red"])
Insert cell
chart(numericSort(popdensity), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(popdensity),d3.max(popdensity)])
.range(["#fee0d2", "#fc9272", "#de2d26"])
Insert cell
chart(numericSort(popdensity), quantize)
Insert cell
md`# Threshold`
Insert cell
threshold = d3.scaleThreshold()
.domain([0.05, 0.1, 0.2])
.range(["white", "#fb6a4a", "red"])
Insert cell
chart(numericSort(popdensity), threshold)
Insert cell
showScaleGrouping(popdensity, {
scaleQuantile: quantile,
scaleThreshold: threshold,
scaleJenks: jenks,
scaleQuantize: quantize,
scaleQuantizeNice: quantize.copy().nice()
})
Insert cell
md`# Annex`
Insert cell
Insert cell
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
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