data = ("../data/uscities.csv")
us_cities = read_csv(data)
Question 1:
#1.1
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'
#1.2: US State Boundaries
conus = USAboundaries::us_states(resolution = "low") %>%
filter(!state_name %in% c("Puerto Rico", "Alaska", "Hawaii")) %>%
st_transform(eqdc)
#1.3:North America Country Boundaries
nat_earth = rnaturalearthdata::countries110
country_bound = st_as_sf(nat_earth) %>%
filter(admin %in% c("United States", "Mexico","Canada")) %>% st_transform(eqdc)
#1.4: City Locations
locations = us_cities %>%
st_as_sf(coords = c("lng", "lat"), crs= 4326)%>%
filter(!state_name %in% c("Puerto Rico", "Alaska", "Hawaii")) %>%
st_transform(eqdc)
Question 2:
# 2.1:Distance to US Borders (km)
cb_u_ml <- conus %>%
st_union() %>%
st_cast("MULTILINESTRING")
us_borders <- locations %>%
mutate(border_dist = st_distance(locations, cb_u_ml),
border_dist = units::set_units(border_dist, 'km'),
border_dist = units::drop_units(border_dist))
fur_border = us_borders %>%
slice_max(border_dist, n = 5) %>%
select(city, state_name, border_dist) %>%
st_drop_geometry()
#us_borders_df <- as.data.frame(top_us_borders) %>% #select(city, state_name, border_dist)
knitr::kable(fur_border, caption = 'Top 5 Cities Furthest to USA Border',
col.names = c("City", "State", "Distance (km)"),
format.args = list(big.mark = ",")) %>%
kableExtra::kable_styling("striped", full_width = TRUE, font_size = NULL)
## Warning in kableExtra::kable_styling(., "striped", full_width = TRUE, font_size
## = NULL): Please specify format in kable. kableExtra can customize either HTML or
## LaTeX outputs. See https://haozhu233.github.io/kableExtra/ for details.
Top 5 Cities Furthest to USA Border
Dresden |
Kansas |
1,012.317 |
Herndon |
Kansas |
1,007.750 |
Hill City |
Kansas |
1,005.147 |
Atwood |
Kansas |
1,004.734 |
Jennings |
Kansas |
1,003.646 |
#2.2:Distance to States (km)
cb_c_ml <- conus %>%
st_combine() %>%
st_cast("MULTILINESTRING")
us_borders <- locations %>%
mutate(border_state = st_distance(locations, cb_c_ml),
border_state = units::set_units(border_state, 'km')) %>%
select(city, state_name, border_state) %>%
st_drop_geometry() %>%
slice_max(border_state, n = 5)
knitr::kable(us_borders, caption = 'The Five Cities Farthest from a State Border',
col.names = c("City", "State", "Distance (km)"),
format.args = list(big.mark = ",")) %>%
kableExtra::kable_styling("striped", full_width = TRUE, font_size = NULL)
The Five Cities Farthest from a State Border
City
|
State
|
Distance (km)
|
Lampasas
|
Texas
|
308.9216 [km]
|
Bertram
|
Texas
|
302.8190 [km]
|
Kempner
|
Texas
|
302.5912 [km]
|
Harker Heights
|
Texas
|
298.8125 [km]
|
Florence
|
Texas
|
298.6804 [km]
|
#2.3: Distance to Mexico (km)
mexico <- country_bound %>%
filter(sovereignt == "Mexico")
mx_cities <- locations %>%
mutate(mx_border = st_distance(locations, mexico),
mx_border = set_units(mx_border, 'km')) %>%
select(city, state_name, mx_border) %>%
slice_max(mx_border, n = 5) %>%
arrange(-mx_border)
mx_df <- as.data.frame(mx_cities) %>%
select(city, state_name, mx_border)
knitr::kable(mx_df, caption = "Five Cities Farthest from Mexico's Border",
col.names = c("City", "State", "Distance (km)"),
format.args = list(big.mark = ",")) %>%
kableExtra::kable_styling("striped", full_width = TRUE, font_size = NULL)
Five Cities Farthest from Mexico’s Border
City
|
State
|
Distance (km)
|
Caribou
|
Maine
|
3,250.334 [km]
|
Presque Isle
|
Maine
|
3,234.570 [km]
|
Calais
|
Maine
|
3,134.348 [km]
|
Eastport
|
Maine
|
3,125.624 [km]
|
Old Town
|
Maine
|
3,048.366 [km]
|
#2.4:Distance to Canada (km)
canada <- country_bound %>%
filter(sovereignt == "Canada")
can_cities <- locations %>%
mutate(can_border = st_distance(locations, canada),
can_border = set_units(can_border, 'km'),
can_border = drop_units(can_border)) %>%
select(city, state_name, can_border) %>%
slice_max(can_border, n = 5) %>%
arrange(-can_border)
can_df <- as.data.frame(can_cities) %>%
select(city, state_name, can_border)
knitr::kable(can_df, caption = "Five Cities Farthest from Canada's Border",
col.names = c("City", "State", "Distance (km)"),
format.args = list(big.mark = ",")) %>%
kableExtra::kable_styling("striped", full_width = TRUE, font_size = NULL)
Five Cities Farthest from Canada’s Border
City
|
State
|
Distance (km)
|
Guadalupe Guerra
|
Texas
|
2,206.455
|
Sandoval
|
Texas
|
2,205.641
|
Fronton
|
Texas
|
2,204.784
|
Fronton Ranchettes
|
Texas
|
2,202.118
|
Evergreen
|
Texas
|
2,202.020
|
Question 3
#3.1: Data
big_cities = locations %>%
slice_max(population, n=10)
ggplot() +
geom_sf(data = country_bound, col= "black") +
geom_sf(data = conus, col= "black") +
geom_sf(data = big_cities, col= "red", size = 2)+
ggrepel::geom_label_repel(data = big_cities,
aes(label = city, geometry = geometry),
stat = "sf_coordinates", size= 3)+
labs(title = '10 Most Populated US cities',
x = '',
y = '')
#3.2: City Distance from the Border
far_cities = fur_border %>%
slice_max(border_dist, n=5)
plot_far_cities <-ggplot() +
geom_sf(data = conus) +
geom_sf(data = us_borders, aes(col = border_dist), size = .1) +
geom_sf(data = far_cities) +
scale_color_gradient(low = "gray", high = "red") +
geom_label_repel(data = far_cities, aes( label = city, geometry = geometry),
stat = 'sf_coordinates') +
labs(title = 'Cities Farthest from the US Border',
x = 'Longitude',
y = 'Latitude') +
theme_gray()
#3.3: City Distance from Nearest State
near_state <- us_borders %>%
slice_max(border_state, n=5)
near_plot = ggplot() +
geom_sf(data = conus) +
geom_sf(data = us_borders, aes(col = border_state), size = .1) +
geom_sf(data = near_state) +
scale_color_gradient(low = "gray", high = "red") +
geom_label_repel(data = near_state, aes( label = city, geometry = geometry),
stat = 'sf_coordinates') +
labs(title = 'Cities Farthest from the US Border',
x = '',
y = '') +
theme_gray()
#3.4: Equidistance boundary from Mexico and Canada
Question 4
#4.1:Quantifing Border Zone
qbz <- locations %>%
mutate(sum_pop = sum(population)) %>%
mutate(zones = st_distance(locations, cb_u_ml),
zones = units::set_units(zones, "km"),
zones = units::drop_units(zones)) %>%
filter(zones <= 160) %>%
summarise(bor_pop = sum(population), bor_per = (bor_pop/ sum_pop)*100, n_cities = n()) %>%
head(1) %>%
st_drop_geometry()
knitr::kable(qbz,
caption = 'Quantifing Border Zone',
col.names = c('Border Zone Population', 'Percent of Total US population', 'Number of Cities'),
format.args = list(big.mark = ",")) %>%
kableExtra::kable_styling("striped", full_width = TRUE, font_size = NULL)
Quantifing Border Zone
Border Zone Population
|
Percent of Total US population
|
Number of Cities
|
259,935,815
|
65.43979
|
12,283
|
#4.2: Mapping Border Zone
border_zone <- locations %>%
st_as_sf(coords = c('lng', 'lat'), crs = 4326) %>%
mutate(dist_bor = st_distance(locations, cb_u_ml),
dist_bor = units::set_units(dist_bor, 'km'),
dist_bor = units::drop_units(dist_bor)) %>%
filter(dist_bor <= 160) %>%
select(city, state_name, dist_bor, population)
most_pop <- border_zone %>%
select(state_name, population, city) %>%
arrange(-population) %>%
head(10)
ggplot() +
geom_sf(data = conus) +
geom_sf(data = locations, col = 'grey', size = .1) +
geom_sf(data = border_zone, aes(col = dist_bor), size = .5) +
gghighlight(dist_bor <= 160) +
scale_color_gradient(low = 'orange', high = 'darkred', name = "Distance (km)") +
geom_label_repel(data = most_pop, aes(label = city, geometry = geometry), stat = 'sf_coordinates') +
labs(title = "Cities Within 100 km of the US border",
x = '',
y = '') +
theme_gray()
## Warning: Could not calculate the predicate for layer 1, layer 2; ignored