Skip to content

Instantly share code, notes, and snippets.

@k5cents
Last active February 4, 2021 18:02
Show Gist options
  • Save k5cents/6380d6107d70c5fd90ae322681f7dabe to your computer and use it in GitHub Desktop.
Save k5cents/6380d6107d70c5fd90ae322681f7dabe to your computer and use it in GitHub Desktop.
Finding the first letter of each state's median county
# install.packages("usa")
library(tidyverse)
x <- usa::counties %>%
filter(state %in% state.abb) %>%
group_by(state) %>%
arrange(name, .by_group = TRUE) %>%
mutate(first_letter = str_sub(name, end = 1)) %>%
filter(first_letter == first_letter[length(first_letter)/2]) %>%
slice(1) %>%
mutate(place = as.factor(first_letter))
x$first_letter <- factor(
x = x$first_letter,
levels = LETTERS
)
x %>%
arrange(first_letter) %>%
ggplot(aes(x = reorder(state, as.integer(first_letter)), y = first_letter)) +
geom_col(aes(fill = first_letter)) +
scale_fill_viridis_d(guide = FALSE, end = 0.9) +
geom_text(aes(label = name, y = 3), nudge_y = -1) +
labs(
x = "State",
y = "Median County First Letter",
title = "States by median county first letter",
caption = "Source: https://www.nrcs.usda.gov/wps/portal/nrcs/detail/national/home/?cid=nrcs143_013697"
) +
coord_flip()
ggsave(
filename = "~/Pictures/med_county.png",
width = 6,
height = 12,
dpi = "retina"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment