-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
213 lines (187 loc) · 5.79 KB
/
script.R
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(ggsvg)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#2B579A"
c2 <- colorspace::lighten(c1, .7)
c3 <- colorspace::lighten(c1, .9)
c4 <- "#A41620"
c5 <- "white"
# colores de las medallas
md <- "#FFD700"
mp <- "#BABABA"
mb <- "#BF8970"
pal_medallas <- c(
Gold = md,
Silver = mp,
Bronze = mb
) |>
fct()
# fuente: Ubuntu
font_add(
family = "ubuntu",
regular = "fuente/Ubuntu-Regular.ttf",
bold = "fuente/Ubuntu-Bold.ttf",
italic = "fuente/Ubuntu-Italic.ttf"
)
# monoespacio & íconos
font_add(
family = "jet",
regular = "fuente/JetBrainsMonoNLNerdFontMono-Regular.ttf"
)
# bebas neue
font_add(
family = "bebas",
regular = "fuente/BebasNeue-Regular.ttf"
)
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{c1};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {32}, ",
"120 years of Olympic history.</span>"
)
autor <- glue("<span style='color:{c1};'>**Víctor Gauto**</span>")
icon_twitter <- glue("<span style='font-family:jet;'></span>")
icon_instagram <- glue("<span style='font-family:jet;'></span>")
icon_github <- glue("<span style='font-family:jet;'></span>")
icon_mastodon <- glue("<span style='font-family:jet;'>󰫑</span>")
usuario <- glue("<span style='color:{c1};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} {icon_instagram} ",
"{icon_mastodon} {usuario}"
)
# datos -------------------------------------------------------------------
tuesdata <- tidytuesdayR::tt_load(2024, 32)
olympics <- tuesdata$olympics
# me interesa la participación de Argentina y las medallas recibidas
# selecciono únicamente los JJOO post 1920
# aspecto de tiles
largo <- 13
alto <- 3
filas <- 10
# bandera
bandera <- "<img src='2024/s32/arg.png' width=30>"
# participación de Argentina en los JJOO
arg_part <- olympics |>
filter(season == "Summer" & team == "Argentina" & year > 1920) |>
distinct(event, year) |>
arrange(year) |>
count(year) |>
mutate(
x_part = map(n, ~ rep(1:largo, length.out = .x))
) |>
mutate(
y_part = map(n, ~ rep(1:largo, length.out = .x, each = largo))
) |>
unnest(cols = c(x_part, y_part)) |>
mutate(year = glue("{year} {bandera}"))
# medallas recibidas por Argentina en los JJOO
arg_med <- olympics |>
filter(season == "Summer" & team == "Argentina" & year > 1920) |>
drop_na(medal) |>
distinct(event, year) |>
arrange(year) |>
count(year) |>
mutate(
x_med = map(n, ~ rep(1:largo, length.out = .x))
) |>
mutate(
y_med = map(n, ~ rep(1:largo, length.out = .x, each = largo))
) |>
unnest(cols = c(x_med, y_med)) |>
mutate(year = glue("{year} {bandera}"))
# función que devuelve ícono del color y tamaño deseado
f_icon <- function(color, tamaño = 40) {
glue(
"<span style='font-family:jet; color: {color}; font-size: {tamaño}px'>",
"󰝤</span>")
}
# cantidad de medallas recibidas en los JJOO (oro, plata, bronce)
arg_r <- olympics |>
filter(season == "Summer" & team == "Argentina" & year > 1920) |>
drop_na(medal) |>
distinct(event, year, medal) |>
mutate(col = pal_medallas[medal]) |>
arrange(year) |>
count(year, medal, col) |>
arrange(year, col) |>
mutate(
l = glue("{f_icon(col, 20)} {n}")
) |>
reframe(
label = str_flatten(l, collapse = " "),
.by = year
) |>
mutate(year = glue("{year} {bandera}"))
# figura -----------------------------------------------------------------
# logo de los JJOO, agrego a caption
jjoo <- "<img src='2024/s32/jjoo.png' width=250>"
mi_caption_jjoo <- glue("{jjoo}<br>{mi_caption}")
# subtítulo
mi_subtitle <- glue(
"Desempeño de <b style='color: {c1}'>Argentina</b> en los",
"<b style='font-family: jet; color: {c4}'>Juegos Olímpicos</b>.<br>",
"{f_icon(c1)} indica participación en un evento.",
"{f_icon(c4)} señala la obtención de una medalla.",
.sep = " "
)
# figura
g <- ggplot(arg_part, aes(x_part, y_part)) +
geom_tile(fill = c1, color = c2, linewidth = 2) +
geom_tile(
data = arg_med, aes(x = x_med, y = y_med), fill = c4, , color = c2,
linewidth = 2
) +
# medallas DORADA, PLATEADA, BRONZE
geom_richtext(
data = arg_r, aes(largo + .5, filas + 1.5, label = label), family = "jet",
hjust = 1, fill = c5, , label.color = NA, vjust = 1, size = 4,
label.padding = unit(c(1, 1, .1, 1), "mm"),
label.r = unit(0, "mm")
) +
facet_wrap(vars(year), ncol = 4) +
coord_equal(
expand = FALSE, xlim = c(.25, largo + .75), ylim = c(.25, filas + 1.8),
clip = "off"
) +
labs(subtitle = mi_subtitle, caption = mi_caption_jjoo) +
theme_void() +
theme(
plot.margin = margin(l = 30.8, r = 30.8, t = 10),
plot.background = element_rect(fill = c3, color = c2, linewidth = 3),
plot.title = element_markdown(),
plot.subtitle = element_textbox_simple(
family = "ubuntu", size = 22, color = "black", margin = margin(b = 20)
),
plot.caption = element_markdown(
family = "ubuntu", size = 15, color = c4, lineheight = unit(1.3, "line"),
margin = margin(t = -130, b = 10)
),
panel.background = element_rect(fill = c2, color = NA),
panel.grid = element_blank(),
panel.spacing.x = unit(1, "line"),
axis.ticks = element_blank(),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_markdown(
size = 28, color = c4, family = "bebas", margin = margin(t = 5, b = 2),
hjust = 0
)
)
# guardo
ggsave(
plot = g,
filename = "2024/s32/viz.png",
width = 30,
height = 45,
units = "cm"
)
# abro
browseURL(glue("{getwd()}/2024/s32/viz.png"))