Homework 4
sf
Maps; Animation Plots
Direction
Please submit your R script for Homework 4 to Brightspace with the name below:
danl-310-hw4-LASTNAME-FIRSTNAME.R
( e.g.,danl-310-hw4-choe-byeonghak.R
)
The due is May 5, 2025, 2:00 P.M.
Please send Byeong-Hak an email (
bchoe@geneseo.edu
) if you have any questions.
Part 1. ggmap
visualization
Question 1
Replicate the following map using Classwork 10 example.
Below Google font families are used:
- Title and Label: Pacifico
- Subtitle and Legend: Annie Use Your Telescope
- Caption: Alegreya Sans
scale_*_colorblind()
is used to make a plot colorblind-friendly.The following option is used to create the figure.
ggsave("PATHNAME_OF_GGPLOT_OUTPUT.png",
plot = p, height = 12, width = 8, units = "in")
Part 2. Simple Feature (sf
) and Aninimation Plots
Consider the county-level climate opinion data from Yale Program on Climate Change Communication:
<- read_csv(
climate_opinion 'https://bcdanl.github.io/data/climate-change-public-opinion.csv')
Question 2
- Replicate the following map about
variable
human:- human: Estimated percentage who think that global warming is caused mostly by human activities
- For R packages and map data, use the following:
# Load required packages ---------------------------------------------------
library(tidyverse)
library(sf) # simple-features objects for spatial data
library(tigris) # fast access to Census TIGER/Line shapefiles
library(ggthemes)
library(gganimate) # turn static ggplots into animations
library(showtext) # Google-font rendering inside R graphics
# Register Google fonts so they render the same on any machine -------------
font_add_google("Roboto", "roboto") # body text
font_add_google("Alegreya Sans","aleg") # title text
showtext_auto() # activate showtext globally
# 1) Download US state & county geometries (simplified “cartographic”
# boundaries) at 1:5 million scale, then shift Alaska, Hawai‘i & PR closer ---
<- states(
us_states cb = TRUE, # use generalized cartographic boundary files
resolution = "5m", # 1 : 5 000 000
year = 2024 # 2024 vintage
|>
) shift_geometry() # moves AK/HI & PR into a CONUS-friendly layout
<- counties(
us_counties state = NULL, # all states + DC + territories
cb = TRUE,
resolution = "5m",
year = 2024
|>
) shift_geometry()
# 2) Build a bounding box that covers only the 50 states -------------------
# (exclude DC + territories so the map crops nicely)
<- states(cb = TRUE, resolution = "5m", year = 2024)
main_states
<- state.abb # built-in vec of 50 state abbr.
keep <- main_states |>
main_states filter(STUSPS %in% keep) |>
shift_geometry()
<- sf::st_bbox(main_states) # xmin / ymin / xmax / ymax vector
bb
# 3) Quick static map -------------------------------------------------------
ggplot() +
geom_sf(data = us_counties,
fill = NA, # counties as thin outlines
color = "grey80",
linewidth = 0.01) +
geom_sf(data = us_states,
fill = NA, # thicker state borders
color = "black") +
coord_sf( # crop to 50-state bounding box
xlim = c(bb["xmin"], bb["xmax"]),
ylim = c(bb["ymin"], bb["ymax"]),
expand = FALSE
+
) theme_map() # clean, border-free background
Question 3
- Animate the maps for the even-numbered years used in Question 2:
- For
animate()
, use the following parameter setting:
ggplot() +
...labs(subtitle = "Year: {floor(frame_time)}") +
...
<- animate(p_anim,
anim nframes = 20,
fps = 5,
width = 8, height = 5,
units = "in",
res = 900) # resolution can be reduced
# To save animation as PATHNAME_FOR_YOUR_FILE.gif
anim_save("PATHNAME_FOR_YOUR_FILE.gif", anim)
You can open “
PATHNAME_FOR_YOUR_FILE.gif
” in any web browser.transition_time(year)
treats year as a continuous numeric variable.After you call
animate()
,gganimate
:
- Spans the full numeric range of the variable — here 2014 → 2024, a width of 10 years.
- Slices that span into
nframes
equally spaced instants.- Here
nframes = 20
, so the frame-to-frame step is \[ \begin{align} \Delta = \frac{2024 - 2014}{20 - 1} \;=\; \frac{10}{19} \;\approx\; 0.526 \text{ years}. \end{align} \]
- Here
- Assigns each frame its own “virtual” time stamp:
- \(t_0 = 2014\), \(t_1 = 2014.526\), \(t_2 = 2015.053\), …