Published
Edited
Mar 1, 2021
Insert cell
md`# Second Exercise on Observable: Classification and Colors`
Insert cell
//import d3
Insert cell
d3 = require("d3@5")
Insert cell
// import topojson
Insert cell
topojson = require("topojson-client@3")
Insert cell
//import the topojson file ia_covid19_county.json
Insert cell
Covid = FileAttachment("ia_covid19.json").json()
Insert cell
//get the features from the topojson file
Insert cell
county_features = topojson.feature(Covid, Covid.objects.ia_covid19)
Insert cell
//pull the fields that will be used for the assignment out of the csv file
Insert cell
csv_data = d3.csvParse(await FileAttachment("ia_covid19_county.csv").text(),({FIPS, Confirmed, pop_est_20}) => [FIPS, [+Confirmed, +Confirmed/+pop_est_20]])
Insert cell
//collect the case confirmed percentage with a filter that pevents looking at data where is divided by zero
Insert cell
CaseConfirmedpct = Array.from(csv_data.values(), d => d[1][1]).filter(d => d <= 1)
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Percentage of Confirmed Covid Cases from total County Population"]})
Insert cell
md`# Linear Scale (Unclassed)`
Insert cell
//all of the colors used for this assignment came from colorbrewer.org. To select colors you go to the top and there is a pull down menu for the number of classes that you want to use, for this assignment I just left the number of classes at 3 but if you want to use more you use this menu to select the number of classes that you want. After you select the number of classes you choose which hue you would like to use for your map. Below that is an export section. There is a drop down menu for if you want to export HEX, RGB, or CMYK. For this assignment I used HEX. To export you copy the value next to each color that you want to where ever you want to use it.
Insert cell
//the code below creates a Linear Scale or Unclassified map on observable. .domain is how you select the data that will be used for the map and .rage is how you set the rage of colors to go with the data from the domain.
Insert cell
linear = d3.scaleLinear()
.domain([d3.min(CaseConfirmedpct),d3.max(CaseConfirmedpct)])
.range(["#fde0dd", "#c51b8a"])
Insert cell
chart(numericSort(CaseConfirmedpct), linear)
Insert cell
md`# Quantile Classification`
Insert cell
// The code below is used to create a quantile map. scaleQuantile() tell it to create three classes with roughly equal number of elements in them. .domain tells it which data to use and .range tells it which colors to assign to data within certain ranges.
Insert cell
quantile = d3.scaleQuantile()
.domain(CaseConfirmedpct)
.range(["#e5f5f9", "#99d8c9", "#2ca25f"])
Insert cell
chart(numericSort(CaseConfirmedpct), quantile)
Insert cell
md`# Jenks Natural Breaks Classification`
Insert cell
// this is how you create natural breaks for your data. It looks at CaseConfirmedpct for the data and the 3 after CaseConfirmedpct tells it the number of breaks to assign. Natural breaks means that it looks at the data to find naturally occuring gaps in the data distribution.
Insert cell
naturalbreaks = simple.ckmeans(CaseConfirmedpct, 3).map(v => v.pop())
Insert cell
//this maps out the natural breaks from above. .domain is naturalbreaks as that is what is being mapped and the .range assigns the colors for each of those classes
Insert cell
jenks = d3
.scaleThreshold()
.domain(naturalbreaks)
.range(["#deebf7", "#9ecae1", "#3182bd"])
Insert cell
chart(numericSort(CaseConfirmedpct), jenks)
Insert cell
md`# Equal Interval Classification (Quantize)`
Insert cell
//The code below creates a Equal Interval map. domain is set to the min and max of CaseConfirmedpct and the range assigns colors from colorbrewer to each class
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(CaseConfirmedpct),d3.max(CaseConfirmedpct)])
.range(["#fee6ce", "#fdae6b", "#e6550d"])
Insert cell
chart(numericSort(CaseConfirmedpct), quantize)
Insert cell
md`# Threshold`
Insert cell
// below is the threshold map. the .domian has the thresold for the classes. I changed the thresolds from what they orginally were to use all of the colors from colorbrewer. I left it with three classes as that is what it orginally was, I just raised the thresold values to show all of the colors
Insert cell
threshold = d3.scaleThreshold()
.domain([0.09, 0.105, 0.11])
.range(["#fee0d2", "#fc9272", "#de2d26"])
Insert cell
chart(numericSort(CaseConfirmedpct), threshold)
Insert cell
showScaleGrouping(CaseConfirmedpct, {
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