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
states = FileAttachment("US_county_2022_cont_WGS84.json").json()
Insert cell
states_features = topojson.feature(states, states.objects.US_county_2022_cont_WGS84)
Insert cell
csv_data = d3.csvParse(await FileAttachment("US_county_2022_cont_WGS84.csv").text(),({NAME, ALAND, AWATER}) => [NAME, AWATER/(AWATER+ALAND)])
Insert cell
csv_data_objects = Object.assign((d3.csvParse(await FileAttachment("US_county_2022_cont_WGS84.csv").text(), d3.autoType)).map(({NAME, ALAND, AWATER}) => ({NAME:NAME, Watercoverpct: AWATER/(AWATER+ALAND)})))
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: "Watercoverpct", thresholds: bins})),
Plot.ruleY([0])
]
})
Insert cell
Watercoverpct = Array.from(csv_data.values(), d => d[1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Percenetage of water area to total area by county"]})
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
linear = d3.scaleLinear()
.domain(d3.extent(Watercoverpct))
.range(["white", "green"])
Insert cell
chart(numericSort(Watercoverpct), linear)
Insert cell
md`# Quantile Classification`
Insert cell
quantile = d3.scaleQuantile()
.domain(Watercoverpct)
.range(["white", "#9ecae1", "blue"])
Insert cell
chart(numericSort(Watercoverpct), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
naturalbreaks = simple.ckmeans(Watercoverpct, 5).map(v => v.pop())
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["white", "#fdae6b", "#e6550d"])
Insert cell
chart(numericSort(Watercoverpct), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
quantize = d3.scaleQuantize()
.domain(d3.extent(Watercoverpct))
.range(["White", "#c51b8a"])
Insert cell
chart(numericSort(Watercoverpct), quantize)
Insert cell
md`# Threshold`
Insert cell
threshold = d3.scaleThreshold()
.domain([0.05, 0.1, 0.2])
.range(["white", "#99d594", "green"])
Insert cell
chart(numericSort(Watercoverpct), threshold)
Insert cell
showScaleGrouping(Watercoverpct, {
scaleQuantile: quantile,
scaleThreshold: threshold,
scaleJenks: jenks,
scaleQuantize: quantize,
scaleQuantizeNice: quantize.copy().nice()
})
Insert cell
md`# Annex`
Insert cell
Insert cell
function chart(data, scale) {
const w = 7.5,
cols = Math.floor(Math.min(1200, width) / w),
lines = Math.ceil (3500 / cols);
const chart = d3
.create("svg")
.attr("width", cols * w)
.attr("height", lines * w);

chart
.append("g")
.attr("transform", "translate(2,2)")
.attr("style", "stroke:black; fill:white;")
.selectAll("rect")
.data(data)
.join("rect")
.attr("width", w - 3)
.attr("height", w - 3)
.attr("x", (_, i) => w * (i % cols))
.attr("y", (_, i) => w * ((i / cols) | 0))
.style("fill", d => (scale ? scale(d) : "#ddd"));
return chart.node();
}
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