-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.qmd
70 lines (54 loc) · 1.28 KB
/
analysis.qmd
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
---
title: "Analysis"
---
We will use R version 4.3.0 with some libraries:
- babynames
- knitr
- dplyr
- ggplot2
```{r}
library(babynames)
library(knitr)
library(dplyr)
library(ggplot2)
```
## A glimpse of the data to be used:
```{r}
head(babynames) |> kable()
```
## Let's go
```{r}
#| code-fold: true
get_most_frequent <- function(babynames, select_sex, from = 1950) {
most_freq <- babynames |>
filter(sex == select_sex, year > from) |>
group_by(name) |>
summarise(average = mean(prop)) |>
arrange(desc(average))
return(most_freq)
}
plot_top <- function(x, babynames, select_sex, top = 10, from = 1950) {
topx <- x$name[1:top]
p <- babynames |>
filter(name %in% topx, sex == select_sex, year > from) |>
ggplot(aes(x = year, y = prop, color = name)) +
geom_line() +
scale_color_brewer(palette = "Paired") +
theme_classic()
return(p)
}
```
```{r}
#| label: "fig-girl"
#| fig-cap: "girls"
get_most_frequent(babynames, select_sex = "F") |>
plot_top(babynames, select_sex = "F", top = 10)
```
You can see girl names in @fig-girl just above.
```{r}
#| label: "fig-boy"
#| fig-cap: "boys"
get_most_frequent(babynames, select_sex = "M") |>
plot_top(babynames, select_sex = "M", top = 10)
```
Same for boy names in @fig-boy just above.