Public
Edited
Feb 20
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
cntydisbl = FileAttachment("lab1project.json").json()
Insert cell
county_features = topojson.feature(cntydisbl, cntydisbl.objects.lab1project)
Insert cell
csv_data = d3.csvParse(await FileAttachment("lab1project.csv").text(),({PID, tot_pop, tot_dis}) => [PID, [100*+tot_dis/tot_pop]])
Insert cell
csv_data_objects = Object.assign((d3.csvParse(await FileAttachment("lab1project.csv").text(), d3.autoType)).map(({PID, tot_dis, tot_pop}) => ({PID: +PID, pctdisabl: +tot_dis/+tot_pop})))
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: "pctdisabl", thresholds: bins})),
Plot.ruleY([0])
]
})
Insert cell
pctdisabl = Array.from(csv_data.values(), d => d[1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Percent Population Disabled by County"]})
Insert cell
md`# Unclassed Linear Scale`
Insert cell
linear = d3.scaleLinear()
.domain(d3.extent(pctdisabl))
.range(["#f0f0f0", "#de2d26", ])
Insert cell
chart(numericSort(pctdisabl), linear)
Insert cell
md`# Classification #1: Quantile`
Insert cell
quantile = d3.scaleQuantile()
.domain(pctdisabl)
.range(["#fee5d9", "#fb6a4a", "#a50f15"])
Insert cell
chart(numericSort(pctdisabl), quantile)
Insert cell
md`# Classification #2: Natural Breaks`
Insert cell
naturalbreaks = simple.ckmeans(pctdisabl, 3).map(v => v.pop())
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["#eff3ff", "#6baed6", "#08519c"])
Insert cell
chart(numericSort(pctdisabl), jenks)
Insert cell
md`# Classification #3: Equal Interval`
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(pctdisabl),d3.max(pctdisabl)])
.range(["#edf8e9", "#74c476", "#006d2c"])
Insert cell
chart(numericSort(pctdisabl), quantize)
Insert cell
md`# Classification #4: Manual Threshold`
Insert cell
threshold = d3.scaleThreshold()
.domain([25, 50])
.range(["#feedde", "#fd8d3c", "#a63603"])
Insert cell
chart(numericSort(pctdisabl), threshold)
Insert cell
md`# Data Comparison`
Insert cell
showScaleGrouping(pctdisabl, {
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