Classwork 8

Map Visualization

Author

Byeong-Hak Choe

Published

March 26, 2025

Modified

April 16, 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')



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')
  • 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()
  )

Discussion

Welcome to our Classwork 8 Discussion Board! 👋

This space is designed for you to engage with your classmates about the material covered in Classwork 8.

Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.

If you have any specific questions for Byeong-Hak (@bcdanl) regarding the Classwork 8 materials or need clarification on any points, don’t hesitate to ask here.

All comments will be stored here.

Let’s collaborate and learn from each other!

Back to top