Skip to content

Instantly share code, notes, and snippets.

@arvi1000
Created April 29, 2019 22:30
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 arvi1000/df0b7689e32c85ba8e57523264b856cc to your computer and use it in GitHub Desktop.
Save arvi1000/df0b7689e32c85ba8e57523264b856cc to your computer and use it in GitHub Desktop.
library(babynames)
library(tidyverse)
babynames::babynames %>%
filter(name=='Arya') %>%
ggplot(aes(x=year, y=prop*100, color=sex)) +
geom_line(size=1) +
labs(title = 'Babies Named Arya',
subtitle = 'as a proportion of all births that year',
y='%') +
scale_color_manual(values = c(F='pink', M='dodgerblue'))
@arvi1000
Copy link
Author

arvi1000 commented May 2, 2019

Alternate version with supply-your-own regex:

library(babynames)
library(tidyverse)

name_regex <- 'Ramse?y'
babynames::babynames %>%
  filter(grepl(name_regex, name)) %>%
  group_by(year, sex) %>%
  summarise(prop=sum(prop)) %>%
  ggplot(aes(x=year, y=prop*100, color=sex)) +
  geom_line(size=1) +
  labs(title = paste('Babies regex matching:', name_regex), 
       subtitle = 'as a proportion of all births that year',
       y='%') +
  scale_color_manual(values = c(F='pink', M='dodgerblue'))

@arvi1000
Copy link
Author

arvi1000 commented May 2, 2019

Compare multiple names and label certain years with vertical lines:

library(babynames)
library(tidyverse)

babynames::babynames %>%
  filter(name %in% c('Katrina', 'Camille')) %>%
  group_by(year, name) %>%
  summarise(prop=sum(prop)) %>%
  ggplot(aes(x=year, y=prop*100, color=name)) +
  geom_vline(xintercept = c(1969,2005), linetype = 'dotted') +
  geom_line(size=1) +
  labs(title = 'Births by name', 
       subtitle = 'as a proportion of all births that year',
       y='%')

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