Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Last active March 17, 2025 13:16
Show Gist options
  • Save andrewheiss/54a6edeff6c51cd448d95c4c6ef5259c to your computer and use it in GitHub Desktop.
Save andrewheiss/54a6edeff6c51cd448d95c4c6ef5259c to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gutenbergr)
constitution_raw <- gutenberg_download(5)
constitution <- constitution_raw |>
slice(36:546) |>
filter(text != "") |>
mutate(text_lc = str_to_lower(text)) |>
mutate(is_article = str_starts(text_lc, "article")) |>
mutate(
article_number = cumsum(is_article),
article_number = ifelse(article_number == 0, "Preamble", as.character(as.roman(article_number))),
article_number = fct_inorder(article_number)
)
words_per_article <- constitution |>
mutate(words = stringi::stri_count_words(text_lc)) |>
group_by(article_number) |>
summarize(total_words = sum(words)) |>
mutate(
prop = total_words / sum(total_words),
prop_nice = scales::label_percent(accuracy = 0.1)(prop),
total_nice = scales::label_comma()(total_words)
) |>
mutate(label = paste0(total_nice, "\n(", prop_nice, ")"))
ggplot(words_per_article, aes(x = article_number, y = total_words)) +
geom_col(aes(fill = article_number == "I")) +
geom_text(
aes(label = label), nudge_y = 150,
size = 7, size.unit = "pt", family = "Public Sans", fontface = "bold"
) +
scale_y_continuous(labels = scales::label_comma()) +
# Colors from the USWDS
# https://designsystem.digital.gov/utilities/color/#basic-palette-2
scale_fill_manual(values = c("#0076d6", "#e52207"), guide = "none") +
labs(
x = NULL,
y = "Total words",
title = "More than half of the\nUS Constitution is in Article I"
) +
theme_minimal(base_family = "Public Sans") +
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
plot.title = element_text(face = "bold", hjust = 0.5)
)
ggsave("~/Desktop/words.png", width = 5, height = 3.5, units = "in", dpi = 300, bg = "white")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment