-
Notifications
You must be signed in to change notification settings - Fork 12
/
olympicdash-r-1.qmd
92 lines (83 loc) · 1.92 KB
/
olympicdash-r-1.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Olympic Games"
format: html
---
```{r}
#| label: load-packages
#| message: false
library(tidyverse)
library(gt)
```
```{r}
#| label: load-data
#| message: false
olympics_full <- read_csv("data/olympics.csv")
```
```{r}
#| label: prep-data
olympics <- olympics_full |>
filter(
season == "Summer",
!is.na(medal)
) |>
separate_wider_delim(cols = team, names = c("team", "suffix"), delim = "-", too_many = "merge", too_few = "align_start") |>
select(-suffix) |>
mutate(medal = fct_relevel(medal, "Bronze", "Silver", "Gold"))
```
## Medals by sport
```{r}
olympics |>
mutate(
sport = fct_lump_n(sport, n = 15),
sport = fct_infreq(sport),
sport = fct_rev(sport),
sport = fct_relevel(sport, "Other", after = 0)
) |>
ggplot(aes(y = sport, fill = medal)) +
geom_bar() +
guides(fill = guide_legend(reverse = TRUE)) +
labs(
x = NULL,
y = NULL,
fill = "Medal"
) +
theme_minimal() +
theme(
legend.position = "inside",
legend.position.inside = c(0.9, 0.2),
legend.direction = "horizontal",
legend.background = element_rect(fill = "white", color = "gray")
)
```
## Medals by year
```{r}
olympics |>
count(year, medal) |>
ggplot(aes(x = year, y = n, color = medal)) +
geom_point(size = 0.5) +
geom_line() +
guides(color = guide_legend(reverse = TRUE)) +
scale_x_continuous(breaks = seq(1896, 2020, 8)) +
labs(
x = "Year",
y = NULL,
color = "Medal"
) +
theme_minimal() +
theme(
legend.position = "inside",
legend.position.inside = c(0.9, 0.2),
legend.direction = "horizontal",
legend.background = element_rect(fill = "white", color = "gray")
)
```
## Medals by country
```{r}
olympics |>
count(team, medal) |>
pivot_wider(names_from = medal, values_from = n, values_fill = 0) |>
mutate(Total = Bronze + Gold + Silver) |>
relocate(Team = team, Gold, Silver, Bronze, Total) |>
arrange(desc(Total), Team) |>
gt()
```