Babynames Assignment

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── 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.1
✔ 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)
Warning: package 'gt' was built under R version 4.2.3
library(wordcloud2)
Warning: package 'wordcloud2' was built under R version 4.2.3
library(babynames)
Warning: package 'babynames' was built under R version 4.2.3
  1. Determine your name’s rank in the year you were born.
babynames |>                             
  filter(year == 2003, sex == "F") |>    
  mutate(rank = row_number()) |>         
  mutate(percent = round(prop * 100, 1)) |> 
  filter(name == "Hailey") |> 
  gt()
year sex name n prop rank percent
2003 F Hailey 8793 0.00438465 26 0.4
  1. Create a word cloud of the names of your sex from the year you were born.
babynames |>
  filter(year == 2003) |>     # use only one year
  filter(sex == "F") |>       # 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, shape = "circle", color = "pink")                 # generate the word cloud
  1. Graph its popularity over time.
babynames |>                                    # start with the data
  filter(name == "Hailey", sex == "F") |>      # 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 = "green")                      # make it a line graph and give the line a color

  1. Create a table showing which years it was most popular.
babynames |>                                  # Start with the dataset
  filter(name == "Hailey", sex == "F") |>    # 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  2003 F     Hailey  8793 0.00438
 2  2004 F     Hailey  8683 0.00431
 3  2005 F     Hailey  8589 0.00423
 4  2001 F     Hailey  8030 0.00406
 5  2002 F     Hailey  7991 0.00405
 6  2006 F     Hailey  8421 0.00403
 7  2007 F     Hailey  8433 0.00399
 8  2000 F     Hailey  7831 0.00393
 9  2008 F     Hailey  7866 0.00378
10  2009 F     Hailey  7314 0.00362
  1. Graph its popularity in comparison to another name or two.
babynames |>
  filter(name == "Hailey" | name == "Ellie" | name == "Terran") |> 
  filter(sex == "F") |> 
  filter(year > 2000) %>% 
  ggplot(aes(x = year, y = n, color = name)) +
  geom_line()

  1. Select a few famous people’s names from different time periods. Graph their pattern of popularity over time.

Stevie Nicks

babynames |>
  filter(name == "Stevie") |> 
  filter(sex == "F") %>% 
  ggplot(aes(x = year, y = n)) +
  geom_line()

Post Fleetwood Mac breakup

babynames |>
  filter(name == "Stevie") |> 
  filter(sex == "F") %>% 
  filter(year > 1975) %>% 
  ggplot(aes(x = year, y = n)) +
  geom_line()

Fleetwood Mac split up in the mid 1970s. The plot shows that the name Stevie for a female hit a peak in the late 1980s.

Johnny Depp

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

Post Pirates of the Carribean

babynames |>
  filter(name == "Johnny") |> 
  filter(sex == "M") %>% 
  filter(year > 2003) %>% 
  ggplot(aes(x = year, y = n)) +
  geom_line()

From the first graph above, it shows that the name JOhnny hit its peak in about the 1950s. We can see from this graph directly above that there was a spike in about 2007 and again in 2013. There were films made in 2007 and 2011.