DAT-4313 - LAB 7

Map of State

Author

Andy Koerner

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

GET OKLAHOMA POPULATION DATA BY COUNTY DIRECTLY FROM THE US CENSUS VIA API

head(as_tibble(ok_pop))
# A tibble: 6 × 6
  GEOID NAME                   variable estimate   moe                  geometry
  <chr> <chr>                  <chr>       <dbl> <dbl>        <MULTIPOLYGON [°]>
1 40095 Marshall County, Okla… B01003_…    15494    NA (((-96.96963 34.07102, -…
2 40013 Bryan County, Oklahoma B01003_…    46528    NA (((-96.62975 33.85087, -…
3 40023 Choctaw County, Oklah… B01003_…    14285    NA (((-95.99154 34.1568, -9…
4 40027 Cleveland County, Okl… B01003_…   295060    NA (((-97.67146 35.3469, -9…
5 40147 Washington County, Ok… B01003_…    52579    NA (((-96.00114 36.43976, -…
6 40075 Kiowa County, Oklahoma B01003_…     8446    NA (((-99.40928 35.11693, -…

CREATE A CHOLOPLETH USING LEAFLET FOR OKLAHOMA COUNTIES

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

library(sf)
ok_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.8,
              color = ~ MapPalette(estimate)) %>% 
  addLegend("bottomright", 
            pal = MapPalette,
            values = ~ estimate,
            title = "Population Percentiles",
            opacity = 1) %>%
  setView(lng = -98, lat = 36, zoom = 6)  # Adjust the longitude, latitude, and zoom

This shows all the percentage for the states population in each county. Most of the people live in the top right area, but there are still plenty in the bottom right.

PLOT WHATABURGER LOCATIONS IN OKLAHOMA

# Load required libraries
library(readxl)
library(dplyr)
library(leaflet)


whataburger_data <- read.csv("M:/Data VIZ/LM 7/aLab7/whataburger_2018_11_06.csv")


# Filter for just AR
whataburger_OK <- whataburger_data %>% filter(state == "OK")

# Plot leaflet
whataburger_OK %>% leaflet(width = "100%") %>% 
  addTiles() %>% 
  setView(-97.0, 35.5, zoom = 7) %>% 
  addMarkers(lat = ~latitude, 
             lng = ~longitude, 
             popup = whataburger_OK$name,
             icon = makeIcon("what2.png", iconWidth = 30, iconHeight = 30))

This map shows all the Whataburgers in Oklahoma, where most seem to be near Tulsa or Oklahoma City.

COMBINE CHOROPLETH AND LOCATIONS – OVERLAY WHATABURGER LOCATIONS ONTO CHOROPLETH

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


ok_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) %>% 
  setView(lng = -96, lat = 36, zoom = 7) %>% 
addMarkers(data = whataburger_OK, 
                   lat = whataburger_OK$latitude,
                   lng = whataburger_OK$longitude,
                   popup = whataburger_OK$name,
                    icon = makeIcon("what2.png", iconWidth = 30, iconHeight = 30)
                   )

This map shows the percentages with the Whataburgers, and they are only in the high percentages.