Skip to content

Instantly share code, notes, and snippets.

@mikemahoney218
Created November 9, 2021 02:57
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 mikemahoney218/5dba6c0cab4de7c60edad4f482825580 to your computer and use it in GitHub Desktop.
Save mikemahoney218/5dba6c0cab4de7c60edad4f482825580 to your computer and use it in GitHub Desktop.
Code for my map for day 12 of the 2021 30 Day Map Challenge (November 12 2021: "Population")
library(sf)
library(tidycensus)
library(ggplot2)
library(dplyr)
acs_pop <- get_acs(
geography = "block group",
variables = "B01003_001",
state = "MA",
year = 2019,
geometry = TRUE
)
acs_pop <- acs_pop |>
mutate(area = units::drop_units(units::set_units(st_area(geometry), "miles^2")),
area_adjust = estimate / area,
area_adjust = area_adjust + 1)
ggplot(acs_pop) +
geom_sf(aes(fill = area_adjust), color = "black", size = 0.035) +
scale_fill_distiller(palette = "Purples", direction = 1, trans = "log", labels = scales::number_format(big.mark = ","), breaks = c(1, 10, 100, 1000, 10000), name = expression("Population density (# / mile"^2 ~ ")")) +
theme_void() +
theme(legend.position = "bottom",
legend.key.width = unit(0.6, "in"),
plot.background = element_rect(fill = "white", color = NA))
ggsave("20211112.png", width = 16, height = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment