-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbabynames.Rmd
158 lines (121 loc) · 4.78 KB
/
babynames.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: "baby names"
author: "Rose Hartman"
date: "June 5, 2016"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(babynames)
library(ggplot2)
library(dplyr)
```
```{r select_names}
classic.data <- babynames %>%
filter(name %in% c("Eugene", "Baby", "Kellie", "Jason", "John", "Emma", "Nicholas", "Dorothy")) %>%
filter(year > 1900) %>%
mutate(category="classic")
virtue.data <- babynames %>%
filter(name %in% c("Chastity", "Honor", "Justice", "Faith", "Patience", "Earnest", "Benedict", "Noble")) %>%
filter(year > 1900) %>%
mutate(category="virtue")
famous.data <- babynames %>%
filter(name %in% c("Bella", "Hillary", "Bernard", "Donald", "Barack", "Buffy", "Angel", "Edward")) %>%
filter(year > 1900) %>%
mutate(category="famous")
plot.data <- rbind(classic.data, virtue.data, famous.data)
```
```{r disguise_names}
disguise_names <- function(name.df){
num.names <- length(unique(name.df$name))
placeholders <- paste0("Name ", 1:num.names)
if("placeholder" %in% colnames(name.df)) {
name.df$placeholder <- NULL
name.df$sort <- NULL
name.df$category <- NULL
message("Note: pre-exsiting placeholder column deleted")
}
key <- data.frame(name = base::sample(unique(name.df$name), size = num.names),
stringsAsFactors = FALSE) %>%
left_join(select(name.df, name, category), by="name") %>%
unique() %>%
group_by(category) %>%
sample_frac(1, replace=FALSE)
key$placeholder <- placeholders
disguised.df <- name.df %>%
left_join(key, by=c("name", "category")) %>%
tidyr::extract(col=placeholder, into="sort", regex="Name ([[:digit:]]+)", remove=FALSE)
disguised.df$sort <- as.numeric(disguised.df$sort)
disguised.df$placeholder <- reorder(disguised.df$placeholder, disguised.df$sort)
return(disguised.df)
}
plot.data <- disguise_names(plot.data)
plot.data$sex <- factor(plot.data$sex, levels=c("F", "M"))
```
```{r type_token}
ttr <- babynames %>%
group_by(year, sex) %>%
summarize(type=n(), token=sum(n)) %>%
mutate(ttr=type/token)
ggplot(ttr, aes(y=ttr, x=year, color=sex, fill=sex)) +
geom_point(alpha=.4) +
geom_smooth() +
labs(y=NULL, x=NULL, title="Number of unique names\n per births over time")+
theme(axis.text.y=element_blank(), axis.ticks.y=element_blank(), text=element_text(size=20))
ggsave("plots/babyshower/ttr_year.png", width=12, height=10, units="cm")
ggplot(ttr, aes(y=type, x=year, color=sex, fill=sex)) +
geom_point(alpha=.4) +
geom_smooth() +
labs(y=NULL, x=NULL, title="Number of unique names\nover time")+
theme(axis.text.y=element_blank(), axis.ticks.y=element_blank(), text=element_text(size=20))
ggsave("plots/babyshower/type_year.png", width=12, height=10, units="cm")
ggplot(ttr, aes(y=token, x=year, color=sex, fill=sex)) +
geom_point(alpha=.4) +
geom_smooth() +
labs(y=NULL, x=NULL, title="Number of births\nover time") +
theme(axis.text.y=element_blank(), axis.ticks.y=element_blank(), text=element_text(size=20))
ggsave("plots/babyshower/token_year.png", width=12, height=10, units="cm")
```
```{r plotting}
for(p in as.character(unique(plot.data$placeholder))){
this.data <- filter(plot.data, placeholder == p)
this.data$sex <- factor(this.data$sex, levels=c("F", "M"))
this.category <- unique(this.data$category)
ggplot(this.data, aes(y=prop, x=year, color=sex)) +
geom_line(size=4, alpha=.7, show.legend = FALSE) +
xlim(c(1900, 2014)) +
labs(x=NULL, y=NULL) +
scale_colour_hue(drop=FALSE) + # so it doesn't drop unused levels of sex
theme(text=element_text(size=25), axis.text.y=element_text(size=10), axis.text.x=element_text(size=15)) +
facet_wrap(~ placeholder)
ggsave(paste0("plots/babyshower/", this.category, "_", p, ".png"), width=11, height=10, units="cm")
}
```
```{r the_classics}
ggplot(classic.data, aes(y=prop, x=year, color=sex)) +
geom_line() +
facet_wrap(~name, scales = "free") +
xlim(c(1900, 2014))
ggplot(classic.data, aes(y=prop, x=year, color=name)) +
geom_line() +
facet_wrap(~ sex, ncol=1, scales = "free")
```
Some back up facts [http://nameberry.com/blog/nameberry-picks-13-best-virtue-names-for-boys](http://nameberry.com/blog/nameberry-picks-13-best-virtue-names-for-boys)
```{r names_to_aspire_to}
ggplot(virtue.data, aes(y=prop, x=year, color=sex)) +
geom_line() +
facet_wrap(~name, scales = "free") +
xlim(c(1900, 2014))
ggplot(virtue.data, aes(y=prop, x=year, color=name)) +
geom_line() +
facet_wrap(~ sex, ncol=1, scales = "free")
```
```{r famous_names}
ggplot(famous.data, aes(y=prop, x=year, color=sex)) +
geom_line() +
facet_wrap(~ name, scales = "free") +
xlim(c(1900, 2014))
ggplot(famous.data, aes(y=prop, x=year, color=name)) +
geom_line() +
facet_wrap(~ sex, ncol=1, scales = "free")
```