Research, Writings + Code

Coding Annoyances

Fed Districts Map with D3

The goal is to generate a map of the 12 Federal Reserve Districts using d3.js.

Here is the final result:

The historical definitions of the Fed Districts are available from FRASER, a service for archival information from the St. Louis Fed.

The county definitions in the PDF were manually coded into a TSV file by Chris Vecchio. Over time, some of the county definitions have changed but some may have been missed, so clearly, this file does not represent official definitions.

Choropleth map

The Fed Districts map is a straight-up modification of the recent example of a choropleth map for unemployment rates by Mike Bostock. He uses a threshold scale and the blues color scheme which work great for a continuous variable such as the unemployment rate, but are probably not the best choice for a categorical map with 12 categories! For one, the blues color scheme does not even have 12 categories. So I went over to ColorBrewer and selected a scheme with soft colors for qualitative data with 12 categories.

I replaced the CSV file for the unemployment rates with the Fed Districts definitions and I added code to define the boundaries between Districts using topojson.mesh in the same way as Mike generated the boundaries between states.

I also learned about map() to define dictionary-like variables that contain useful information about each county to display on hover.

Notice that the Fed District numbers are defined in a Westerly direction.

<!DOCTYPE html>
<meta charset="utf-8">

.counties {
  fill: none;

.states {
  fill: none;
  stroke: #fff;
  stroke-linejoin: round;

.districts {
  fill: none;
  stroke: #808080;
  stroke-linejoin: round;
  stroke-width: 1.5px;

<svg width="960" height="600"></svg>
<script src=""></script>
<script src=""></script>
<script src=""></script>

var svg ="svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

// Define maps with definitions and county and District names
var frbdefinitions =;
var countynames    =;
var districtNames  =;

// Harcoded Fed District denominations
// The city name indicates the city of the main office
// Notice that the district numbers are defined in a Westerly direction 
districtNames.set(2,"New York")
districtNames.set(8,"St. Louis")
districtNames.set(10,"Kansas City")
districtNames.set(12,"San Francisco")

var path = d3.geoPath();

var x = d3.scaleLinear()
    .domain([0, 12])
    .rangeRound([600, 860]);

var color = d3.scaleThreshold()
    .domain(d3.range(1, 13))

var g = svg.append("g")
    .attr("class", "key")
    .attr("transform", "translate(0,40)");

  .data(color.range().map(function(d) {
      d = color.invertExtent(d);
      if (d[0] == null) d[0] = x.domain()[0];
      if (d[1] == null) d[1] = x.domain()[1];
      return d;
    .attr("height", 8)
    .attr("x", function(d) { return x(d[0]); })
    .attr("width", function(d) { return x(d[1]) - x(d[0]); })
    .attr("fill", function(d) { return color(d[0]); });

    .attr("class", "caption")
    .attr("x", x.range()[0])
    .attr("y", -6)
    .attr("fill", "#000")
    .attr("text-anchor", "start")
    .attr("font-weight", "bold")
    .text("Fed District");
    .tickFormat(function(x, i) { return i ? x : x ; })

    .defer(d3.json, "")
    .defer(d3.tsv, "counties-by-fed-district.tsv", function(d) { 
        frbdefinitions.set(d.countyfips, +d.dist_frb); 
        countynames.set(d.countyfips, d.county_name+", "+d.state_abbr);

function ready(error, us) {
  if (error) throw error;

  // Select fill color for Fed Districts using the map for the definitions
  // and the color scheme defined earlier
  // Also add information to each county to be displayed on hover
      .attr("class", "counties")
    .data(topojson.feature(us, us.objects.counties).features)
      .attr("fill", function(d) { return color(d.dist_frb  = frbdefinitions.get( -1); })
      .attr("d", path)
      .text(function(d) { 
        return "District: " + (+d.dist_frb + 1) + ". " +
               districtNames.get(+d.dist_frb + 1) + "\n" +
               "County fips: " + + "\n" +
               "County name: " + countynames.get(});

  // Define boundaries for states
      .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
      .attr("class", "states")
      .attr("d", path);

  // Define boundaries for Fed Districts using counties
      .datum(topojson.mesh(us, us.objects.counties, function(a, b) { 
        return (frbdefinitions.get( !== frbdefinitions.get( & (a !== b); }))
      .attr("class", "districts")
      .attr("d", path);



If you enjoyed this article, consider sharing it.

Follow for updates.

Follow for updates.


Comments section

comments powered by Disqus