-
Notifications
You must be signed in to change notification settings - Fork 12
/
olympicdash-r-2.qmd
118 lines (108 loc) · 2.43 KB
/
olympicdash-r-2.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
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
---
title: "Olympic Games"
format:
dashboard:
orientation: columns
nav-buttons: [github]
github: https://github.com/posit-conf-2024/olympicdash
logo: images/olympics-logo.svg
logo-alt: "Olympics logo with multicolored circles."
---
```{r}
#| label: load-packages
library(tidyverse)
library(gt)
```
```{r}
#| label: load-data
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"))
```
## Column - Medals by sport and year
### Row - Medals by sport {height=60%}
```{r}
#| label: medals-by-sport
#| title: Medals by sport
#| fig-width: 10
#| fig-asp: 0.618
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")
)
```
### Row - Medals by year {height=40%}
```{r}
#| label: medals-by-year
#| title: Medals by year
#| fig-asp: 0.3
#| fig-width: 10
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")
)
```
## Column - Medals by country
```{r}
#| label: medals-by-country
#| title: Medals by country
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()
```