-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNICAR2024.Rmd
209 lines (135 loc) · 3.53 KB
/
NICAR2024.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
---
title: "Baby Names"
author: "Lucia Walinchus"
date: "6/23/2020"
output: html_document
---
Bringing in required packages
```{r}
library(ggplot2)
library(tidyverse) #includes dplyr
library(DT)
library(babynames)
```
## Baby Names
The babynames package brings in names from the [Social Security Administration](https://www.ssa.gov/oact/babynames/limits.html)
Alternatively, you can download this file yourself which I prefer as it's more current.
Bringing in the dataframe
```{r}
US_Names <- babynames
summary(US_Names)
```
Learning count
```{r}
US_Names %>%
count(name)
```
Learning arrange
```{r}
US_Names %>%
count(name) %>%
arrange(desc(n))
```
Learning filter (and datatable)
```{r}
US_Names %>%
filter(year==2010) %>%
datatable()
```
Learning select
```{r}
US_Names %>%
select(name,prop) %>%
arrange(desc(prop))
```
Learning mutate
```{r}
popularity <- US_Names %>%
mutate(popularity=rank(-n, ties.method= "first")) %>%
arrange(popularity)
head(popularity)
```
Learning summarize/group_by
```{r}
top_names <- US_Names %>%
group_by(name) %>%
summarize(total=sum(n)) %>%
arrange(desc(total))
datatable(top_names)
```
The real power of R: grouping and ungrouping these
Learning summarize/group_by
```{r}
top_names_by_sex <- US_Names %>%
group_by(name,sex) %>%
summarize(total=sum(n)) %>%
ungroup() %>%
group_by(name) %>%
mutate(percent=total/sum(total))
datatable(top_names_by_sex)
```
How many male versus female Averys?
```{r}
top_names_by_sex %>%
filter(name=="Avery") %>%
ggplot( aes(x=sex, y=percent, fill=sex))+
geom_col(stat="identity")
```
Basics of ggplot
```{r}
top_names %>%
slice_head(n= 10) %>%
ggplot( aes(x= reorder(name, -total), y=total))+ #NOT %>%
geom_col()
```
```{r}
Marys <- US_Names %>%
filter(name=="Mary" & sex == "F")
ggplot(Marys, aes(year, n))+
geom_point()+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "Children Named 'Mary' in the \n United States per Year", )
```
```{r}
Leslies <- US_Names %>%
filter(name=="Leslie")
ggplot(Leslies, aes(year, n, fill = sex))+
geom_bar(stat = "identity")+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "Children Named 'Leslie' in the United States per Year")
```
```{r}
LuciaLucy <- US_Names %>%
filter(name=="Lucia"| name=="Lucy"& sex=="F")
ggplot(LuciaLucy, aes(year, n, color = name))+
geom_point(stat = "identity")+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "Children Named 'Lucy' and 'Lucia' in the United States per Year")
```
```{r}
ManyNames <- US_Names %>%
filter(name=="Tyrion" |
name=="Kaleesi"|
name=="Shae"|
name=="Arya"|
name=="Bran"|
name=="Catelyn"|
name=="Joffrey"|
name=="Sansa"|
name=="Brienne")
ggplot(ManyNames, aes(year, n, fill=name))+
geom_bar(stat = "identity")+
facet_wrap(~name, scales = "free")+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "US Baby Names",subtitle = "Sample of 9 popular baby names in the US per year")
```
Your turn: use your own name!
```{r}
Lucias <- US_Names %>%
filter(name=="Lucia"& sex == "F")
ggplot(Lucias, aes(year, n))+
geom_point()+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "Number of Lucias born each year", caption="I am having so much fun learning R")
#ggsave("Lucia.jpg", plot=last_plot())
```