Public
Edited
Mar 1, 2023
Insert cell
md`# Assignment 3 Grubbs
Proportion of Rural Residents in Ohio Counties`
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
ohio_counties = FileAttachment("Ohio_counties.json").json()
Insert cell
county_features = topojson.feature(ohio_counties, ohio_counties.objects.Ohio_counties_1)
Insert cell
csv_data = d3.csvParse(await FileAttachment("OhioCounties.csv").text(),({FIPS, Num_Rural, Population}) => [+FIPS,+Num_Rural/+Population])
Insert cell
Insert cell
data = Object.assign(new Map(csv_data), {title: "Proportion of Residents Who are Rural"})
Insert cell
Insert cell
propRural = Array.from(csv_data.values(), d => d[1])
Insert cell
Insert cell
YlOrBr = [d3.color("#ffffd4"), d3.color("#fed98e"), d3.color("#fe9929"), d3.color("#cc4c02")]
Insert cell
Insert cell
color = d3.scaleThreshold()
.domain([0.25, 0.5, 0.75])
.range(YlOrBr)
Insert cell
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(threshold)
// .range(YlOrBr)
Insert cell
width = 900
Insert cell
Insert cell
height = 950
Insert cell
margin = 100
Insert cell
//Rotate the map sets the longitude of origin for our UTM Zone 15N projection.
// projection = d3.geoTransverseMercator().rotate([84,0]).fitExtent([[80, 80], [width, height]], county_features);
//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]], county_features)

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

svg.append("g")
.attr("transform", "translate(300,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 300,
tickFormat: ".2f"
})
);

svg.append("g")
.selectAll("path")
.data(county_features.features)
.join("path")
.attr("stroke", "black")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1)
// .attr("fill", function(d){
// console.log(color(data.get(d.properties.FIPS)[0]))
// return color(data.get(d.properties.FIPS)[0]);
// })
.attr("fill", d => color(data.get(+d.properties.FIPS)))
.attr("d", path)
.append("title")
.text(d => " Rural Proportion: " + data.get(+d.properties.FIPS));

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more