library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(babynames)
#glimpse(babynames)
#head(babynames)
#babynames %>% filter(name == "Emma", sex == "F") %>% tail()
#range((babynames$year))
shortlist <- tibble::tribble(
~name, ~sex,
"John", "M",
"Michael", "M",
"Betty", "F",
"Maria", "F",
"Marco", "M",
"Ida", "F",
)
ranked <- babynames %>%
group_by(year, sex) %>%
mutate(rank = min_rank(desc(n))) %>%
ungroup()
names_df <- ranked %>%
inner_join(shortlist, by = c("name", "sex")) %>%
filter(year >= 1900) %>%
mutate(per_1000 = prop * 1000, sex_label = ifelse(sex == "F", "girls", "boys" ))
plot_ly(names_df, x = ~year, y = ~per_1000, color = ~name,
text = ~paste0("<b>", name, "</b> (", year, ")",
"<br> Babies: ", format(n, big.mark = ","),
"<br>Per 1,000 ", sex_label, ": ", round(per_1000, 2),
"<br>Rank: #", rank),
hoverinfo = "text") %>%
add_lines()%>%
layout(title = "How popular are our picked names on the shortlist? from 1900 to 2017",
yaxis = list(title = "Babies per 1000 birth of same sex"),
xaxis = list(title = "Year"),
legend = list(title = list(text = "<b>Name</b>"))
)
The graph shows popularity between babies from 1900s to 2017. We can see how certain names are more popular at the beginning of the 1900s, and how some exploded during a certain period of time, and then they decline as you further go into the present. We can also see names like Maria, Ida and Marco are kind of are low in terms of ranks. However, they maintain a similar level of popularity throughout the years, not necessarily like any decline. As steep as Michael and John. And then you see Betty. Betty, takes a massive spike between 1920s to mid 1940s. It was, relatively popular. I wonder if it’s because of some TV show or some kind of toy. I showed my wife the graph, and she found it pretty interesting. Then she pointed out to me that Betty is probably a popular name during that period of time because of Betty Boop. Which was very popular cartoon during those times. My wife and i will trying to have children soon and i thought that using this data set would interesting to get ideas for names. The interesting part to me is just how many babies are named certain names that we can think of and how they rise or declined in popularity over time. like Michael and John are biblical names so it makes sense why they are rank so high in popularity early on, but decline as you get closer to the present. This is because religion following by the youth is lower in recent years. The challenges i had were the tilde(~) forgetting all of the time and getting the error.
unisex <- c("Addison" , "Adrian" , "Aiden" , "Ainsley" , "Alex" , "Alfie" , "Ali" , "Amory" , "Andie" , "Andy" , "Angel" , "Archer" , "Arden" , "Ari" , "Ariel" , "Armani" , "Arya" , "Ash" , "Ashley" , "Ashton" , "Aspen" , "Athena" , "Aubrey" , "Auden" , "August" , "Avery" , "Avis" , "Bailey" , "Baker" , "Bay" , "Bellamy" , "Bergen" , "Bevan" , "Billie" , "Billy" , "Blaine" , "Blair" , "Blake" , "Blue" , "Bobby" , "Bowie" , "Brady" , "Brennan" , "Brent" , "Brett" , "Briar" , "Brighton" , "Britton" , "Brooke" , "Brooklyn" , "Brooks" , "Caelan" , "Cameron" , "Campbell" , "Carey" , "Carmel" , "Carmen" , "Carroll" , "Carson" , "Carter" , "Casey" , "Cassidy" , "Chance" , "Channing" , "Charley" , "Charlie" , "Chris" , "Clay" , "Clayton" , "Cody" , "Cole" , "Corey" , "Dakota" , "Dale" , "Dallas" , "Dana" , "Dane" , "Darby" , "Daryl" , "Dawson" , "Delta" , "Denver" , "Devin" , "Dorian" , "Drew" , "Dylan" , "Easton" , "Eddie" , "Eden" , "Elliott" , "Ellis" , "Ellison" , "Ember" , "Emerson" , "Emery" , "Emory" , "Erin" , "Evelyn" , "Ezra" , "Finley" , "Finn" , "Florian" , "Flynn" , "Francis" , "Frankie" , "Gabriel" , "Gene" , "Genesis" , "George" , "Glen" , "Grey" , "Hadley" , "Harley" , "Harlow" , "Harper" , "Haven" , "Hayden" , "Henry" , "Honour" , "Hudson" , "Hunter" , "Indigo" , "Jack" , "Jackie" , "Jade" , "Jaden" , "James" , "Jamie" , "Jan" , "Jayden" , "Jean" , "Jesse" , "Jessie" , "Jody" , "Jordan" , "Jude" , "Jules" , "Julian" , "Kaden" , "Kai" , "Kameron" , "Keegan" , "Keely" , "Kelly" , "Kelsey" , "Kendall" , "Kennedy" , "Kerry" , "Kim" , "Kit" , "Kylar" , "Kyle" , "Kylin" , "Kyrie" , "Lake" , "Landon" , "Lane" , "Leah" , "Lee" , "Leighton" , "Lennon" , "Lennox" , "Leslie" , "Lincoln" , "Linden" , "Lindsay" , "Logan" , "London" , "Lonnie" , "Loren" , "Lou" , "Mackenzie" , "Maddox" , "Madison" , "Marley" , "Marlow" , "Mason" , "Max" , "Maxwell" , "Mckenna" , "Mckenzie" , "Micah" , "Michael" , "Milan" , "Miller" , "Monroe" , "Montana" , "Morgan" , "Murphy" , "Nevada" , "Nicky" , "Nico" , "Nikita" , "Noel" , "Noelle" , "Nolan" , "Nova" , "Ocean" , "Owen" , "Paige" , "Paisley" , "Paris" , "Parker" , "Pat" , "Payton" , "Peace" , "Perry" , "Peyton" , "Phoenix" , "Piper" , "Poet" , "Quincy" , "Quinn" , "Raleigh" , "Ramsey" , "Raphael" , "Ray" , "Rayne" , "Reagan" , "Reed" , "Reese" , "Regan" , "Remi" , "Remington" , "Remy" , "Rene" , "Riley" , "Rio" , "Ripley" , "River" , "Roan" , "Robin" , "Robyn" , "Rory" , "Rowan" , "Royal" , "Ryan" , "Ryder" , "Rylan" , "Sage" , "Sailor" , "Salem" , "Sam" , "Sasha" , "Sawyer" , "Sean" , "Shae" , "Shannon" , "Shawn" , "Shay" , "Shiloh" , "Sidney" , "Skye" , "Skylar" , "Sloane" , "Spencer" , "Sterling" , "Stevie" , "Storm" , "Sunny" , "Sutton" , "Sydney" , "Tanner" , "Tate" , "Tatum" , "Taylor" , "Teagan" , "Tennessee" , "Tennyson" , "Teri" , "Terry" , "Tiernan" , "Tobin" , "Toby" , "Toni" , "Tony" , "Tori" , "Tracy" , "Trinity" , "Tristan" , "True" , "Tyler" , "Umber" , "Val" , "Valentine" , "Venice" , "Vick" , "Wallace" , "Wesley" , "West" , "Weston" , "Whitney" , "Wilder" , "Windsor" , "Wynn" , "Winter" , "Wisdom" , "Wren" , "Wyatt" , "Zephyr")
mergenames <- babynames %>%
filter(name %in% unisex, year >= 1940) %>%
group_by(name, year) %>%
summarise(girls = sum(n[sex == "F"]),
boys = sum(n[sex == "M"]),
.groups = "drop") %>%
mutate(babies = girls + boys,
pct_girls = 100 * girls/babies)
total <- babynames %>% group_by(year) %>% summarise(total = sum(n))
mergenames <- mergenames %>%
filter(babies >= 100) %>%
left_join(total, by = "year") %>%
mutate(per_100k = 1e5 * babies / total)
trend <- mergenames %>%
group_by(name) %>%
summarise(change = last(pct_girls, order_by = year) -
first(pct_girls, order_by = year)) %>%
mutate(direction = cut(change, breaks = c(-Inf, -15,15,Inf), labels = c("Steer to Boys", "Little chanage","Steer to Girls")))
mergenames<- left_join(mergenames, trend, by = "name")
plot2 <- mergenames %>%
plot_ly(x= ~pct_girls, y = ~per_100k, frame = ~year, ids =~name,
color = ~direction,
colors = c("red","blue","grey"),
text = ~paste0("<b>", name, "</b> (", year, ")",
"<br>Girls: ", format(girls, big.mark = ","),
"<br>Boys: ", format(boys, big.mark = ","),
"<br>% girls: ", round(pct_girls, 1), "%",
"<br>Per 100,000 births:", round(per_100k, 1)),
hoverinfo = "text") %>%
add_markers(marker = list(size = 12, opacity = 0.85))
plot2 <- plot2 %>%
layout(
title = "Unisex Names: Change fro Boys to Girls from 1940 to 2017",
xaxis = list(title = "% of babies with this name who were girls",
range = c(-5,105), ticksuffix = "5"),
yaxis = list(title = "Babies per 100,000 births (Log)", type = "log"),
legend = list(title = list(text = "<b>Overall Trend</b>")),
shapes = list(list(type = "line",x0 = 50, x1=50,y0 = 0,y1 =1,
yref = "paper",
line = list(dash = "dot", color ="gray"))))
plot2 %>%
animation_opts(frame = 250, transition = 200, redraw = FALSE) %>%
animation_slider(currentvalue = list(prefix = "Year: ",
font = list(size = 26, color = "darkred"),
xanchor = "right")) %>%
animation_button(x = 0, xabchor = "left", y = -0.12)
Throughout the animation, the dots show how names swap between being male- or female-dominant among babies born each year. Names in the “little change” group tend to stay on one side, either the girl side or the boy side. Some of them cross over from time to time, but for the most part, they stay where they are. Other names steer toward the boys’ side, almost as if they are being claimed by that sex, and the same thing happens with names that steer toward the girls’ side. In certain years, some names are used much more for girls. For example, Taylor is a unisex name that is used fairly frequently, and toward the end of the 2000s, it becomes more dominant on the girls’ side. On the other hand, Kai is a common boys’ name that stays mostly on the boys’ side, even though some girls are also named Kai. My biggest challenge with this project was time. I’m in the middle of peak season at work, so I’m working a lot more hours than usual, and I ended up finishing closer to the deadline than I’d like. Working on it while tired also made it harder to absorb everything as I went, so I plan to go back over the code once things slow down.
Will Code Camp stay active after the course is over?