Getting Spatial Data

USAboundaries::us_states(resolution = "low") %>%
  filter(!(name %in% c("Puerto Rico", "Alaska", "Hawaii"))) %>%
  st_transform(5070) ->
  USAconus

rnaturalearthdata::countries110 %>%
  st_as_sf() %>%
  filter(admin %in% c("Mexico", "United States of America", "Canada")) %>%
  st_transform(5070) ->
  northAmericaBoundaries

read_csv("data/uscities.csv") %>%
  filter(!(state_name %in% c("Puerto Rico", "Alaska", "Hawaii"))) %>%
  select(city, state_id, state_name, county_name, lat, lng, population) %>%
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>%
  st_transform(5070) ->
  USAcities

Distances

# Get USA border
USAboundaries::us_states(resolution = "low") %>%
  st_transform(5070) %>%
  st_union() %>%
  st_cast("MULTILINESTRING") ->
  USAborder

# Add column for city distance to border
USAcities %>%
  mutate(dist_to_border = st_distance(USAcities, USAborder) %>%
           set_units("km") %>%
           drop_units()) ->
  USAcities

# Create table for the 5 farthest cities from border
knitr::kable(
  USAcities %>%
    as.data.frame() %>%
    select(city, state_name, dist_to_border) %>%
    arrange(-dist_to_border) %>%
    head(5),
  col.names = c("City", "State", "Distance to USA Border (km)"),
  caption = "US cities farthest from national border or coastline"
)

# Get USA State borders
USAboundaries::us_states(resolution = "low") %>%
  st_transform(5070) %>%
  st_combine() %>%
  st_cast("MULTILINESTRING") ->
  USAstateBorders

# Add column for city distance to state border
USAcities %>%
  mutate(dist_to_state_border = st_distance(USAcities, USAstateBorders) %>%
           set_units("km") %>%
           drop_units()) ->
  USAcities

# Create table for the 5 farthest cities from their state border
knitr::kable(
  USAcities %>%
    as.data.frame() %>%
    select(city, state_name, dist_to_state_border) %>%
    arrange(-dist_to_state_border) %>%
    head(5),
  col.names = c("City", "State", "Distance to State Border (km)"),
  caption = "US cities farthest from respective state border"
)

# Get Mexico national border
northAmericaBoundaries %>%
  filter(admin == "Mexico") %>%
  st_union() %>%
  st_cast("MULTILINESTRING") ->
  mexicoBorder

# Add column for city distance to Mexico
USAcities %>%
  mutate(dist_to_mexico_border = st_distance(USAcities, mexicoBorder) %>%
           set_units("km") %>%
           drop_units()) ->
  USAcities

# Create table for the 5 farthest cities from their state border
knitr::kable(
  USAcities %>%
    as.data.frame() %>%
    select(city, state_name, dist_to_mexico_border) %>%
    arrange(-dist_to_mexico_border) %>%
    head(5),
  col.names = c("City", "State", "Distance to Mexican Border (km)"),
  caption = "US cities farthest from Mexican border"
)

# Get Canada national border
northAmericaBoundaries %>%
  filter(admin == "Canada") %>%
  st_union() %>%
  st_cast("MULTILINESTRING") ->
  canadaBorder

# Add column for city distance to Canada
USAcities %>%
  mutate(dist_to_canada_border = st_distance(USAcities, canadaBorder) %>%
           set_units("km") %>%
           drop_units()) ->
  USAcities

# Create table for the 5 farthest cities from Canada
knitr::kable(
  USAcities %>%
    as.data.frame() %>%
    select(city, state_name, dist_to_canada_border) %>%
    arrange(-dist_to_canada_border) %>%
    head(5),
  col.names = c("City", "State", "Distance to Canadian Border (km)"),
  caption = "US cities farthest from Canadian border"
)
US cities farthest from national border or coastline
City State Distance to USA Border (km)
Hill City Kansas 1035.632
Palco Kansas 1032.352
Dresden Kansas 1031.450
Jennings Kansas 1030.252
Bogue Kansas 1028.555
US cities farthest from respective state border
City State Distance to State Border (km)
Lampasas Texas 311.6903
Bertram Texas 306.8676
Kempner Texas 304.9356
Florence Texas 301.8986
Harker Heights Texas 300.6722
US cities farthest from Mexican border
City State Distance to Mexican Border (km)
Caribou Maine 3292.785
Presque Isle Maine 3277.282
Calais Maine 3181.785
Eastport Maine 3174.086
Old Town Maine 3090.254
US cities farthest from Canadian border
City State Distance to Canadian Border (km)
Guadalupe Guerra Texas 2255.590
Sandoval Texas 2254.783
Fronton Texas 2253.929
Fronton Ranchettes Texas 2251.062
Evergreen Texas 2250.952
ggplot() +
  geom_sf(data = mexicoBorder, lty = 2) +
  geom_sf(data = canadaBorder, lty = 2) +
  geom_sf(data = USAconus, size = 0.75) +
  geom_sf(data = USAcities %>%
               arrange(-population) %>%
               head(10)
          ) +
  geom_label_repel(data = USAcities %>%
                     arrange(-population) %>%
                     head(10), 
                   aes(label = city, geometry = geometry),
                   stat = "sf_coordinates",
                   size = 2,
                   point.padding = 0.5,
                   force = 2) +
  theme_map()


ggplot() +
  geom_sf(data = USAconus) +
  geom_sf(data = USAcities, aes(colour = dist_to_border), alpha = 0.25) +
  geom_sf(data = USAcities %>%
            arrange(-dist_to_border) %>%
            head(5), colour = "red") +
  geom_label_repel(data = USAcities %>%
                     arrange(-dist_to_border) %>%
                     head(5), 
                   aes(label = city, geometry = geometry),
                   stat = "sf_coordinates",
                   point.padding = 0.5,
                   force = 2) +
  scale_colour_viridis(option = "cividis",
                       direction = -1,
                       name = "Distance to US National Border",
                       guide = guide_colorbar(
                         direction = "horizontal",
                         barheight = unit(2, units = "mm"),
                         barwidth = unit(50, units = "mm"),
                         draw.ulim = F,
                         title.position = 'top',
                         title.hjust = 0.5,
                         label.hjust = 0.5
                         )) +
  labs(title = "Cities Farthest from US National Border") +
  theme_map()