Public
Edited
Feb 21
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
US = FileAttachment("dtl_cnty.json").json()
Insert cell
county_features = topojson.feature(US, US.objects.dtl_cnty)
Insert cell
csv_data = d3.csvParse(await FileAttachment("dtl_cnty.csv").text(),({FIPS, POPULATION, SQMI}) => [+FIPS, +POPULATION/+SQMI])
Insert cell
csv_data_objects = Object.assign((d3.csvParse(await FileAttachment("dtl_cnty.csv").text(), d3.autoType)).map(({FIPS, POPULATION, SQMI}) => ({FIPS: +FIPS, uscountypopdensity: +POPULATION/+SQMI})))
Insert cell
viewof bins = Inputs.range([0, 20], {step: 1, label: "Bins"})
Insert cell
Plot.plot({
marks: [
Plot.rectY(csv_data_objects, Plot.binX({y: "count"}, {x: "uscountypopdensity", thresholds: bins})),
Plot.ruleY([0])
]
})
Insert cell
uscountypopdensity = Array.from(csv_data.values(), d => d[1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["US County Population Density"]})
Insert cell
d3.mean(uscountypopdensity)
Insert cell
d3.median (uscountypopdensity)
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
Insert cell
chart(numericSort(uscountypopdensity), linear)
Insert cell
md`# Quantile Classification`
Insert cell
quantile = d3.scaleQuantile()
.domain(uscountypopdensity)
.range(["#f0f9e8", "#bae4bc", "#7bccc4", "#2b8cbe"])
Insert cell
chart(numericSort(uscountypopdensity), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
naturalbreaks = simple.ckmeans(uscountypopdensity, 5).map(v => v.pop())
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["#fef0d9", "#fdcc8a", "#fc8d59", "#e34a33", "#b30000"])
Insert cell
chart(numericSort(uscountypopdensity), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(uscountypopdensity),d3.max(uscountypopdensity)])
.range(["#f0f9e8", "#7bccc4", "#0868ac"])
Insert cell
chart(numericSort(uscountypopdensity), quantize)
Insert cell
md`# Threshold`
Insert cell
threshold = d3.scaleThreshold()
.domain([1, 20, 90, 500, 2000, 72588.3])
.range(["#fef0d9", "#fdd49e", "#fdbb84", "#fc8d59", "#e34a33", "#b30000"])
Insert cell
chart(numericSort(uscountypopdensity), threshold)
Insert cell
showScaleGrouping(uscountypopdensity, {
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

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