Skip to content

Instantly share code, notes, and snippets.

@JamshidSod
Forked from walkerke/mapchallenge_day2_lines.R
Created November 2, 2022 16:28
Show Gist options
  • Save JamshidSod/e335277fa6de626178b737e1ed470185 to your computer and use it in GitHub Desktop.
Save JamshidSod/e335277fa6de626178b737e1ed470185 to your computer and use it in GitHub Desktop.
library(tigris)
library(mapboxapi)
library(gganimate)
library(tidyverse)
library(sf)
library(showtext)
options(tigris_use_cache = TRUE)
animation::ani.options(ani.width = 700, ani.height = 550)
font_add_google("Roboto", "Roboto")
showtext_auto()
cook_roads <- roads("IL", "Cook")
chicago_hall <- mb_geocode("121 N La Salle St, Chicago, IL", output = "sf") %>%
st_transform(st_crs(cook_roads))
cook_roads$dist <- as.numeric(st_distance(cook_roads, chicago_hall))
cook_roads$dist_km <- cook_roads$dist / 1000
cook_roads$dist_round <- round(cook_roads$dist, -3)
ggplot(cook_roads, aes(color = dist_km)) +
geom_sf(linewidth = 0.1) +
theme_void(base_family = "Roboto", base_size = 10) +
scale_color_viridis_c(option = "plasma") +
labs(color = "Distance (km) ",
title = "Cook County roads by distance to Chicago City Hall",
subtitle = "@kyle_e_walker | tigris / gganimate R packages",
caption = "") +
theme(legend.position = "bottom", legend.direction = "horizontal",
legend.key.width = unit(1.5, "cm")) +
transition_states(dist_round) +
shadow_mark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment