Skip to content

Instantly share code, notes, and snippets.

@rcatlord
Last active September 2, 2022 12:08
Show Gist options
  • Save rcatlord/2784e2866ce8351831fe264f9589a6a0 to your computer and use it in GitHub Desktop.
Save rcatlord/2784e2866ce8351831fe264f9589a6a0 to your computer and use it in GitHub Desktop.
Baby names in England & Wales, 1996-2020
library(tidyverse)

df <- read_csv("https://raw.githubusercontent.com/rcatlord/ddj/main/data/babynames1996to2020.csv")

selected_name <- "Alex"

df %>% 
  filter(name == selected_name) %>% 
  ggplot(aes(x = year, y = n)) +
  geom_line(aes(colour = sex), size = 1.5) +
  scale_x_continuous(breaks = seq(1996, 2020, 4)) +
  scale_y_continuous(position = "right", labels = scales::comma_format(accuracy = 1)) +
  scale_color_manual(values = c("F" = "#8700F9", "M" = "#00C4AA"),
                     labels = c("F" = "Girls", "M" = "Boys")) +
  labs(x = NULL, y = NULL,
       title = paste("Number of babies named", selected_name),
       subtitle = "England & Wales, 1996-2020",
       caption = "Source: Office for National Statistics",
       colour = NULL)  +
  theme_minimal(base_size = 14) +
  theme(plot.margin = unit(rep(1, 4), "cm"),
        panel.border = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line.x = element_line(),
        plot.title.position = "plot",
        plot.title = element_text(size = rel(1.2), face = "bold", hjust = 0, margin = margin(b = 7)),
        plot.subtitle = element_text(size = rel(0.85), hjust = 0),
        plot.caption = element_text(size = rel(0.8), color = "#777777", hjust = 0, margin = margin(t = 15)),
        legend.position = "top") +
  coord_cartesian(expand = FALSE, clip = "off")

ggsave(paste0(str_to_lower(selected_name), "_plot.png"), scale = 1, dpi = 300)

plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment