DAT-4313 - GIS with R

Starbucks and Population Overlay

Author

Dr. V

library(ggplot2)
library(ggthemes)
library(socviz)
library(maps)
library(mapproj)
library(viridis)

GET ARKANSAS POPULATION DATA BY COUNTY DIRECTLY FROM THE US CENSUS VIA API

head(as_tibble(ar_pop))
# A tibble: 6 × 6
  GEOID NAME                   variable estimate   moe                  geometry
  <chr> <chr>                  <chr>       <dbl> <dbl>        <MULTIPOLYGON [°]>
1 05081 Little River County, … B01003_…    12024    NA (((-94.48558 33.65331, -…
2 05121 Randolph County, Arka… B01003_…    18619    NA (((-91.40687 36.49712, -…
3 05013 Calhoun County, Arkan… B01003_…     4773    NA (((-92.77672 33.53926, -…
4 05061 Howard County, Arkans… B01003_…    12779    NA (((-94.2549 34.3462, -94…
5 05099 Nevada County, Arkans… B01003_…     8292    NA (((-93.48322 33.47617, -…
6 05103 Ouachita County, Arka… B01003_…    22606    NA (((-93.11638 33.45245, -…

CREATE A CHOLOPLETH USING LEAFLET FOR ARKANSAS COUNTIES

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

library(sf)
ar_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) 

PLOT STARBUCKS LOCATIONS IN ARKANSAS

# load all USA Starbucks locations
load("starbucksUS.RData")
head(starbucksUS)
# A tibble: 6 × 6
  name                         city    state country latitude longitude
  <chr>                        <chr>   <chr> <chr>      <dbl>     <dbl>
1 Val Vista & Baseline, Mesa   Mesa    AZ    US          33.4     -112.
2 Val Vista & Southern, Mesa   Mesa    AZ    US          33.4     -112.
3 Safeway - Mesa #1637         Mesa    AZ    US          33.4     -112.
4 Greenfield & Inverness, Mesa Mesa    AZ    US          33.4     -112.
5 Safeway - Gilbert #1748      Gilbert AZ    US          33.4     -112.
6 Gilbert & Baseline           Gilbert AZ    US          33.4     -112.
# filter for just AR
sbar <- starbucksUS %>% filter(state=="AR")

# dataset is old -- let's add Siloam Springs Starbucks manually:)
sbar <- sbar %>% 
  add_row(name="Highway 412", city="Siloam Springs", state="AR", country="US",
          latitude=36.181498, longitude=-94.507065)

# plot leaflet
sbar %>% leaflet(width = "100%") %>% 
             addTiles() %>% 
             setView(-94.0, 36.0, zoom = 8) %>% 
             addMarkers(lat = ~latitude, 
                                 lng = ~longitude, 
                                 popup = sbar$name)

COMBINE CHOROPLETH AND LOCATIONS – OVERLAY STARBUCKS LOCATIONS ONTO CHOROPLETH

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


ar_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 = sbar, 
                   lat = sbar$latitude,
                   lng = sbar$longitude,
                   popup = sbar$name,
                   weight = 1,
                   radius=4,
                   color = "green", 
                   opacity = 1)