Skip to content

Instantly share code, notes, and snippets.

@charliejhadley
Created March 1, 2023 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliejhadley/81b836555b51fdb82a542f7f04afb06a to your computer and use it in GitHub Desktop.
Save charliejhadley/81b836555b51fdb82a542f7f04afb06a to your computer and use it in GitHub Desktop.
library(tigris)
library(sf)
library(rmapshaper)
library(readxl)
library(janitor)
library(leaflet)
library(tidyverse)
# ==== Streaming Data ====
# Data obtained from https://kiss951.com/2021/05/20/national-streaming-day-list-of-the-most-popular-streaming-services-in-each-state/
most_popular_streaming_service <- read_csv("data/most-popular-streaming-service.csv") %>%
clean_names()
# ==== States Data =====
us_contiguous <- states() %>%
clean_names() %>%
mutate(statefp = as.numeric(statefp)) %>%
filter(statefp < 60,
!statefp %in% c(2, 15)) %>%
ms_simplify()
us_most_popular_streaming_sf <- us_contiguous %>%
left_join(most_popular_streaming_service,
by = c("name" = "state"))
colors_services <- list(
"Amazon Prime" = "#2A96D9",
"ESPN" = "#BE0002",
"Hulu" = "#35B12E",
"Netflix" = "black"
)
order_streaming_service <- us_most_popular_streaming_sf %>%
st_drop_geometry() %>%
count(streaming_service, sort = TRUE) %>%
as_tibble() %>%
pull(streaming_service)
recoded_sf <- us_most_popular_streaming_sf %>%
mutate(streaming_service = fct_relevel(streaming_service, order_streaming_service))
pal_streaming <- colorFactor(unlist(colors_services[order_streaming_service]),
recoded_sf$streaming_service)
recoded_sf %>%
pull(streaming_service)
recoded_sf %>%
leaflet() %>%
addPolygons(fillColor = ~pal_streaming(streaming_service),
fillOpacity = 1) %>%
addLegend(pal = pal_streaming,
values = ~streaming_service,
opacity = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment