LAB #7

Author

Jose Gabriel Rodriguez

library(ggplot2)
library(ggthemes)
library(socviz)
library(maps)
library(mapproj)
library(viridis)
Loading required package: viridisLite

Attaching package: 'viridis'
The following object is masked from 'package:maps':

    unemp

Lowes - Store Locations

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
✖ purrr::map()    masks maps::map()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidycensus)
library(leaflet)

library(stringr)

#census_api_key("3e87a14c2e4c3c095111a5b79353762d398009d1", install=TRUE)  # use this to install if you trust the device
census_api_key("3e87a14c2e4c3c095111a5b79353762d398009d1")  #use this one when rendering
To install your API key for use in future sessions, run this function with `install = TRUE`.
ar_pop <- 
  get_acs(geography = "county",
          variables = "B01003_001",
          state = "AR",
          geometry = TRUE)
Getting data from the 2018-2022 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
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, -…
MapPalette <- colorQuantile(palette = "viridis", domain = ar_pop$estimate, n = 20)

library(sf)
Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
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 LOWES LOCATIONS IN ARKANSAS

library(readxl)
lowesUS <- read_excel("LOWES.xlsx")

sbar <- lowesUS %>% filter(state=="AR")


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 LOWES 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)

More Maps

In this map, every county is supposed to have an unique color (I could not make it work). Arkansas map is divided by black lines to have a clear division of it. The whole state is surrounded by a division line to differentiate AR with the rest of the states. Each Lowes location is represented by a “pin” icon, and once clicked , it will display more information about the store, with counties as well.

library(tidyverse)
library(leaflet)

sbar <- lowesUS %>% filter(state == "AR")

ar_pop <- get_acs(
  geography = "county",
  variables = "B01003_001",
  state = "AR",
  geometry = TRUE
)
Getting data from the 2018-2022 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
ar_pop <- st_transform(ar_pop, crs = "+proj=longlat +datum=WGS84")

lowesUS <- read_excel("LOWES.xlsx")

sbar <- lowesUS %>% filter(state == "AR")

starIcon <- makeIcon(
  iconUrl = "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png",
  iconWidth = 24, iconHeight = 24,
  iconAnchorX = 12, iconAnchorY = 12,
  shadowUrl = "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png",
  shadowWidth = 24, shadowHeight = 24,
  shadowAnchorX = 12, shadowAnchorY = 12
)

county_palette <- colorFactor("viridis", domain = ar_pop$NAME)

# Create the leaflet map for Lowe's stores in Arkansas
map_lowes_stores <- leaflet() %>% 
  addProviderTiles(provider = "Esri.WorldPhysical") %>%  # Map tiles
  
  addPolygons(
    data = ar_pop %>% filter(NAME == "Arkansas"),  # Filter for Arkansas state
    fillColor = NA,
    color = "blue",
    weight = 2,
    opacity = 1,
    highlightOptions = highlightOptions(color = "blue", weight = 2),
    group = "Arkansas Border"
  ) %>% 
  
  addPolygons(
    data = ar_pop,
    fillColor = ~county_palette(NAME),
    color = "black",
    weight = 1,
    opacity = 0.8,
    fillOpacity = 0.7,
    group = "County Borders",
    popup = ~paste(
      "<b>", NAME, "</b><br>",
      "Population Estimate: ", estimate, "<br>"
    )
  ) %>%
  
  addMarkers(
    data = sbar,
    icon = starIcon,
    lat = ~latitude,
    lng = ~longitude,
    popup = ~paste(
      "<b>", name, "</b><br>",
      "<b>Address:</b> ", street_address, "<br>",
      "<b>City:</b> ", city, "<br>",
      "<b>State:</b> ", state, "<br>",
      "<b>Zip Code:</b> ", zip_code
    )
  ) %>%
  
  addLayersControl(
    overlayGroups = c("Arkansas Border", "County Borders"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  
  setView(lng = -92.0, lat = 34.8, zoom = 6)  # Set initial view for the map

map_lowes_stores