Exercise 25: Mississippi River Mapping

ESS 330 - Quantitative Reasoning

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
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
✔ ggplot2   3.5.1     ✔ stringr   1.5.1
✔ lubridate 1.9.4     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(units)
Warning: package 'units' was built under R version 4.4.3
udunits database from C:/Users/Zacha/AppData/Local/R/win-library/4.4/units/share/udunits/udunits2.xml
library(flextable)
Warning: package 'flextable' was built under R version 4.4.3

Attaching package: 'flextable'

The following object is masked from 'package:purrr':

    compose
library(ggplot2)
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(AOI)
library(USAboundaries)
library(USAboundariesData)

Attaching package: 'USAboundariesData'

The following object is masked from 'package:AOI':

    zipcodes
eqdc <- '+proj=eqdc +lat_0=40 +lon_0=-96 +lat_1=20 +lat_2=60 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs'

rivers <- read_sf("../data/assignment_data/majorrivers")
MS_rivers <- rivers %>% 
  filter(SYSTEM == "Mississippi")
MS_rivers <- st_transform(MS_rivers, 5070)

all_counties <- us_counties()
names(all_counties) <- make.unique(names(all_counties))
conus_counties <- all_counties %>%
  filter(!(state_abbr %in% c("AK", "HI")))
conus_counties <- st_transform(conus_counties, 5070)

# counties intersecting MS river system
ms_counties <- st_filter(conus_counties, MS_rivers, .predicate = st_intersects)
ms_counties <- st_transform(ms_counties, 5070) 

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.
cities_sf <- st_as_sf(cities, coords = c("lng", "lat"), crs = 4269) 
cities_sf <- st_transform (cities_sf, 5070) 

cities_in_ms_counties <- st_join(cities_sf, ms_counties, join = st_intersects)

# Calculate the total urban population in each county
pop_by_county <- cities_in_ms_counties %>%
  group_by(county_name) %>%
  summarise(total_population = sum(population, na.rm = TRUE))

ms_counties_pop <- ms_counties %>%
  st_join(pop_by_county, by = "GEOID")

# plot
ggplot() +
  geom_sf(data = conus_counties, color = "darkgrey", fill = "white") +
  geom_sf(data = ms_counties_pop, aes(fill = total_population), color = NA) +
  geom_sf(data = MS_rivers, color = "lightblue", size = 0.4) +
  scale_fill_viridis_c (na.value = "grey80") +
  theme_void() +
  labs(title = "Urban Population in Counties Intersecting the Mississippi River System",
       fill = "Urban Population")