DVLab7

Subway and Population Overlay

Author

Abigail Russell

library(ggplot2)
library(ggthemes)
library(socviz)
library(maps)
library(mapproj)
library(viridis)
library(tidyverse)
library(tidycensus)
library(leaflet)

library(stringr)

census_api_key("2b86d958f8aa678b59489cf543adc5f1264ce053", overwrite  = TRUE)  

TX_pop <- 
  get_acs(geography = "county",
          variables = "B01003_001",
          state = "TX",
          geometry = TRUE)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
head(as_tibble(TX_pop))
# A tibble: 6 × 6
  GEOID NAME                   variable estimate   moe                  geometry
  <chr> <chr>                  <chr>       <dbl> <dbl>        <MULTIPOLYGON [°]>
1 48273 Kleberg County, Texas  B01003_…    30860    NA (((-97.3178 27.49456, -9…
2 48391 Refugio County, Texas  B01003_…     6718    NA (((-97.54085 28.16496, -…
3 48201 Harris County, Texas   B01003_…  4726177    NA (((-94.97839 29.68365, -…
4 48443 Terrell County, Texas  B01003_…      862   252 (((-102.5669 30.28327, -…
5 48229 Hudspeth County, Texas B01003_…     3329    NA (((-105.998 32.00233, -1…
6 48205 Hartley County, Texas  B01003_…     5369    NA (((-103.0422 35.82522, -…

Create a Cholopleth Using Leaflet for Arkansas Counties

MapPalette <- colorQuantile(palette = "viridis", domain = TX_pop$estimate, n = 20)

library(sf)
TX_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "100%", height = 500) %>% 
  addProviderTiles(provider = "Esri.WorldPhysical") %>% 
  addPolygons(popup = ~NAME,
              stroke = FALSE,
              smoothFactor = 0,
              fillOpacity = 0.7,
              color = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) 

In Texas there are some highly populated counties in the east side. These counties appear to be the Dallas and Austin areas where there are large cities. The western half of texas has a lower population proably due to the fact that that area is mainly farm land or desert.

Plotting Subway Locations

subway <- read.csv("subway_locations_in_us.csv")
head(subway)
  X                    title              address_1 address_2
1 0     Tikahtnu Commons S/C      1118 N Muldoon Rd Suite 115
2 1   1307 W Northern Lights 1307 W Northern Lights          
3 2          1330 Huffman Rd        1330 Huffman Rd          
4 3       2715 E. Tudor Road     2715 E. Tudor Road          
5 4 Alaska Regional Hospital         2801 Debarr Rd          
6 5          3104 Seward Hwy        3104 Seward Hwy          
                                                                                                                               open_hours
1       Closed Today    10:30 AM - 7:00 PM 10:30 AM - 7:00 PM 10:30 AM - 7:00 PM 10:30 AM - 8:00 PM 10:30 AM - 8:00 PM 10:30 AM - 7:00 PM
2         9:00 AM - 9:00 PM 9:00 AM - 9:00 PM 9:00 AM - 9:00 PM 9:00 AM - 9:00 PM 9:00 AM - 9:00 PM 10:00 AM - 9:00 PM 11:00 AM - 8:00 PM
3     8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 9:00 AM - 9:00 PM 10:00 AM - 8:00 PM
4  8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 10:00 AM - 10:00 PM 11:00 AM - 10:00 PM
5                               Closed Today   Closed Today   Closed Today   Closed Today   Closed Today   Closed Today   Closed Today   
6   8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 8:00 AM - 10:00 PM 10:00 AM - 10:00 PM 11:00 AM - 9:00 PM
       city state postal_code latitude longitude
1 Anchorage    AK       99504  61.2037 -149.7447
2 Anchorage    AK       99503  61.1900 -149.8938
3 Anchorage    AK       99515  61.1194 -149.8974
4 Anchorage    AK       99507  61.1535 -149.8289
5 Anchorage    AK       99508  61.2060 -149.8101
6 Anchorage    AK       99503  61.1900 -149.8938
tbar <- subway %>% filter(state=="TX")

tbar %>% leaflet(width = "100%") %>% 
             addTiles() %>% 
             setView(-99.0, 31.0, zoom = 3.5) %>% 
             addMarkers(lat = ~latitude, 
                                 lng = ~longitude, 
                                 popup = tbar$name)

Again we can see that the majority of Subways are in the eastern half of the state where the large cities are located. There are also a large number of Subway’s located in the state of Texas.

Combine Choropleth and Locations

MapPalette <- colorQuantile(palette = "viridis", domain = TX_pop$estimate, n = 20)


TX_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "100%", height = 500) %>% 
  addProviderTiles(provider = "Esri.WorldPhysical") %>% 
  addPolygons(popup = ~NAME,
              stroke = FALSE,
              smoothFactor = 0,
              fillOpacity = 0.7,
              color = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) %>% 
addCircleMarkers(data = tbar, 
                   lat = tbar$latitude,
                   lng = tbar$longitude,
                   popup = tbar$name,
                   weight = 1,
                   radius=4,
                   color = "white", 
                   opacity = 1)

In those yellow highly populated areas there are a lot of dots representing Subway’s located in that county.These (coming from the top down) are the Dallas Fort-Worth Area, the Austin area, the Houston area and the Rio Grand area. There is also a yellow county on the far west handle of Texas where the city of El Paso resides. Overall Texas is a large state with a lot of Subway locations especially in counties that have a population percentage of 90-100%