Public
Edited
Mar 21, 2023
Insert cell
md`# Choropleth Mapping [GHK]
SNAP`
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
format = d => `${d}%`
Insert cell
topojson = require("topojson-client@3")
Insert cell
kanecounty = FileAttachment("kanecounty5.json").json()
Insert cell
county = topojson.feature(kanecounty, kanecounty.objects.kanecounty5)
Insert cell
csv_data = d3.csvParse(await FileAttachment("IllinoisKaneCounty3.csv").text(),({GEOID, Pop2010, TractSNAP}) => [GEOID, +TractSNAP/+Pop2010])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Percentage of Residents Receiving SNAP Benefits"})
Insert cell
Insert cell
pct_snap = Array.from(csv_data.values(), d => d[1])
Insert cell
YlGn = [d3.color("#ffffcc"), d3.color("#c2e699"), d3.color("#78c679"), d3.color("#31a354"),d3.color("#006837")]
Insert cell
naturalbreaks = simple.ckmeans(pct_snap, YlGn.length).map(v => v.pop())
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
// color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

// color = d3.scaleQuantile()
// .domain(med_age)
// .range()

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(YlGn)
Insert cell
Insert cell
width = 945
Insert cell
height = 500

Insert cell
margin = 100
Insert cell
//Rotate the map sets the longitude of origin for our UTM Zone 15N projection.
projection = d3.geoTransverseMercator().rotate([90,0]).fitExtent([[80, 80], [width, height]], county);
//d3 reference for projections: https://github.com/d3/d3-geo/blob/master/README.md

//use the following url for specific projection settings: https://github.com/veltman/d3-stateplane
//Use this code to set up the map projection (if different than geographic projection)

//projection = d3.geoAlbers().fitExtent([[margin, margin], [width - margin, height - margin]], counties)

//projection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], counties)
Insert cell
//Using a path generator to project geometry onto the map
path = d3.geoPath().projection(projection);
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

// CHEAT SHEET: What is SVG? How is it used in visualization? SVG stands for "scalable vector graphics." SVG can be // thought of as a canvas that allows for the map to be visualized. SVG provides a asic template for a map. You can find // new SVG templates online to create new map types.
svg.append("g")
.attr("transform", "translate( 320 ,0)")
.append(() =>
legend({
color: color,
title: data.title,
width: 360,
tickFormat: ".4f"
})
);

// CHEAT SHEET: How do you create a legend? The above code creates a legend. The coordinates after after "translate" // shift the position of the legend, the "color" code is set to pull the color scheme from the previous color cell, the // "title" code is set to pull the title from the data cell, the width is set to a number of pixels, and the "tickFormat" // gives the number of decimal places for the breaks in the classification.

svg.append("g")
.selectAll("path")
.data(county.features)
.join("path")
.attr("stroke", "black")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1.5)
// CHEAT SHEET: How do you determine the style, width and color of the polygon borders (outlines)?
// The above lines define the visuals of the polygon borders. For my map, I changed the color to black to better
// contrast the light colored polygons.
// .attr("fill", function(d){
// console.log(color(data.get(d.properties.GEOID)[0]))
// return color(data.get(d.properties.GEOID)[0]);
// })
.attr("fill", d => color(data.get(d.properties.GEOID)))
.attr("d", path)
.append("title")
.text(d => " Percent Recieving SNAP: " + data.get(d.properties.GEOID));

// CHEAT SHEET: How do the values get displayed when you hover over each polygon? The above "text" code displays the
// text "Percent Recieiving SNAP" plus the corresponding value for that cell by reading in the "data" cell.

return svg.node();
}
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