-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
247 lines (220 loc) · 7.19 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#CD1076"
c2 <- "#679C35"
c3 <- "#B54EA3"
c4 <- "#80A1C1"
c5 <- colorspace::lighten(c4, .9)
c6 <- colorspace::lighten(c4, .5)
c7 <- "white"
c8 <- "black"
# 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"
)
# fontawesome
font_add(
family = "fa",
regular = "fuente/Font Awesome 6 Free-Solid-900.otf"
)
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{c1};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {34}, ",
"English Monarchs and Marriages, <b>ianvisits</b>.</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, 34)
monarcas <- tuesdata$english_monarchs_marriages_df
# me interesa si el monarca es mayor o menor que su consorte
# categorías asociadas al diferencia de edad
categorias <- c(
glue("El monarca es <b style='color: {c1}'>mayor</b><br>que el consorte"),
glue("El monarca es <b style='color: {c2}'>menor</b><br>que el consorte"),
glue("<b style='color: {c3}'>Igual</b> edad")
) |>
fct_inorder()
# arreglo de datos y agrego categorías
d <- monarcas |>
mutate(
across(
.cols = ends_with("_age"),
.fns = ~ if_else(.x == "?", NA, .x)
)
) |>
mutate(
across(
.cols = ends_with("_age"),
.fns = ~ if_else(.x == "–", NA, .x)
)
) |>
mutate(
across(
.cols = ends_with("_age"),
.fns = ~ str_replace(.x, "\\(\\?\\)", "")
)
) |>
mutate(
across(
.cols = ends_with("_age"),
.fns = as.numeric
)
) |>
drop_na() |>
mutate(dif = king_age - consort_age) |>
mutate(es_mayor = case_when(
king_age > consort_age ~ categorias[1],
king_age < consort_age ~ categorias[2],
.default = categorias[3]
))
# matrimonio con máxima diferencia
d_max <- d |>
slice_max(order_by = dif)
max_label <- glue(
"{d_max$king_name} ({d_max$king_age}) y",
"{d_max$consort_name} ({d_max$consort_age})",
.sep = "<br>"
)
# matrimonio con la menor edad
d_min <- d |>
slice_min(order_by = king_age)
min_label <- glue(
"{d_min$king_name} ({d_min$king_age}) y",
"{d_min$consort_name} ({d_min$consort_age})",
.sep = "<br>"
)
# combino el matrimonio con la máxima diferencia y el de menor edad
d_ext <- bind_rows(d_max, d_min) |>
mutate(
label = glue(
"{king_name}: <b style='font-family:jet; color: {c1}'>{king_age}</b><br>",
"{consort_name}: <b style='font-family:jet; color: {c1}'>{consort_age}",
"</b><br>",
"Matrimonio: <b style='font-family:jet; color: {c1}'>{year_of_marriage}</b>"
)
)
# figura ------------------------------------------------------------------
# título y subtítulo
corona <- glue("<span style='font-family:jet;'></span>")
mi_subtitulo <- glue(
"Los <b style='color: {c3}'>monarcas ingleses</b> en general siempre ",
"mayores que sus<br>",
"consortes. Puntos por encima de la diagonal a <b>45°</b> representan ",
"<br>",
"matrimonios en los que el Rey/Reina son mayores que su pareja."
)
# ícono de rey/reina del ajedrez sobre la leyenda
icono_rey <- "<span style='font-family:fa;'></span>"
icono_reina <- "<span style='font-family:fa;'></span>"
# figura
g <- ggplot(
d,
aes(
consort_age, king_age, fill = es_mayor, shape = es_mayor)
) +
# 45°
geom_abline(color = c6, linetype = 1, linewidth = 2, alpha = .6) +
geom_abline(color = c4, linetype = "44", linewidth = 2, alpha = .3) +
# puntos
geom_point(size = 5.2, alpha = 1, color = c8, stroke = 1) +
# máximo/mínimo
geom_richtext(
data = d_ext, aes(label = label), fill = c5, color = c8, nudge_x = .7,
family = "ubuntu", size = 6, hjust = 0, label.color = c4, vjust = .5,
label.r = unit(0, "mm"), label.size = unit(.6, "mm"),
label.padding = unit(3, "mm")
) +
# corona rey/reina
annotate(
geom = "richtext", x = I(1), y = I(.285), hjust = 1, vjust = 0, fill = c5,
label = glue("{icono_rey}{icono_reina}"), label.color = NA, size = 25,
color = c4
) +
scale_x_continuous(
limits = c(3, 60), breaks = scales::breaks_width(5), expand = c(0, 1)
) +
scale_y_continuous(
limits = c(3, 60), breaks = scales::breaks_width(5), expand = c(0, 1)
) +
scale_fill_manual(values = c(c1, c2, c3)) +
scale_shape_manual(values = c(21, 22, 23)) +
coord_cartesian(clip = "off") +
labs(
x = "Edad del consorte", y = "Edad del monarca", fill = NULL,
shape = NULL, caption = mi_caption, title = corona, subtitle = mi_subtitulo
) +
guides(
fill = guide_legend(position = "inside", override.aes = list(size = 8)),
shape = guide_legend(position = "inside")
) +
theme_minimal() +
theme(
aspect.ratio = 1,
plot.margin = margin(r = 35.6, l = 35),
plot.background = element_rect(fill = c5, color = c3, linewidth = 3),
plot.title = element_markdown(
color = c3, size = 140, margin = margin(b = -100, t = -10, l = 50)
),
plot.title.position = "plot",
plot.subtitle = element_markdown(
family = "ubuntu", color = c8, size = 18, lineheight = unit(1.2, "line"),
margin = margin(b = 50, l = 170)
),
plot.caption = element_markdown(
family = "ubuntu", size = 13, color = c2, margin = margin(t = 20, b = 10),
lineheight = unit(1.2, "line")
),
panel.grid.major = element_line(
linetype = "FF", linewidth = .15, color = c4
),
panel.grid.minor = element_blank(),
axis.text = element_text(family = "jet", size = 15, color = c8),
axis.text.x = element_text(margin = margin(t = 10)),
axis.text.y = element_text(margin = margin(r = 10)),
axis.title = element_text(family = "ubuntu", size = 22),
axis.title.x = element_text(margin = margin(t = 20)),
axis.title.y = element_text(margin = margin(r = 20)),
legend.position.inside = c(.99, .08),
legend.justification.inside = c(1, 0),
legend.background = element_rect(fill = c5, color = c4),
legend.text = element_markdown(
family = "ubuntu", size = 16, margin = margin(l = 10), color = c8
),
legend.margin = margin(20, 20, 20, 20),
legend.key.spacing.y = unit(.6, "cm")
)
# guardo
ggsave(
plot = g,
filename = "2024/s34/viz.png",
width = 30,
height = 34,
units = "cm")
# abro
browseURL(glue("{getwd()}/2024/s34/viz.png"))