Skip to content
OpenChart

How to make an animated choropleth map

An animated choropleth recolors each state as the weeks play. This post builds one from 2020 unemployment claims, on a states map and on a tile map.

In the median state, insured unemployment rose from 1.0% to 11.2% by June 27Insured unemployment rate by state, 2020, every other week5.010.015.020.0Insured unemployment rate (%)In the median state, insured unemployment rose from 1.0% to 11.2% by June 27Insured unemployment rate by state, 2020, every other week5.010.015.020.0Insured unemployment rate (%)
Insured unemployment rate by state through 2020 on a playing choropleth. Source: U.S. Department of Labor, weekly unemployment insurance claims; geography: US Census Bureau via us-atlas.

Made with OpenChart. How to build this choropleth map

In the week ending March 21, 2020, the median state’s insured unemployment rate was 1.34%. Fourteen weeks later, in the week ending June 27, it was 11.2%. The rate is the Department of Labor’s weekly count of people collecting state unemployment benefits, as a share of the jobs those benefits cover, and in this dataset it’s averaged over 13 weeks.

An animated choropleth shows that climb one week at a time, recoloring every state as the weeks play. In OpenChart the map is an ordinary map spec with one more encoding channel, encoding.time. The map at the top of this post plays 2020 in 20 seconds; the strip under it plays, pauses and scrubs, and with focus on the strip the arrow keys step through the weeks one at a time.

The data: one row per state per week

An animated map reads long data, one row for each state in each week:

const rows = [
  { state: "Michigan", week: "2020-03-21", rate: 1.88 },
  { state: "Michigan", week: "2020-03-28", rate: 2.4 },
  { state: "Nevada", week: "2020-03-21", rate: 1.65 },
  { state: "Nevada", week: "2020-03-28", rate: 2.26 },
  // ...51 states and DC times 52 weeks: 2,652 rows
];

If your source has one column per week, reshape it before it goes in the spec:

const rows = wide.flatMap(({ state, ...byWeek }) =>
  Object.entries(byWeek).map(([week, rate]) => ({ state, week, rate: Number(rate) })),
);

A state can have at most one row per week. A second row stops the compile with an error that names it: “2 rows have code = ‘AK’ and week = 2020-01-25. Aggregate them first.” The week can be a date string, a number or an ordinal label. Dates and numbers play in order of value, and labels play in the order they first appear unless encoding.time.sort lists them. Data with a single week draws as a static map with no period label.

Adding encoding.time to a map spec

Start from a static choropleth and add a time channel that names the week field. This spec draws all 52 weeks, with the states from us-atlas:

import { createGeoMap } from "@opendata-ai/openchart-vanilla";
import states from "us-atlas/states-10m.json";

const spec = {
  type: "map",
  geo: { features: states, projection: "albersUsa", idField: "name" },
  data: rows,
  encoding: {
    key: { field: "state", type: "nominal" },
    color: { field: "rate", type: "quantitative", format: ".1f", title: "Insured unemployment rate (%)" },
    time: { field: "week", type: "temporal" },
  },
  playback: { duration: 20000, period: { format: "%b %-d" } },
};

const map = createGeoMap(document.getElementById("map"), spec);

The choropleths on this page keep every other week, 26 of the 52, so the page loads quickly. Playback interpolates each state’s rate between the weeks kept, which include March 21 and June 27.

In the median state, insured unemployment rose from 1.0% to 11.2% by June 27Insured unemployment rate by state, 2020, every other week5.010.015.020.0Insured unemployment rate (%)In the median state, insured unemployment rose from 1.0% to 11.2% by June 27Insured unemployment rate by state, 2020, every other week5.010.015.020.0Insured unemployment rate (%)
Insured unemployment rate by state through 2020 on a playing choropleth. Source: U.S. Department of Labor, weekly unemployment insurance claims; geography: US Census Bureau via us-atlas.

Made with OpenChart. How to build this choropleth map

{
  "type": "map",
  "geo": {
    "features": {/* TopoJSON, 181 arcs; Copy includes it */},
    "projection": "albersUsa",
    "idField": "name"
  },
  "data": [
    {
      "state": "Alaska",
      "week": "2020-01-11",
      "rate": 2.75
    },
    {
      "state": "Alaska",
      "week": "2020-01-25",
      "rate": 2.91
    },
    {
      "state": "Alaska",
      "week": "2020-02-08",
      "rate": 3.01
    },
    {
      "state": "Alaska",
      "week": "2020-02-22",
      "rate": 3.04
    },
    {
      "state": "Alaska",
      "week": "2020-03-07",
      "rate": 3
    },
    {
      "state": "Alaska",
      "week": "2020-03-21",
      "rate": 3.03
    },
    // … 1,320 more rows; Copy includes them
  ],
  "encoding": {
    "key": {
      "field": "state",
      "type": "nominal"
    },
    "color": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "period": {
      "format": "%b %-d"
    }
  },
  "chrome": {
    "title": "In the median state, insured unemployment rose from 1.0% to 11.2% by June 27",
    "subtitle": "Insured unemployment rate by state, 2020, every other week"
  }
}
import { GeoMap } from "@opendata-ai/openchart-react";
import type { GeoMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,326 rows; Copy includes them */];

const spec: GeoMapSpec = {
  data,
  "type": "map",
  "geo": {
    "features": {/* TopoJSON, 181 arcs; Copy includes it */},
    "projection": "albersUsa",
    "idField": "name"
  },
  "encoding": {
    "key": {
      "field": "state",
      "type": "nominal"
    },
    "color": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "period": {
      "format": "%b %-d"
    }
  },
  "chrome": {
    "title": "In the median state, insured unemployment rose from 1.0% to 11.2% by June 27",
    "subtitle": "Insured unemployment rate by state, 2020, every other week"
  }
};

export function Example() {
  return <GeoMap spec={spec} />;
}
<script setup lang="ts">
import { GeoMap } from "@opendata-ai/openchart-vue";
import type { GeoMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,326 rows; Copy includes them */];

const spec: GeoMapSpec = {
  data,
  "type": "map",
  "geo": {
    "features": {/* TopoJSON, 181 arcs; Copy includes it */},
    "projection": "albersUsa",
    "idField": "name"
  },
  "encoding": {
    "key": {
      "field": "state",
      "type": "nominal"
    },
    "color": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "period": {
      "format": "%b %-d"
    }
  },
  "chrome": {
    "title": "In the median state, insured unemployment rose from 1.0% to 11.2% by June 27",
    "subtitle": "Insured unemployment rate by state, 2020, every other week"
  }
};
</script>

<template>
  <GeoMap :spec="spec" />
</template>
<script lang="ts">
import { GeoMap } from "@opendata-ai/openchart-svelte";
import type { GeoMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,326 rows; Copy includes them */];

const spec: GeoMapSpec = {
  data,
  "type": "map",
  "geo": {
    "features": {/* TopoJSON, 181 arcs; Copy includes it */},
    "projection": "albersUsa",
    "idField": "name"
  },
  "encoding": {
    "key": {
      "field": "state",
      "type": "nominal"
    },
    "color": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "period": {
      "format": "%b %-d"
    }
  },
  "chrome": {
    "title": "In the median state, insured unemployment rose from 1.0% to 11.2% by June 27",
    "subtitle": "Insured unemployment rate by state, 2020, every other week"
  }
};
</script>

<GeoMap {spec} />
import { createGeoMap } from "@opendata-ai/openchart-vanilla";
import type { GeoMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,326 rows; Copy includes them */];

const spec: GeoMapSpec = {
  data,
  "type": "map",
  "geo": {
    "features": {/* TopoJSON, 181 arcs; Copy includes it */},
    "projection": "albersUsa",
    "idField": "name"
  },
  "encoding": {
    "key": {
      "field": "state",
      "type": "nominal"
    },
    "color": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "period": {
      "format": "%b %-d"
    }
  },
  "chrome": {
    "title": "In the median state, insured unemployment rose from 1.0% to 11.2% by June 27",
    "subtitle": "Insured unemployment rate by state, 2020, every other week"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createGeoMap(container, spec);

time.type is temporal for dates and years, quantitative for other numbers and ordinal for labels such as "2020-Q3". The readout under the map prints each week as “Jun 27, 2020” without a format, because the periods are dates that don’t fall on the first of a month. time.format changes the readout and playback.period.format changes only the large period label in the lower left of the map, which this spec shortens to “Jun 27”.

The control strip takes 44 pixels of the container’s height, and the map fills the rest.

Identity: how a state is followed from week to week

encoding.key is the field that identifies a state in every week, and it’s required once a map has a time channel. Each value is joined to the feature property geo.idField names, here the state’s name in us-atlas, so “Nevada” in the data colors the Nevada shape in every frame. A key with no matching shape is reported in a warning, as it is on a static map.

The color channel has to be quantitative. A time map recolors each state from its value, so a categorical color is a validation error that asks for a numeric field. A geo map’s points layer can play as well: give it points.time and points.key, and the symbols grow, shrink and fade in step with the states underneath.

A color scale fixed for the whole year

OpenChart computes the color classes and the legend once, from every week’s values together, and keeps them for the whole playback. On this map the breaks fall at 5%, 10%, 15% and 20%. In January every state is in the lightest of the five classes. By July all five are in use.

A scale refit to each week would give rates almost nine times apart the same color. In the first week of January the highest rate is Alaska’s 2.64%, so a per-week scale would paint Alaska in the darkest class, the same blue Nevada gets at its peak, 23.13% in the week ending August 1. The legend would also change with every week of playback. The axes of a playing chart are fixed for the same reason.

Between two weeks each state’s rate is interpolated, and its color jumps to the next class at the moment the rate crosses a break. A state with no row in some week gets a value interpolated from the weeks on either side by default (playback.gaps: "interpolate"). "hold" carries the last value forward, and "drop" shows the state as no data until its next row. Once any state lacks a value in any week, the legend keeps its “No data” swatch for the whole run.

The same data on a tile map

A tile map takes the same time channel. Its identity is encoding.state, the two-letter postal code, and its data has to be an array of rows; the object form a static tile map accepts, { "NV": 7.3 }, has no room for a week. Tile maps shade by a single color’s intensity, and that scale is fixed across the run too: the legend reads 0.4 to 22.5 in every frame.

Every state ended 2020 with a higher rate than it started withInsured unemployment rate by state, 2020, every fourth weekME2.3VT3.9NH3.6WA5.0ID1.3MT3.5ND1.8MN4.3WI3.4MI4.8NY5.0MA5.9OR4.5NV7.3WY2.1SD1.0IA2.5IL5.9IN2.9OH3.0PA5.7NJ4.2CT5.1CA7.7UT1.1CO3.7NE1.4MO1.8KY2.8WV3.3VA2.4MD3.8DE3.3RI3.7AZ3.3NM6.2KS3.3AR2.0TN1.8NC1.9SC2.4DC5.8AK6.4OK3.4LA5.0MS3.3AL1.4GA5.7HI8.0TX3.9FL2.30.422.5Every state ended 2020 with a higher rate than it started withInsured unemployment rate by state, 2020, every fourth weekME2.3VT3.9NH3.6WA5.0ID1.3MT3.5ND1.8MN4.3WI3.4MI4.8NY5.0MA5.9OR4.5NV7.3WY2.1SD1.0IA2.5IL5.9IN2.9OH3.0PA5.7NJ4.2CT5.1CA7.7UT1.1CO3.7NE1.4MO1.8KY2.8WV3.3VA2.4MD3.8DE3.3RI3.7AZ3.3NM6.2KS3.3AR2.0TN1.8NC1.9SC2.4DC5.8AK6.4OK3.4LA5.0MS3.3AL1.4GA5.7HI8.0TX3.9FL2.30.422.5
Live example. Source: U.S. Department of Labor, weekly unemployment insurance claims.

Made with OpenChart. How to build this tile map

{
  "type": "tilemap",
  "encoding": {
    "state": {
      "field": "code",
      "type": "nominal"
    },
    "value": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 12000
  },
  "chrome": {
    "title": "Every state ended 2020 with a higher rate than it started with",
    "subtitle": "Insured unemployment rate by state, 2020, every fourth week"
  },
  "data": [
    {
      "code": "AK",
      "week": "2020-01-25",
      "rate": 2.91
    },
    {
      "code": "AK",
      "week": "2020-02-22",
      "rate": 3.04
    },
    {
      "code": "AK",
      "week": "2020-03-21",
      "rate": 3.03
    },
    {
      "code": "AK",
      "week": "2020-04-18",
      "rate": 5.93
    },
    {
      "code": "AK",
      "week": "2020-05-16",
      "rate": 9.97
    },
    {
      "code": "AK",
      "week": "2020-06-13",
      "rate": 13.72
    },
    // … 657 more rows; Copy includes them
  ]
}
import { TileMap } from "@opendata-ai/openchart-react";
import type { TileMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 663 rows; Copy includes them */];

const spec: TileMapSpec = {
  data,
  "type": "tilemap",
  "encoding": {
    "state": {
      "field": "code",
      "type": "nominal"
    },
    "value": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 12000
  },
  "chrome": {
    "title": "Every state ended 2020 with a higher rate than it started with",
    "subtitle": "Insured unemployment rate by state, 2020, every fourth week"
  }
};

export function Example() {
  return <TileMap spec={spec} />;
}
<script setup lang="ts">
import { TileMap } from "@opendata-ai/openchart-vue";
import type { TileMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 663 rows; Copy includes them */];

const spec: TileMapSpec = {
  data,
  "type": "tilemap",
  "encoding": {
    "state": {
      "field": "code",
      "type": "nominal"
    },
    "value": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 12000
  },
  "chrome": {
    "title": "Every state ended 2020 with a higher rate than it started with",
    "subtitle": "Insured unemployment rate by state, 2020, every fourth week"
  }
};
</script>

<template>
  <TileMap :spec="spec" />
</template>
<script lang="ts">
import { TileMap } from "@opendata-ai/openchart-svelte";
import type { TileMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 663 rows; Copy includes them */];

const spec: TileMapSpec = {
  data,
  "type": "tilemap",
  "encoding": {
    "state": {
      "field": "code",
      "type": "nominal"
    },
    "value": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 12000
  },
  "chrome": {
    "title": "Every state ended 2020 with a higher rate than it started with",
    "subtitle": "Insured unemployment rate by state, 2020, every fourth week"
  }
};
</script>

<TileMap {spec} />
import { createTileMap } from "@opendata-ai/openchart-vanilla";
import type { TileMapSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 663 rows; Copy includes them */];

const spec: TileMapSpec = {
  data,
  "type": "tilemap",
  "encoding": {
    "state": {
      "field": "code",
      "type": "nominal"
    },
    "value": {
      "field": "rate",
      "type": "quantitative",
      "title": "Insured unemployment rate (%)",
      "format": ".1f"
    },
    "time": {
      "field": "week",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 12000
  },
  "chrome": {
    "title": "Every state ended 2020 with a higher rate than it started with",
    "subtitle": "Insured unemployment rate by state, 2020, every fourth week"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createTileMap(container, spec);

This spec keeps every fourth week, 13 of the 52, counted back from December 26 so the last week of the year stays in. That cuts the data to 663 rows, short enough to read in a code block, and it keeps the week ending March 21, the last one before claims jumped. Every equal-sized tile also gets a label with its rate, which a small state’s shape on the choropleth has no room for.

Playback options

playback holds the timing, and its fields have the same names and defaults as the bar chart race’s race block:

Option Default What it does
duration 700 ms per period, kept between 8 and 90 seconds Total run length at normal speed
step none Time per period instead of a total; set duration or step, not both
holdEnd 1500 Milliseconds the last frame holds before the run ends or loops
loop false Start over after the hold
autoplay "visible" Start once the map is a quarter in view; true starts at mount, false waits for play
controls true The play button, scrubber and readout
gaps "interpolate" Missing weeks: "interpolate", "hold" or "drop"
period lower left on maps The large period label: { format, position }, or false to hide it

Without a duration, 52 weekly periods would play in 36.4 seconds. playback.labels and playback.trails belong to animated scatter plots, and a map spec that sets them fails validation.

Controls and map.playback

The object createGeoMap and createTileMap return has a playback property, which is null for a map without a time channel. It has play(), pause(), seek(t), setSpeed(speed) and getState(), and on("period", callback) fires each time the playhead crosses into a new week or lands on one after a seek. seek takes a zero-based period index, and fractions land between weeks. To open the map paused on the week the median state peaked:

const spec = { ...mapSpec, playback: { ...mapSpec.playback, autoplay: false } };
const map = createGeoMap(container, spec);

map.playback?.seek(25); // the week ending June 27
map.playback?.on("period", (period) => {
  caption.textContent = `Week ending ${period.label}`;
});

Readers who have asked their system for reduced motion get the map paused on the last week. The scrubber and arrow keys still move between weeks, and play steps through them one at a time with no tween. A live region announces the week, and the states with the highest and lowest rates, when the reader pauses, steps or scrubs; it stays quiet during playback.

Static output for previews and social cards

renderStaticSVG from @opendata-ai/openchart-vanilla/static draws a time map without a browser, as an SVG string. It shows the last week by default; pass t to draw another:

import { renderStaticSVG } from "@opendata-ai/openchart-vanilla/static";

const svg = renderStaticSVG(spec, { width: 720, height: 480, t: 25 });

Use it for the image in a newsletter or a link preview, or as a no-JavaScript fallback. A static SVG has no control strip. On a mounted map, map.export("svg") and map.export("png") capture the frame on screen.

Every playback option, the points layer and the no-data rules are on the time-lapse maps docs page.