Skip to content

Instantly share code, notes, and snippets.

@mikemahoney218
Created November 15, 2020 21:56
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/58995376deb87ac052a65bdd50930117 to your computer and use it in GitHub Desktop.
Save mikemahoney218/58995376deb87ac052a65bdd50930117 to your computer and use it in GitHub Desktop.
Code to create a map for the #30DayMapChallenge day 20: population
library(ggplot2)
library(dplyr)
library(tigris)
library(tidycensus)
abbr <- state.abb[-c(2, 11)]
state_dat <- vector("list", length(abbr))
names(state_dat) <- abbr
for (i in seq_len(length(state_dat))) {
state_dat[[i]] <- get_estimates("county",
"population",
geometry = TRUE,
state = names(state_dat)[[i]]
)
}
state_pops <- do.call(rbind, state_dat)
state_dens <- filter(state_pops, variable == "DENSITY")
county_line <- counties(abbr, TRUE)
state_dens %>%
arrange(value) %>%
mutate(idx = row_number()) %>%
ggplot() +
geom_sf(aes(fill = idx), color = NA) +
geom_sf(data = county_line,
fill = NA,
color = "#F0F0F0",
size = 0.03,
alpha = 0.05) +
scale_fill_gradientn(colors = viridisLite::magma(50)) +
coord_sf() +
theme_void() %+replace%
theme(
legend.position = "none",
plot.background = element_rect(fill = "#2c2c2c")
)
ggsave("20_US_pop.png", width = 17, height = 9, dpi = 600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment