Question 1
We need to import 3 data sets, USA States, boundaries of NA, All US Cities
#Looking at distance, want to use equidistant projection
#we will use this proj 4 string as the equidistant projection
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.1 This says that the projection is equidistant and the lat and lon of origin is at 40,-96. The datum it is based off of is the North American Datum 1983, and the units used in the projection are meters.


#1.2 packages already installed at a previous date
#regional data (may not be useful)
region = data.frame(region= state.region,
          state_name = state.name)

#load in the USA continental states data
CONUS<- USAboundaries::us_states(resolution = "low") %>% 
  filter(!state_name %in% c("Alaska", "Hawaii", "Puerto Rico")) %>% 
  st_transform('+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.3  Get boundary for Mexico, US, Canada
boundary<- rnaturalearthdata::countries110 %>% 
  st_as_sf(crs=4269) %>% 
  filter(sovereignt %in% c("United States of America", "Canada", "Mexico"))%>%   st_transform(eqdc) 


#1.4 Get cities locations
cities<- readr::read_csv("../data/uscities.csv") %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>% 
  filter(!state_name %in% c("Hawaii", "Alaska", "Puerto Rico")) %>%   
  st_transform(eqdc) 
Question 2
finding distances
#need to union the CONUS data to find distance to the border of each city
#2.1 find distance from all cities to the border
CONUS_c<- CONUS %>% 
  st_union() %>% 
  st_cast("MULTILINESTRING") 
 
  
#find the distance from the unions CONUS to each city
  DisttoBorder<- cities %>% 
   mutate(BorderDist= st_distance(cities,CONUS_c)) %>% 
    mutate(BorderDist= units::set_units(BorderDist,"km")) %>% 
    select(city,state_name, BorderDist ) %>% 
    slice_max(BorderDist, n=5) %>% 
    st_drop_geometry()


#table of the 5 cities furthest from national border
  knitr::kable(DisttoBorder ,caption = "5 Cities Furthest From US National Border", col.names = c("City", "State", "Distance(km)"), format.args = list(big.mark = ","))
5 Cities Furthest From US National Border
City State Distance(km)
Dresden Kansas 1,012.317 [km]
Herndon Kansas 1,007.750 [km]
Hill City Kansas 1,005.147 [km]
Atwood Kansas 1,004.734 [km]
Jennings Kansas 1,003.646 [km]
#2.2 Distance to States 
  #Combine- keep states boundary
  CONUS_s<- CONUS %>% 
    st_combine() %>% 
    st_cast("MULTILINESTRING")

#find the distance to state boundary from each city
  DistToStateBord <- cities %>% 
    mutate(StateBordDist= st_distance(cities,CONUS_s), StateBordDist =    units::set_units(StateBordDist, "km")) %>% 
    select(city, state_name, StateBordDist) %>% 
    st_drop_geometry() %>% 
    slice_max(StateBordDist, n=5)
  
#table of the 5 cities furthest from the State border they are in
   knitr::kable(DistToStateBord ,caption = "5 Cities Furthest From A State Border", col.names = c("City", "State", "Distance(km)"), format.args = list(big.mark = ","))
5 Cities Furthest 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 
   #make data frame with only mexican border
   mexico_bord<- boundary %>% 
     filter(sovereignt %in% "Mexico") %>% 
      st_cast("MULTILINESTRING")
  
   #find distance from cities to mexican border
  distMex<- cities %>% 
    mutate(MexBordDist= st_distance(cities, mexico_bord), MexBordDist= units::set_units(MexBordDist, "km")) %>% 
    slice_max(MexBordDist, n=5) %>% 
    select(city,state_name, MexBordDist) %>% 
    st_drop_geometry()

#create table with 5 cities furthest from Mexico
  knitr::kable(distMex,caption = "5 Cities Furthest From Mexico", col.names = c("City", "State", "Distance(km)"), format.args = list(big.mark = ","))
5 Cities Furthest From Mexico
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 find the 5 cities furthest from canada 
  
  #create canada border data frame
  Canada_bord<- boundary %>% 
     filter(sovereignt %in% "Canada") %>% 
    st_cast("MULTILINESTRING")
  
 
  
  #find distance from cities to canada border
  distCanada<- cities %>% 
    mutate(CanDist<- st_distance(cities,Canada_bord), 
           CanDist= units::set_units(CanDist,"km")) %>% 
    slice_max(CanDist, n=5) %>% 
    select(city,state_name, CanDist) %>% 
    st_drop_geometry()
   
  #make table of the top 5 furthest cities from canada 
  knitr::kable(distCanada,caption = "5 Cities Furthest From Canada", col.names = c("City", "State", "Distance(km)"), format.args = list(big.mark = ","))
5 Cities Furthest From Canada
City State Distance(km)
Guadalupe Guerra Texas 2,206.455 [km]
Sandoval Texas 2,205.641 [km]
Fronton Texas 2,204.784 [km]
Fronton Ranchettes Texas 2,202.118 [km]
Evergreen Texas 2,202.020 [km]
  #Join the 4 distance calculations to the cities data.frame
 
     totalCities<- cities %>% 
    mutate(BorderDist= st_distance(cities,CONUS_c)) %>%  
  mutate(BorderDist= units::set_units(BorderDist,"km")) %>% 
       drop_units() %>% 
    mutate(StateBordDist= st_distance(cities,CONUS_s), StateBordDist =    units::set_units(StateBordDist, "km")) %>% 
       drop_units() %>%
     mutate(MexBordDist= st_distance(cities, mexico_bord), MexBordDist= units::set_units(MexBordDist, "km")) %>% 
       drop_units() %>%
      mutate(CanDist= st_distance(cities,Canada_bord), 
           CanDist= units::set_units(CanDist,"km")) %>%
       drop_units() %>%
       mutate(absDist = abs(CanDist-MexBordDist))
Question 3
Visualization
#3.1 display the data
 #3 continents, CONUS outline, state boundaries, and 10 largest USA cities (by population) on a single map

#create 10 biggest cities
BigCities<- cities %>% 
  slice_max(population, n=10)

#plot 
lab3AllDataPlot<- ggplot()+
  geom_sf(data = boundary$geometry, lty=3)+
  geom_sf(data = CONUS_c, col="blue", size = 1)+
  geom_sf(data = CONUS_s, col="red", size= 0.5)+
  geom_sf(data = BigCities, aes(size= population))+
  ggthemes::theme_map()+
  labs(title= "10 Largest Cities In The United States",
       caption = "Geog 176A map by Jacob Braslaw",
       legend = "Population Scale")+
 ggrepel::geom_label_repel(data = BigCities, aes(label=city, geometry= geometry), stat = "sf_coordinates", size= 3)+
  ggsave(plot = last_plot(), file= "../img/BigCitiesLab3.png")
  
lab3AllDataPlot

#3.2 City distance from the border

#create 10 cities furthest from the border
FarCity<- totalCities %>% 
  slice_max(BorderDist, n=5)

#make the plopt
CityDistPlot<- ggplot()+
  geom_sf(data = totalCities, aes(col=BorderDist),size= 0.1)+
  scale_color_gradient(low="gray", high = "pink", "Border Distance(km)")+
  geom_sf(data=FarCity, col="black", size= 1)+
  ggrepel::geom_label_repel(data = FarCity, aes(label=city, geometry= geometry), stat = "sf_coordinates", size= 3)+
  geom_sf(data = CONUS_c, col="black", size = 1)+
  labs(title= "5 Cities Furthest From The US Border")+
  ggthemes::theme_map()+
   ggsave(plot = last_plot(), file= "../img/FarCitiesLab3.png")
  
  CityDistPlot

#3.3 Cities furthest from their own state border
  #find the cities furthest from any state border
  FarStCity<- totalCities %>% 
    slice_max(StateBordDist, n=5)
  
  #plot
  StateDistPlot<- ggplot()+
    geom_sf(data = totalCities, aes(col=StateBordDist), size = 0.1)+
    scale_color_gradient(low=" grey", high = " blue", "State Border Dist(km)")+
    geom_sf(data = CONUS_s, col="black", size= 1)+
    geom_sf(data=FarStCity, col="black", size= 3)+
    ggrepel::geom_label_repel(data = FarStCity, aes(label=city, geometry= geometry), stat = "sf_coordinates", size= 3)+
      labs(title= "5 Cities Furthest From A State Border")+
   ggsave(plot = last_plot(), file= "../img/FarStateCitiesLab3.png")
  
   StateDistPlot

#3.4 find the cities that are equidistant from canada to mexico
   #create new variable looking at absolute difference in distance from canada to mexico 
   PopEquidistCity<- totalCities %>% 
     filter(absDist<=100) %>% 
     slice_max(population, n=5)
   #plot
   AbsDistCities<- ggplot()+
     geom_sf(data=totalCities, col="pink", size= 0.1)+
     gghighlight::gghighlight(absDist <= 100)+
     geom_sf(data=PopEquidistCity, col="blue", size =2)+
     geom_sf(data= CONUS_c, col= "black", size=1)+
     ggthemes::theme_map()+
     ggrepel::geom_label_repel(data = PopEquidistCity, aes(label=city, geometry= geometry), stat = "sf_coordinates", size= 3)+
      labs(title= "5 Most Populated US Cities Equidistant From Canada And Mexico", caption = "Pink region indicates all equidistant cities in the US")+
   ggsave(plot = last_plot(), file= "../img/EquidstantLab3.png")
     
   AbsDistCities

Question 4

Real World Application
#4.1 find how many cities are within 100 miles of the border
USpop<-cities %>% 
  summarise(USpopulation= sum(population))

BorderCities<- totalCities %>% 
  filter(BorderDist<=160) %>%
  mutate(totalCities= length(city)) %>% 
  group_by(city,state_name,county_fips, totalCities) %>% 
  summarise(totalPpl= sum(population)) %>% 
  ungroup() %>% 
  group_by(totalCities) %>% 
  summarise(totalPpl= sum(totalPpl)) %>% 
  mutate(PercentUSPop = 100*(totalPpl/  397213686)) %>% 
  st_drop_geometry()
  

# 4.1 make a table of the data found
 knitr::kable(BorderCities ,caption = "US Population Statistics: Land Within 100 Miles of US Border", col.names = c("Total Cities", "Total People", "% US Population"), format.args = list(big.mark = ","))
US Population Statistics: Land Within 100 Miles of US Border
Total Cities Total People % US Population
12,283 259,935,815 65.43979
#4.2 make a map of the us zone within the 100 mile from the border
#find most populated border cities
 PopBorderCities<- totalCities %>% 
   filter(BorderDist<=160) %>% 
   slice_max(population, n=10)
 
 
BorderPatrolCities<-
 ggplot()+
  geom_sf(data=totalCities, aes(col=BorderDist), size=0.1)+
  gghighlight::gghighlight(BorderDist<= 160)+
   scale_color_gradient(low= "Dark Red", high = "Orange", "Border Distance (km)")+
   geom_sf(data = CONUS_c, col= "black", size =0.25)+
   ggthemes::theme_map()+
   theme(legend.position="right")+
   geom_sf(data=PopBorderCities, aes(size=population), col = "black")+
   ggrepel::geom_label_repel(data=PopBorderCities, aes(label=city, geometry= geometry), stat = "sf_coordinates", size = 3)+
   labs(title = "10 Most Populated Cities Within 160km of US Border")+
   ggsave(plot = last_plot(), file= "../img/BorderPatrolCities.png")
BorderPatrolCities

#extra Credit label the most populated city within each state within the border region
stateBordCity<- totalCities %>% 
  group_by(state_name) %>% 
  filter(BorderDist<=160) %>% 
  slice_max(population, n=1)

#plot
ExtraCredit<-ggplot()+
  geom_sf(data=totalCities, aes(col=BorderDist), size=0.1)+
  gghighlight::gghighlight(BorderDist<= 160)+
   scale_color_gradient(low= "purple", high = "pink", "Border Distance (km)")+
   geom_sf(data = CONUS_c, col= "black", size =0.25)+
   ggthemes::theme_map()+
   theme(legend.position="right")+
   geom_sf(data=stateBordCity, aes(size=population), col = "black")+
   ggrepel::geom_label_repel(data=stateBordCity, aes(label=city, geometry= geometry), stat = "sf_coordinates", size = 3)+
   labs(title = "Most Populated Cities of Each Border State Within 160km of US Border")+
   ggsave(plot = last_plot(), file= "../img/StateBorderCity.png")

ExtraCredit