1 데이터셋

library(tidyverse)
library(tigris)
library(leaflet)
library(sf)

bank_df <- read_rds("data/bank.rds")

2 시각화

2.1 인터랙티브

library(glue)

bank_df %>% 
  leaflet() %>% 
    addProviderTiles(provider = providers$OpenStreetMap) %>% 
    addMarkers(lng=~location_lon, lat = ~location_lat, clusterOptions = markerClusterOptions(),
               popup = ~ as.character(glue("<strong> {offname} </strong> <br>
                                           &middot; Run Date: {rundate}")))

2.2 Shapefile

options(tigris_class = "sf")
options(tigris_use_cache = TRUE)
# tigris_cache_dir("/Users/statkclee/swc/tmp")

tx_counties <- counties(class = 'sf', state = 'TX', cb = TRUE, year = 2018)

leaflet(tx_counties) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 0.5)

2.3 static ggplot

bank_sf <- bank_df %>%
  st_as_sf(coords = c("location_lon", "location_lat"), crs = 4269)

bank_branches <- bank_df %>% count(county) %>%  pull(county)

tx_counties <- tx_counties %>% 
  mutate(branch_tf = NAME %in% bank_branches)

ggplot() +
  geom_sf(data = tx_counties, aes(fill = branch_tf)) +
  geom_point(data=bank_df, aes(x=location_lon, y=location_lat), 
             size = 2) +
  theme_bw() +
  labs(fill = "Spirit of Texas Branches")

2.4 지점과 shapefile {#shapefile+point}

leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(data = tx_counties,
              fillColor = "white",
              color = "black",
              weight = 0.5) %>% 
    addMarkers(data = bank_df, lng=~location_lon, lat = ~location_lat, 
               popup = ~ as.character(glue("<strong> {offname} </strong> <br>
                                           &middot; Run Date: {rundate}")))