Visualizing 1970s U.S. Unemployment: Create Geographic Heatmap with ggplot2

In this work, we’ll use R ggplot2 to create a geographic heatmap to visualize the unemployment rate in counties in the U.S. in the 1970s.


Data cleanup

Load the unemployment data.

library(tidyverse)library(mapproj)
data(unemp, package = "viridis")tibble(unemp)

Output:

# A tibble: 3,218 × 8
id state_fips county_fips name year rate county state
<chr> <int> <int> <chr> <int> <dbl> <chr> <chr>
1 CN010010 1 1 Autauga County, AL 2009 9.7 autauga AL
2 PA011000 1 3 Baldwin County, AL 2009 9.1 baldwin AL
3 CN010050 1 5 Barbour County, AL 2009 13.4 barbour AL
# ℹ 3,215 more rows

Prepare the dataset of the counties. It is used to draw county outlines, and is combined with the unemployment dataset.

d.county <- map_data("county",                       projection = "albers",                       parameters = c(39, 45)) %>%   as_tibble() %>%   # rename columns  rename(state_name = "region", county = "subregion") %>%   # create state name abbreviations  mutate(state = state.abb[match(.$state_name, tolower(state.name))]) %>%   left_join(unemp, by = c("state", "county"))
d.county

Output:

# A tibble: 87,949 × 13
long lat group order state_name county state id state_fips county_fips
<dbl> <dbl> <dbl> <int> <chr> <chr> <chr> <chr> <int> <int>
1 0.139 -1.27 1 1 alabama autauga AL CN010… 1 1
2 0.139 -1.27 1 2 alabama autauga AL CN010… 1 1
3 0.138 -1.27 1 3 alabama autauga AL CN010… 1 1
# ℹ 87,946 more rows
# ℹ 3 more variables: name <chr>, year <int>, rate <dbl>

Dataset of the states, used to draw state outlines.

d.state <- map_data("state",                     projection = "albers",                     parameters = c(39, 45)) %>% as_tibble()d.state

Output:

# A tibble: 15,537 × 6
long lat group order region subregion
<dbl> <dbl> <dbl> <int> <chr> <chr>
1 0.128 -1.31 1 1 alabama <NA>
2 0.128 -1.31 1 2 alabama <NA>
3 0.127 -1.31 1 3 alabama <NA>
# ℹ 15,534 more rows

Visualization

Create a map outlined by counties, with unemployment rate mapped to colors.

p1 <- d.county %>%   ggplot(aes(long, lat, group = group, fill = rate)) +  geom_polygon(colour = alpha("white", .7),                linewidth = 0.2) +  coord_fixed() p1

Sketch the outline of states.

p2 <- p1 +   geom_polygon(data = d.state,                colour = "white", fill = NA, linewidth = .5) p2

Make some final outlook adjustment.

p3 <- p2 +   # polish up the theme   theme_void() +  theme(legend.position = "bottom") +    # adjust the colorbar  guides(fill = guide_colorbar(    barwidth = unit(200, "pt"),    barheight = unit(7, "pt"),    title = "unemployment rate in the U.S.",    title.position = "top", # put title on top of the colorbar    title.theme = element_text(hjust = .5, face = "bold", vjust = .8))) +    # use the viridis color scale  scale_fill_viridis_c(option = "A", breaks = seq(0, 30, 5)) 
p3
library(tidyverse)library(mapproj)
# load unemployment datadata(unemp, package = "viridis")tibble(unemp)
# Prepare the dataset of the counties. d.county <- map_data("county", projection = "albers", parameters = c(39, 45)) %>% as_tibble() %>% # rename columns rename(state_name = "region", county = "subregion") %>% # create state name abbreviations mutate(state = state.abb[match(.$state_name, tolower(state.name))]) %>% left_join(unemp, by = c("state", "county"))
d.county

# Dataset of the states, used to draw state outlines. d.state <- map_data("state", projection = "albers", parameters = c(39, 45)) %>% as_tibble()d.state
### Visualization
# Create a map outlined by counties, with unemployment rate mapped to colors.p1 <- d.county %>% ggplot(aes(long, lat, group = group, fill = rate)) + geom_polygon(colour = alpha("white", .7), linewidth = 0.2) + coord_fixed() p1
# Sketch the outline of states. p2 <- p1 + geom_polygon(data = d.state, colour = "white", fill = NA, linewidth = .5) p2
# Make some final outlook adjustment. p3 <- p2 + # polish up the theme theme_void() + theme(legend.position = "bottom") + # adjust the colorbar guides(fill = guide_colorbar( barwidth = unit(200, "pt"), barheight = unit(7, "pt"), title = "unemployment rate in the U.S.", title.position = "top", title.theme = element_text(hjust = .5, face = "bold", vjust = .8))) + # use the viridis color scale scale_fill_viridis_c(option = "A", breaks = seq(0, 30, 5))
p3