Public
Edited
Feb 21, 2023
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
counties = FileAttachment("County2010washWGS84.json").json()
Insert cell
county_features = topojson.feature(counties, counties.objects.County2010washWGS84)
Insert cell
csv_data = d3.csvParse(await FileAttachment("County2010washWGS84.dbf.csv").text(),({COUNTYFP10, POPWHITE, POP10}) => [COUNTYFP10, +POPWHITE/+POP10])
Insert cell
csv_data_objects = Object.assign((d3.csvParse(await FileAttachment("County2010washWGS84.dbf.csv").text(), d3.autoType)).map(({COUNTYFP10, POPWHITE, POP10}) => ({FIPS: +COUNTYFP10, pctwhite: +POPWHITE/+POP10})))
Insert cell
viewof bins = Inputs.range([0, 100], {step: 1, label: "Bins"})
Insert cell
Plot.plot({
marks: [
Plot.rectY(csv_data_objects, Plot.binX({y: "count"}, {x: "pctwhite", thresholds: bins})),
Plot.ruleY([0])
]
})
Insert cell
whitepct = Array.from(csv_data.values(), d => d[1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Percent Population of Washington That is White"]})
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
d3.min(whitepct)
Insert cell
d3.max(whitepct)
Insert cell
unclassed = d3.scaleLinear()
.domain(d3.extent(whitepct))
.range(["#edf8b1", "#2c7fb8"])
Insert cell
chart(numericSort(whitepct), unclassed)
Insert cell
md`# Quantile Classification`
Insert cell
quantile = d3.scaleQuantile()
.domain(whitepct)
.range(["#fde0dd", "#fa9fb5", "#c51b8a"])
Insert cell
chart(numericSort(whitepct), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
naturalbreaks = simple.ckmeans(whitepct, 3).map(v => v.pop())
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["#e7e1ef", "#c994c7", "#dd1c77"])
Insert cell
chart(numericSort(whitepct), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
d3.extent(whitepct)
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(whitepct),d3.max(whitepct)])
.range(["#f6eff7","#bdc9e1","#67a9cf", "#02818a"])
Insert cell
chart(numericSort(whitepct), quantize)
Insert cell
md`# Threshold`
Insert cell
d3.min(whitepct)
Insert cell
d3.max(whitepct)
Insert cell
threshold = d3.scaleThreshold()
.domain([0.7, 0.8])
.range(["#fee8c8", "#e34a33"])
Insert cell
chart(numericSort(whitepct), threshold)
Insert cell
showScaleGrouping(whitepct, {
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