DAT-4313 - GIS with R

Subway and Population Overlay - With Bonus Lowe’s

Author

Erick Maldonado

ACQUIRE THE DATA

The following code will read the dataset in directly from data.world.

https://data.world/data-hut/subway-restaurant-location-dataset

# Read Subway dataset from data.world
subwayUS <- read.csv("https://query.data.world/s/xovevw74gngubs33ryyggldutjy42r?dws=00000", header=TRUE, stringsAsFactors=FALSE)

CREATE MAP FOR COLORADO LOCATIONS

Map Subway store locations just for CO using leaflet package.

# Filter Subway data for Colorado locations
subwayCO <- subwayUS %>% filter(state == "CO")

# Create leaflet map for Colorado Subway locations
subwayCO %>% leaflet(width = "100%") %>% 
  addTiles() %>% 
  setView(-105.5, 39.0, zoom = 6) %>%
  addMarkers(lat = ~latitude, 
             lng = ~longitude, 
             popup = subwayCO$name,
             clusterOptions = markerClusterOptions())

ACQUIRE COLORADO COUNTY-LEVEL POPULATION DATA FROM THE US CENSUS

OVERLAY SUBWAY TO POPULATION

Let’s map the CO Population Census using the leaflet package.

In this first map we can see the distribution of Subway restaurants across Colorado, however they look really close and clustered.

# Define color palette for map with light blue and ocean blue
MapPalette <- colorQuantile(palette = c("#ADD8E6","#0077be", "#151B54"), domain = co_pop$estimate, n = 20)

# Create leaflet map for Colorado population with Subway overlay and clustering
co_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "100%", height = 500) %>% 
  addProviderTiles(provider = "CartoDB.Positron") %>% 
  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 = subwayCO, 
                   lat = subwayCO$latitude,
                   lng = subwayCO$longitude,
                   popup = subwayCO$name,
                   weight = 1,
                   radius=4,
                   color = "black",  # Light Blue
                   opacity = 1,
                  )

In this second map we can now see the restaurants clustered in organized sections.

# Define color palette for map with light blue and ocean blue
MapPalette <- colorQuantile(palette = c("#ADD8E6","#0077be", "#151B54"), domain = co_pop$estimate, n = 20)

# Create leaflet map for Colorado population with Subway overlay and clustering
co_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "100%", height = 500) %>% 
  addProviderTiles(provider = "CartoDB.Positron") %>% 
  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 = subwayCO, 
                   lat = subwayCO$latitude,
                   lng = subwayCO$longitude,
                   popup = subwayCO$name,
                   weight = 1,
                   radius=4,
                   color = "black",  # Light Blue
                   opacity = 1,
                   clusterOptions = markerClusterOptions())

ADD ON LOWE’S LOCATIONS

# Read Lowe's dataset from data.world
lowesUS <- read.csv("https://query.data.world/s/pui32u64szpnbw7ewoobwybq3et2j4?dws=00000", 
                     header=TRUE, stringsAsFactors=FALSE)
# Filter Lowe's data for Colorado locations
lowesCO <- lowesUS %>% filter(state == "CO")

# Create leaflet map for Colorado population with Subway and Lowe's overlay
co_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "100%", height = 500) %>% 
  addProviderTiles(provider = "CartoDB.Positron") %>% 
  addPolygons(popup = ~NAME,
              stroke = TRUE,
              color = "gray",
              weight = 1,
              smoothFactor = 0,
              fillOpacity = 0.6,
              fillColor = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) %>% 
  addCircleMarkers(data = subwayCO, 
                   lat = subwayCO$latitude,
                   lng = subwayCO$longitude,
                   popup = subwayCO$name,
                   weight = 1,
                   radius=4,
                   color = "blue", 
                   opacity = 1) %>% 
  addCircleMarkers(data = lowesCO, 
                   lat = lowesCO$latitude,
                   lng = lowesCO$longitude,
                   popup = lowesCO$name,
                   weight = 1,
                   radius=4,
                   color = "red", 
                   opacity = 1)

Interpretations

Based on the code and results, here are a few key points:

  • The code is loading Colorado county population data from the US Census API using the tidycensus package.

  • Subway restaurant locations in Colorado are then mapped on top of the county polygons using leaflet’s addCircleMarkers. The subway locations are shown with blue circle markers.

  • In the final map, Lowe’s store locations in Colorado are also added, using red circle markers.

  • The maps overlay the geographic county polygons, colored by population percentile, with point locations of Subway and Lowe’s stores. This allows you to see the population distribution and density across counties in relation to the locations of these businesses.

  • More populous counties appear to have more Subway and Lowe’s locations, as you would expect. The maps allow you to see this relationship between population and store locations visually.

This allows geographic analysis of the relationship between population patterns and store locations. The interactive leaflet maps enable clear visualization of these spatial relationships.