Published
Edited
Mar 1, 2021
Insert cell
md`# Gulick - Second Exercise on Observable: Classification and Colors`
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
topojson = require("topojson-client@3")
Insert cell
oregon = FileAttachment("Oregon_Census_Proj.json").json()
Insert cell
Insert cell
oregon_features = topojson.feature(oregon, oregon.objects.Oregon_Census_Proj)
Insert cell
Insert cell
csv_data = d3.csvParse(await FileAttachment("Oregon_Census_Proj.csv").text(),({STFID, TABLACK, TAPERSONS}) => [STFID, [STFID, +TABLACK/+TAPERSONS]])
Insert cell
stfid = Array.from(csv_data.values(), d => d[0])
Insert cell
pctblack = Array.from(csv_data.values(), d => d[1][1])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Percentage Black"})
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
Insert cell
linear = d3.scaleLinear()
.domain([d3.min(pctblack),d3.max(pctblack)]) // can use d3.extent() for .domain as well
.range(["#efedf5", "#756bb1"]) // get colors from ColorBrewer(website) in HEX format
Insert cell
chart(numericSort(pctblack), linear)
Insert cell
md`# Quantile Classification`
Insert cell
Insert cell
Insert cell
Insert cell
quantile = d3.scaleQuantile()
.domain(pctblack)
.range(["#f2f0f7", "#cbc9e2", "#9e9ac8", "#6a51a3"])
Insert cell
chart(numericSort(pctblack), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
Insert cell
naturalbreaks = simple.ckmeans(pctblack, 4).map(v => v.pop())//naturalbreaks will decide where the breaks in your data should be depending on how many classes you want
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["#f2f0f7", "#cbc9e2", "#9e9ac8", "#6a51a3"])
Insert cell
chart(numericSort(pctblack), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(pctblack),d3.max(pctblack)])
.range(["#f2f0f7", "#cbc9e2", "#9e9ac8", "#6a51a3"])
Insert cell
chart(numericSort(pctblack), quantize)
Insert cell
md`# Threshold`
Insert cell
Insert cell
threshold = d3.scaleThreshold()
.domain([0.05, 0.2, 0.38])
.range(["#f2f0f7", "#cbc9e2", "#9e9ac8", "#6a51a3"])
Insert cell
chart(numericSort(pctblack), threshold)
Insert cell
showScaleGrouping(pctblack, {
Quantile: quantile,
Threshold: threshold,
Jenks: jenks,
Quantize: quantize,
QuantizeNice: quantize.copy().nice()
})
Insert cell
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