Lab 7 - CHOLOPLETH USING LEAFLET

Race Trac Stores and Population Overlay

Author

Gustavo Maida

library(ggplot2)
library(ggthemes)
library(socviz)
library(maps)
library(mapproj)
library(viridis)

TEXAS POPULATION DATA BY COUNTY DIRECTLY FROM THE US CENSUS VIA API

head(as_tibble(tx_pop))
# A tibble: 6 × 6
  GEOID NAME                   variable  estim…¹   moe                  geometry
  <chr> <chr>                  <chr>       <dbl> <dbl>        <MULTIPOLYGON [°]>
1 48273 Kleberg County, Texas  B01003_0…   30860    NA (((-97.3178 27.49456, -9…
2 48391 Refugio County, Texas  B01003_0…    6718    NA (((-97.54085 28.16496, -…
3 48201 Harris County, Texas   B01003_0… 4726177    NA (((-94.97839 29.68365, -…
4 48443 Terrell County, Texas  B01003_0…     862   252 (((-102.5669 30.28327, -…
5 48229 Hudspeth County, Texas B01003_0…    3329    NA (((-105.998 32.00233, -1…
6 48205 Hartley County, Texas  B01003_0…    5369    NA (((-103.0422 35.82522, -…
# … with abbreviated variable name ¹​estimate

CHOLOPLETH USING LEAFLET FOR TEXAS COUNTIES

MapPalette <- colorQuantile(palette = "viridis", domain = tx_pop$estimate, n = 20)

library(sf)
tx_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 RACETRAC LOCATIONS IN TEXAS

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

racetrac <- racetrac %>% filter(state == "TX")


racetrac %>% leaflet(width = "100%") %>% 
             addTiles() %>% 
             setView(-94.0, 36.0, zoom = 8) %>% 
             addMarkers(lat = ~latitude, 
                                 lng = ~longitude, 
                                 popup = racetrac$street_address)

COMBINE CHOROPLETH AND LOCATIONS – OVERLAY RACETRAC LOCATIONS ONTO CHOROPLETH

MapPalette <- colorQuantile(palette = "viridis", domain = tx_pop$estimate, n = 20)


tx_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 = racetrac, 
                   lat = racetrac$latitude,
                   lng = racetrac$longitude,
                   popup = racetrac$street_address,
                   weight = 1,
                   radius=2,
                   color = "red", 
                   opacity = 1)

This first map only provides the location of the RaceTrac stations with their respective street adresses.

RACETRAC Locations Map 2

MapPalette <- colorQuantile(palette = "plasma", domain = tx_pop$estimate, n = 20)


tx_pop %>% 
  st_transform(crs = "+proj=longlat +datum=WGS84") %>% 
  leaflet(width = "100%", height = 500) %>% 
  addProviderTiles(provider = "OpenStreetMap") %>% 
  addPolygons(popup = ~NAME,
              stroke = TRUE,
              smoothFactor = 1,
              fillOpacity = 0.8,
              color = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) %>% 
addCircleMarkers(data = racetrac, 
                   lat = racetrac$latitude,
                   lng = racetrac$longitude,
                   popup = racetrac$zip_code,
                   weight = 1,
                   radius=2,
                   color = "green", 
                   opacity = 2)

This second map inlcudes more transparency and a street view of the state. I also adds a stroke to the counties.

RACETRAC Locations Map 3

MapPalette <- colorQuantile(palette = "inferno", domain = tx_pop$estimate, n = 20)


tx_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.5,
              color = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) %>% 
addCircleMarkers(data = racetrac, 
                   lat = racetrac$latitude,
                   lng = racetrac$longitude,
                   popup = racetrac$street_address,
                   weight = 2,
                   radius=2,
                   color = "blue", 
                   opacity = 0.8)  

This last map provides more transparency to have a better street view. In this map we could observe more RaceTrac locations in the highways, We could also see Ellis County and Fort Bend County have the most Racetrac Stations on Texas.