Exercise 24: Larimer County Cities

ESS 330 - Quantitative Reasoning

library(tidymodels)
── Attaching packages ────────────────────────────────────── tidymodels 1.2.0 ──
✔ broom        1.0.7     ✔ recipes      1.1.0
✔ dials        1.3.0     ✔ rsample      1.2.1
✔ dplyr        1.1.4     ✔ tibble       3.2.1
✔ ggplot2      3.5.1     ✔ tidyr        1.3.1
✔ infer        1.0.7     ✔ tune         1.2.1
✔ modeldata    1.4.0     ✔ workflows    1.1.4
✔ parsnip      1.2.1     ✔ workflowsets 1.1.0
✔ purrr        1.0.2     ✔ yardstick    1.3.2
Warning: package 'scales' was built under R version 4.4.3
── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
✖ purrr::discard() masks scales::discard()
✖ dplyr::filter()  masks stats::filter()
✖ dplyr::lag()     masks stats::lag()
✖ recipes::step()  masks stats::step()
• Dig deeper into tidy modeling with R at https://www.tmwr.org
library(tidyverse)
Warning: package 'lubridate' was built under R version 4.4.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ lubridate 1.9.4     ✔ stringr   1.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ readr::col_factor() masks scales::col_factor()
✖ purrr::discard()    masks scales::discard()
✖ dplyr::filter()     masks stats::filter()
✖ stringr::fixed()    masks recipes::fixed()
✖ dplyr::lag()        masks stats::lag()
✖ readr::spec()       masks yardstick::spec()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggrepel)
Warning: package 'ggrepel' was built under R version 4.4.3
library(sf)
Warning: package 'sf' was built under R version 4.4.3
Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(readr)
library(AOI)

us_cities <- read_csv("../data/assignment_data/simplemaps_uscities_basicv1.90/uscities.csv")
Rows: 31254 Columns: 17
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (9): city, city_ascii, state_id, state_name, county_fips, county_name, s...
dbl (6): lat, lng, population, density, ranking, id
lgl (2): military, incorporated

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Convert us_cities df to an sf object using lng and lat
cities_sf <- st_as_sf(us_cities, coords = c("lng", "lat"), crs = 4326)

# Get the spatial boundary of Larimer County, CO
boundary <- aoi_get(state = "CO", county = "Larimer")

# Filter cities located in Larimer County
larimer_cities <- cities_sf %>% 
  filter(county_name == "Larimer")

# Identify the top 3 most populous cities in Larimer County
top_3_cities <- larimer_cities %>% 
  slice_max(population, n = 3)

# Create spatial plot (map)
larimer_pop_map <- ggplot() +
  geom_sf(data = boundary, fill = NA, color = "black", size = 1) +
  geom_sf(data = larimer_cities, fill = "lightgray", color = "darkgray") +
  geom_sf(data = top_3_cities, color = "red", size = 3) +
  theme_void() +
  labs(title = "Cities in Lairmer County",
       subtitle = "Red points: 3 Most Populous Cities") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle
        = element_text(hjust = 0.5)) +
  geom_label_repel(
    data = top_3_cities, 
    aes(label = city, geometry = geometry), 
    stat = "sf_coordinates", 
    size = 3)

# Save the plot to images dir
ggsave(filename = "../images/larimer_pop_map.png", plot = larimer_pop_map, width = 8, height = 6, dpi = 600)
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data