library(tidyverse)
library(skimr)
library(ggthemes)
library(socviz)
library(geofacet)
Classwork 8
Map Visualization
Loading R packages
Part 1. Climate Opinion Map
The following data is for Part 1:
<- read_csv(
climate_opinion_long '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 thatclimate_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!
<- socviz::county_map
county_map
<- county_map |>
county_map mutate(id = as.integer(id))
<- county_map |>
county_full left_join(climate_opinion_long)
Question 3
- Replicate the following map.
- Do not use
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
.
- Do not use
Click to Check the Answer!
<- quantile(climate_opinion_long$perc,
qtiles probs = c(0, 0.25, 0.5, 0.75, 1),
na.rm = TRUE)
<- as.numeric(qtiles)
brk
<- paste0(round(qtiles, 1),
lab "\n(",
c("Min", "25th", "50th", "75th", "Max"),
")")
<- ggplot(data = county_full) +
p1 geom_polygon(mapping = aes(x = long, y = lat, group = group,
fill = perc),
color = "grey60",
linewidth = 0.1)
<- p1 + scale_fill_gradient2(
p2 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 )
)
<- p2 + labs(fill = "Percent\nBelief", title = "U.S. Climate Opinion, 2021",
p 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:
<- read_csv(
unemp_house_prices 'https://bcdanl.github.io/data/unemp_house_prices.csv')
- Use
geom_area()
,geom_line()
, andfacet_geo(~state, labeller = adjust_labels)
to replicate the following figure
<- as_labeller(
adjust_labels function(x) {
case_when(
== "New Hampshire" ~ "N. Hampshire",
x == "District of Columbia" ~ "DC",
x TRUE ~ x
)
} )
Click to Check the Answer!
|>
unemp_house_prices filter(
>= ymd("2008-01-01")
date |>
) 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!