library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gt)
library(wordcloud2)
library(babynames)
babynames |>                             
  filter(year == 1991, sex == "M") |>    
  mutate(rank = row_number()) |>         
  mutate(percent = round(prop * 100, 1)) |> 
  filter(name == "Joseph") |> 
  gt()
year sex name n prop rank percent
1991 M Joseph 29835 0.01407848 9 1.4
babynames |>
  filter(year == 1991) |>     # use only one year
  filter(sex == "M") |>       # use only one sex
  slice_max(prop, n=100) |>   # use the top 100 names
  select(name, n) |>          # select the two relevant variables: the name and how often it occurs
  wordcloud2(size = .5) + WCtheme(2)                 # generate the word cloud
babynames |>                                    # start with the data
  filter(name == "Joseph", sex == "M") |>      # choose the name and sex
  mutate(percent = round(prop * 100, 1)) |>     # create a new variable called percent
  ggplot(aes(x = year, y = percent)) +           # put year on the x-axis and prop (proportion) on y
  geom_line(color = "blue")                      # make it a line graph and give the line a color

babynames |>                                  # Start with the dataset
  filter(name == "Joseph", sex == "M") |>    # only look at the name and sex you want
  slice_max(prop, n = 10)                         # get the top 10 names
# A tibble: 10 × 5
    year sex   name       n   prop
   <dbl> <chr> <chr>  <int>  <dbl>
 1  1914 M     Joseph 18829 0.0276
 2  1913 M     Joseph 14471 0.0270
 3  1911 M     Joseph  6488 0.0269
 4  1912 M     Joseph 12062 0.0267
 5  1915 M     Joseph 23052 0.0262
 6  1916 M     Joseph 23883 0.0259
 7  1917 M     Joseph 24762 0.0258
 8  1910 M     Joseph  5228 0.0251
 9  1918 M     Joseph 26263 0.0250
10  1908 M     Joseph  4162 0.0250
babynames |>
  filter(name == "Joseph" | name == "Christopher", sex == "M") |>  
  mutate(percent = round(prop * 100, 1)) |>  
  ggplot(aes(x = year, y = percent, color = name)) +
  geom_line()

babynames |>
  filter(name == "Will" | name == "Johnny" | name == "Arnold") |> 
  filter(sex == "M") |>
  filter(year > 1980) |>
  ggplot(aes(x = year, y = n, color = name)) +
  geom_line()