LAB 7 - GIS with R

Author

Jose Almanza

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

ACQUIRE THE DATA

The following code will read the dataset in directly from data.world.
You may need to join data.world, but I think it works without joining. (Joining is free.)

If you’d rather download and then import, here is the location of the dataset: https://data.world/data-hut/lowes-store-location-dataset.

LowesLocUS <- read.csv("lowes_2018_11_06.csv", header=TRUE, stringsAsFactors=FALSE)

CREATE MAP FOR ARKANSAS LOCATIONS

LowesAR <- LowesLocUS %>% filter(state == "AR")

LowesAR %>% leaflet(width = "100%") %>% 
             addTiles() %>% 
             setView(-92, 35.0, zoom = 6) %>%
             addMarkers(lat = ~latitude, 
                                 lng = ~longitude, 
                                 popup = LowesAR$name)

This map highlights all the spots where you can find Lowe’s stores across Arkansas. We notice something important about Lowe’s as they’ve got spots in every major city across Arkansas. It’s like they’re everywhere you need them to be. Little Rock and Fayeteville are the only two cities with two locations.

ACQUIRE ARKANSAS COUNTY-LEVEL POPULATION DATA FROM THE US CENSUS

Obtain your own census api key at: https://api.census.gov/data/key_signup.html
We will use the api key to directly download population data from the census.

library(tidyverse)
library(tidycensus)
library(leaflet)
library(sf)
library(stringr)

census_api_key("63a3f0b9c50705a1f4dfcdc5a377e04cd165358d")

ar_pop <- 
  get_acs(geography = "county",
          variables = "B01003_001",
          state = "AR",
          geometry = TRUE)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |======================================================================| 100%
as_tibble(ar_pop) 
# A tibble: 75 × 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, Ark… B01003_…    18619    NA (((-91.40687 36.49712, -…
 3 05013 Calhoun County, Arka… B01003_…     4773    NA (((-92.77672 33.53926, -…
 4 05061 Howard County, Arkan… B01003_…    12779    NA (((-94.2549 34.3462, -94…
 5 05099 Nevada County, Arkan… B01003_…     8292    NA (((-93.48322 33.47617, -…
 6 05103 Ouachita County, Ark… B01003_…    22606    NA (((-93.11638 33.45245, -…
 7 05007 Benton County, Arkan… B01003_…   286528    NA (((-94.61792 36.49941, -…
 8 05073 Lafayette County, Ar… B01003_…     6277    NA (((-93.86332 33.09885, -…
 9 05133 Sevier County, Arkan… B01003_…    15913    NA (((-94.47732 33.94095, -…
10 05021 Clay County, Arkansas B01003_…    14537    NA (((-90.80622 36.28812, -…
# ℹ 65 more rows

OVERLAY LOWES TO POPULATION

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.WorldStreetMap") %>% 
  addPolygons(popup = ~NAME,
              stroke = FALSE,
              smoothFactor = 0,
              fillOpacity = 0.6,
              color = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentage",
            opacity = 1) %>% 
  addCircleMarkers(data = LowesLocUS, 
                   lat = LowesLocUS$latitude,
                   lng = LowesLocUS$longitude,
                   popup = LowesLocUS$name,
                   weight = 1,
                   radius=4,
                   color = "blue", 
                   opacity = 1)
## Alternative maps  (just swap out the above)
#addProviderTiles(provider = "Esri.WorldStreetMap") %>% 
#addProviderTiles(provider = "OpenStreetMap") %>% 
#addProviderTiles(provider = "Esri.WorldPhysical") %>% 
#addProviderTiles(provider = "Esri.WorldImagery") %>% 
#addProviderTiles(provider = "Esri.WorldTopoMap") %>% 

This map reveals Lowe’s strategic location choices. Notice how they’ve cleverly positioned their stores in areas with the highest population percentages across Arkansas. It’s clear that they’ve honed in on where people are, ensuring there’s easy access to their stores in the most populous areas of the state. 

END