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')
id <dbl> | GeoName <chr> | belief <chr> | perc <dbl> | |
---|---|---|---|---|
1001 | Autauga County, Alabama | happening | 59.187 | |
1001 | Autauga County, Alabama | human | 45.457 | |
1003 | Baldwin County, Alabama | happening | 60.461 | |
1003 | Baldwin County, Alabama | human | 44.678 | |
1005 | Barbour County, Alabama | happening | 68.059 | |
1005 | Barbour County, Alabama | human | 50.516 | |
1007 | Bibb County, Alabama | happening | 57.593 | |
1007 | Bibb County, Alabama | human | 42.594 | |
1009 | Blount County, Alabama | happening | 52.504 | |
1009 | Blount County, Alabama | human | 41.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 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')
date <date> | state <chr> | unemploy_perc <dbl> | house_price_index <dbl> | house_price_perc <dbl> |
---|---|---|---|---|
1976-01-01 | Alabama | 6.7 | 37.13829 | 4.040541e-02 |
1976-02-01 | Alabama | 6.7 | 37.75644 | 5.180597e-02 |
1976-03-01 | Alabama | 6.6 | 38.27632 | 5.930233e-02 |
1976-04-01 | Alabama | 6.5 | 38.59613 | 5.958458e-02 |
1976-05-01 | Alabama | 6.4 | 38.74052 | 5.572338e-02 |
1976-06-01 | Alabama | 6.5 | 38.79723 | 5.359368e-02 |
1976-07-01 | Alabama | 6.6 | 38.85695 | 5.741669e-02 |
1976-08-01 | Alabama | 6.8 | 38.91870 | 6.592198e-02 |
1976-09-01 | Alabama | 6.9 | 38.95837 | 7.456134e-02 |
1976-10-01 | Alabama | 7.0 | 39.01876 | 7.967682e-02 |
- 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()
)