Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(ggplot2)
- DT <- `[`
- jump_by <- function (x, by, start = 1, fill = NULL) {
- if (!is.null(fill)) {
- x[-seq.int(start, length(x), by = by)] <- fill
- }
- else {
- x <- x[seq.int(start, length(x), by = by)]
- }
- return(x)
- }
- totals <- babynames::babynames |>
- as.data.table() |>
- DT(, .(total = sum(n)), by = .(year))
- babynames::babynames |>
- as.data.table() |>
- DT(, ending := substr(name, nchar(name), nchar(name))) |>
- DT(ending %in% c("a", "e", "i", "o", "u", "y")) |>
- DT(, .(n = sum(n)), by = .(sex, year, ending)) |>
- DT(totals, on = c("year")) |>
- DT(, prop := n/total) |>
- ggplot(aes(year, prop)) +
- geom_line(data = \(x) x[, .(total = sum(prop)), by = .(ending, year)],
- aes(y = total), color = "gray40",
- size = 1) +
- geom_line(aes(color = sex), size = 1.5) +
- scale_color_manual("Sex", values = c("F" = "#F36F52",
- "M" = "#51C5DE"),
- labels = c("F" = "Female",
- "M" = "Male")) +
- scale_x_continuous(NULL, breaks = seq(1800, 2022, by = 10),
- labels = \(x) jump_by(x, 2, fill = "")) +
- scale_y_continuous(NULL, labels = scales::percent_format()) +
- expand_limits(y = 0) +
- theme_minimal(base_size = 20) +
- theme(panel.grid.minor = element_blank(),
- axis.ticks.x = element_line(color = "gray40")) +
- labs(title = "US Babies Receiving Names Ending in... ") +
- facet_wrap(~paste0("-", ending), scale = "free", ncol = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement