Published
Edited
Mar 2, 2021
Insert cell
md`# Second Exercise on Observable: Classification and Colors Will`
Insert cell
d3 = require("d3@5")
Insert cell
topojson = require("topojson-client@3")
Insert cell
NYPD = FileAttachment("NYCPolice.json").json()
Insert cell
county_features = topojson.feature(NYPD, NYPD.objects.NYCPolice)
Insert cell
csv_data = d3.csvParse(await FileAttachment("NYCPolice.csv").text(),({precinct, shape_area, shape_leng}) => [+precinct, [+shape_area/+shape_leng]])
Insert cell
shape_width = Array.from(csv_data.values(), d => d[1][0])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["precinct number", "Width"]})
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
linear = d3.scaleLinear()
.domain([d3.min(shape_width),d3.max(shape_width)])
.range(["#fee0d2", "#de2d26"])
Insert cell
chart(numericSort(shape_width), linear)
Insert cell
md`# Quantile Classification`
Insert cell
quantile = d3.scaleQuantile()
.domain(shape_width)
.range(["#fee5d9", "#fcae91", "#fb6a4a", "#cb181d"])
Insert cell
chart(numericSort(shape_width), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
naturalbreaks = simple.ckmeans(shape_width, 3).map(v => v.pop())
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["#fee5d9", "#fcae91", "#fb6a4a", "#cb181d"])
Insert cell
chart(numericSort(shape_width), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(shape_width),d3.max(shape_width)])
.range(["#fee5d9", "#fcae91", "#fb6a4a", "#cb181d"])
Insert cell
chart(numericSort(shape_width), quantize)
Insert cell
md`# Threshold`
Insert cell
threshold = d3.scaleThreshold()
.domain([0.05, 0.1, 0.50])
.range(["#fee5d9", "#fcae91", "#fb6a4a", "#cb181d"])
Insert cell
chart(numericSort(shape_width), threshold)
Insert cell
showScaleGrouping(shape_width, {
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