library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.3.2
library(socviz)
library(maps)
## 
## Attaching package: 'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
library(mapproj)
library(questionr)
library(viridis)
## Loading required package: viridisLite
## 
## Attaching package: 'viridis'
## 
## The following object is masked from 'package:maps':
## 
##     unemp
library(leaflet)
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.3.2

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/walmart-store-location-data\

costcoUS <- read.csv("https://query.data.world/s/rei24o74e6hoqboioyjzev6kmdhsoh?dws=00000", header=TRUE, stringsAsFactors=FALSE)

CREATE MAP FOR Illinois LOCATIONS

costcoUS <- costcoUS %>% filter(state == "IL")


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

ACQUIRE Illinois 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 COSTCO TO POPULATION

Let’s map the AR 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 = il_pop$estimate, n = 20)

il_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 = costcoUS, 
                   lat = costcoUS$latitude,
                   lng = costcoUS$longitude,
                   popup = costcoUS$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 is a map of the state of Illnois and the number of Costco stores in the state. All of the stores are in places where there is a large population percentile. They all seem to be off of a highway, probably for easy traveling to the store. All the stores are in the north of Illinois.

ADD ON Costcos LOCATIONS

Note – modified code slightly in the addPolygons() function to outline each county border in gray

custom_palette <- colorQuantile(palette = c("darkgreen", "yellow", "lightblue"), domain = il_pop$estimate, n = 10)

il_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "110%", height = 500) %>% 
  addProviderTiles(provider = "Esri.WorldStreetMap") %>% 
  setView(-89.45, 39.76, zoom = 8) %>% 
  addPolygons(popup = ~NAME,
              stroke = FALSE, 
              weight = 1, 
              smoothFactor = 0,
              fillOpacity = 0.6,
              color = ~ custom_palette(estimate)) %>% 
  addLegend("bottomright", 
            pal = custom_palette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) %>% 
  addCircleMarkers(data = costcoUS, 
                   lat = ~latitude,
                   lng = ~longitude,
                   popup = costcoUS,
                   weight = 1,
                   radius=4,
                   color = "blue", 
                   opacity = 1)

This helps show the stores better. The colors are more contrasting to one another, so it is easier to see the map. The lighter the color the more people there are in that county. The change in the legend also helps because it is more easier to see the rest of the map with the legend being smaller.