Classwork 8

Map Visualization

Author

Byeong-Hak Choe

Published

March 26, 2025

Modified

March 27, 2025

Loading R packages

library(tidyverse)
library(skimr)
library(ggthemes)
library(socviz)
library(geofacet)

Part 1. Climate Opinion Map

The following data is for Part 1:

climate_opinion_long <- read_csv(
  'https://bcdanl.github.io/data/climate_opinion_2021.csv')
ABCDEFGHIJ0123456789
id
<dbl>
GeoName
<chr>
belief
<chr>
perc
<dbl>
1001Autauga County, Alabamahappening59.187
1001Autauga County, Alabamahuman45.457
1003Baldwin County, Alabamahappening60.461
1003Baldwin County, Alabamahuman44.678
1005Barbour County, Alabamahappening68.059
1005Barbour County, Alabamahuman50.516
1007Bibb County, Alabamahappening57.593
1007Bibb County, Alabamahuman42.594
1009Blount County, Alabamahappening52.504
1009Blount County, Alabamahuman41.505



Variable Description

  • belief:
    • human: Estimated percentage who think that global warming is caused mostly by human activities.
    • happening: Estimated percentage who think that global warming is happening.

Question 1

  • Filter climate_opinion_long, so that climate_opinion_long has only estimated percentage of people who think that global warming is caused mostly by human activities.
Click to Check the Answer!
climate_opinion_long <- climate_opinion_long |>  
  filter(belief == 'human')



Question 2

  • Join the two data.frames, socviz::county_map and the resulting data.frame in Question 1.
Click to Check the Answer!
county_map <- socviz::county_map

county_map <- county_map |> 
  mutate(id = as.integer(id))

county_full <- county_map |> 
  left_join(climate_opinion_long)



Question 3

  • Replicate the following map.
    • Do not use coord_map(projection = "albers", lat0 = 39, lat1 = 45).

Click to Check the Answer!
qtiles <- quantile(climate_opinion_long$perc, 
                   probs = c(0, 0.25, 0.5, 0.75, 1), 
                   na.rm = TRUE)

brk <- as.numeric(qtiles)

lab <- paste0(round(qtiles, 1), 
              "\n(", 
              c("Min", "25th", "50th", "75th", "Max"), 
              ")")

p1 <- ggplot(data = county_full) + 
  geom_polygon(mapping = aes(x = long, y = lat, group = group, 
                             fill = perc),
               color = "grey60", 
               linewidth = 0.1) 

p2 <- p1 + scale_fill_gradient2( 
  low = '#2E74C0',  
  high = '#CB454A',  
  mid = 'white', 
  na.value = "grey80",
  midpoint = quantile(climate_opinion_long$perc, .5, na.rm = T),
  breaks = brk,
  labels = lab,
  guide = guide_colorbar( direction = "horizontal",
                          barwidth = 25,
                          title.vjust = 1 )
) 

p <- p2 + labs(fill = "Percent\nBelief", title = "U.S. Climate Opinion, 2021",
               caption = "Sources: Yale Program on Climate Change Communication\n(https://climatecommunication.yale.edu/visualizations-data/ycom-us/)") +
  theme_map() +
  theme(plot.margin = unit( c(1, 1, 3.85, 0.5), "cm"),
        plot.title = element_text(size = rel(2),
                                  hjust = .5),
        plot.caption = element_text(hjust = 1),
        legend.position = c(0.5, -.15),
        legend.justification = c(.5,.5),
        aspect.ratio = .8,
        strip.background = element_rect( colour = "black",
                                         fill = "white",
                                         color = "grey80" )
  ) +
  guides(fill = guide_colourbar(direction = "horizontal", 
                                barwidth = 25,
                                title.vjust = 1)
         )
p



Part 2. Unemployment Rate Maps with geofacet::facet_geo()

The following data is for Part 2:

unemp_house_prices <- read_csv(
  'https://bcdanl.github.io/data/unemp_house_prices.csv')
ABCDEFGHIJ0123456789
date
<date>
state
<chr>
unemploy_perc
<dbl>
house_price_index
<dbl>
house_price_perc
<dbl>
1976-01-01Alabama6.737.138294.040541e-02
1976-02-01Alabama6.737.756445.180597e-02
1976-03-01Alabama6.638.276325.930233e-02
1976-04-01Alabama6.538.596135.958458e-02
1976-05-01Alabama6.438.740525.572338e-02
1976-06-01Alabama6.538.797235.359368e-02
1976-07-01Alabama6.638.856955.741669e-02
1976-08-01Alabama6.838.918706.592198e-02
1976-09-01Alabama6.938.958377.456134e-02
1976-10-01Alabama7.039.018767.967682e-02
  • Use geom_area(), geom_line(), and facet_geo(~state, labeller = adjust_labels) to replicate the following figure
adjust_labels <- as_labeller(
  function(x) {
    case_when(
      x == "New Hampshire" ~ "N. Hampshire",
      x == "District of Columbia" ~ "DC",
      TRUE ~ x
    )
  }
)


Click to Check the Answer!
unemp_house_prices |> 
  filter(
    date >= ymd("2008-01-01")
  ) |>
  ggplot(aes(date, unemploy_perc)) + 
  geom_area(fill = "#56B4E9", alpha = 0.7) +
  geom_line() + 
  scale_y_continuous(
    name = "unemployment rate",
    limits = c(0, 16),
    breaks = c(0, 5, 10, 15),
    labels = c("0%", "5%", "10%", "15%")
  ) +
  scale_x_date(
    name = NULL,
    breaks = ymd(c("2009-01-01", "2011-01-01", 
                   "2013-01-01", "2015-01-01", "2017-01-01")),
    labels = c("'09", "'11", "'13", "'15", "'17")
  ) +
  facet_geo(~state, labeller = adjust_labels) +
  theme(
    strip.text = element_text(
      margin = margin(3, 3, 3, 3)
    ),
    axis.line.x = element_blank()
  )
Back to top