DAT-4313 - GIS with R

Whataburger Locations in Texas

Author

Dr. V, Pamela Carta

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/whataburger-restaurant-location-dataset

whataburguerUS <- read.csv("whataburger_2018_11_06.csv", header=TRUE, stringsAsFactors=FALSE)

CREATE MAP FOR TEXAS LOCATIONS

Map Whataburger store locations JUST FOR in TX using leaflet package.

whataUS <- whataburguerUS %>% filter(state == "TX")


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

ACQUIRE TEXAS 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.

OVERLAY WHATABURGER TO POPULATION

Let’s map the TX Population Census using the leaflet package.
This time we are using a different provider for the map: OpenStreetMap
Try swapping other maps – see a few below in the commented code.

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 Percentiles",
            opacity = 1) %>% 
  addCircleMarkers(data = whataUS, 
                   lat = whataUS$latitude,
                   lng = whataUS$longitude,
                   popup = whataUS$name,
                   weight = 1,
                   radius=4,
                   color = "red", 
                   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") %>% 

END