-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
194 lines (163 loc) · 5.45 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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#4F53B7"
c2 <- "#DDDDEE"
c3 <- "#6B6100"
c4 <- "#E0DDC9"
c5 <- "#C75DAA"
c6 <- "#F3DEEE"
# fuente: ubuntu
font_add(
family = "ubuntu",
regular = "fuente/Ubuntu-Regular.ttf",
bold = "fuente/Ubuntu-Bold.ttf",
italic = "fuente/Ubuntu-Italic.ttf")
# fuente: victor
font_add(
family = "victor",
regular = "fuente/VictorMono-ExtraLight.ttf",
bold = "fuente/VictorMono-VariableFont_wght.ttf",
italic = "fuente/VictorMono-ExtraLightItalic.ttf")
# fuente: abril
font_add_google(
name = "Abril Fatface",
family = "abril"
)
# fuente: bebas
font_add(
family = "bebas",
regular = "fuente/BebasNeue-Regular.ttf"
)
# íconos
font_add("fa-brands", "icon/Font Awesome 6 Brands-Regular-400.otf")
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{c3};'><span style='font-family:mono;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {9}. ",
"Artículo *February 29*, Wikipedia.</span>")
autor <- glue("<span style='color:{c3};'>**Víctor Gauto**</span>")
icon_twitter <- glue("<span style='font-family:fa-brands;'></span>")
icon_instagram <- glue("<span style='font-family:fa-brands;'></span>")
icon_github <- glue("<span style='font-family:fa-brands;'></span>")
icon_mastodon <- glue("<span style='font-family:fa-brands;'></span>")
usuario <- glue("<span style='color:{c3};'>**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, 9)
births <- tuesdata$births
deaths <- tuesdata$deaths
# me interesa un listado de 29 nacimientos/muertes, señalando el caso de
# James Milne Wilson, que nació y murió un 29/2
jmw <- inner_join(deaths, births, by = join_by(person)) |>
pull(person)
jmw_n <- births |>
filter(person == jmw) |>
pull(year_birth)
jmw_m <- births |>
filter(person == jmw) |>
pull(year_death)
# para asegurar la reproducibilidad
set.seed(2024)
# cantidad de personas y desplazamiento horizontal
n_nombres <- 28
nud <- .2
# nacimientos
n_28 <- births |>
select(persona = person) |>
filter(persona != jmw) |>
slice_sample(n = n_nombres) |>
mutate(tipo = "nacimiento")
n_jmw <- births |>
filter(person == jmw) |>
select(persona = person) |>
mutate(tipo = "nacimiento")
d_nac <- bind_rows(n_28, n_jmw) |>
slice_sample(n = n_nombres+1) |>
mutate(x = rep_len(c(1-nud, 1+nud), n_nombres + 1))
# muertes
m_28 <- deaths |>
select(persona = person) |>
filter(persona != jmw) |>
slice_sample(n = n_nombres) |>
mutate(tipo = "muerte")
m_jmw <- deaths |>
filter(person == jmw) |>
select(persona = person) |>
mutate(tipo = "muerte")
d_mue <- bind_rows(m_28, m_jmw) |>
slice_sample(n = n_nombres+1) |>
mutate(x = rep_len(c(3-nud, 3+nud), n_nombres + 1))
# combino datos
d <- bind_rows(d_nac, d_mue) |>
mutate(y = row_number(), .by = tipo) |>
mutate(nudge_x = rep_len(c(.2, -.2), n_nombres + 1), .by = tipo) |>
mutate(persona = str_wrap(persona, 15)) |>
mutate(persona = str_replace_all(persona, fixed("\n"), "<br>")) |>
mutate(tipo = factor(tipo, levels = c("nacimiento", "muerte")))
# figura ------------------------------------------------------------------
# subtítulo
mi_subtitle <- glue(
"29 personas que <b style='color:{c1}'>nacieron</b> ó ",
"<b style='color:{c3}'>murieron</b> el **29 de febrero**.<br>",
"Es curioso el caso de <b style='color:{c5}'>{jmw}</b> ",
"({jmw_n}-{jmw_m}), Primer<br>Ministro de Tasmania, ",
"muerto el día de su cumpleaños."
)
# figura
g <- ggplot(d, aes(x, y, label = persona, color = tipo)) +
# personas
geom_richtext(
size = 6, family = "bebas", fill = NA, label.color = NA,
show.legend = FALSE) +
# JMW
geom_richtext(
data = filter(d, str_detect(persona, "Milne")), aes(fill = tipo),
family = "bebas", size = 6, show.legend = FALSE, label.color = NA,
label.padding = unit(c(5, 5, 5, 5), "mm"), label.r = unit(0, "lines")) +
# 29
annotate(
geom = "text", x = 2, y = (n_nombres+1)/2, label = "2\n9", fontface = "bold",
size = 150, family = "abril", lineheight = unit(1, "line"),
color = c5) +
annotate(
geom = "text", x = 2, y = (n_nombres+1)/2, label = "Febrero", size = 20,
fontface = "bold", family = "abril", lineheight = unit(1, "line"),
color = c5) +
scale_color_manual(
values = c(c1, c3)) +
scale_fill_manual(
values = c(c2, c4)) +
coord_cartesian(
clip = "off", xlim = c(.5, 3.5), expand = FALSE) +
labs(subtitle = mi_subtitle, caption = mi_caption) +
theme_void() +
theme(
aspect.ratio = 1,
plot.background = element_rect(color = c5, fill = c6, linewidth = 3),
plot.margin = margin(t = 25, r = 28.7, b = 25, l = 28.7),
plot.title.position = "plot",
plot.subtitle = element_markdown(
family = "ubuntu", size = 20, margin = margin(b = 50), hjust = .5,
lineheight = unit(1.3, "line")),
plot.caption = element_markdown(
family = "ubuntu", size = 14, color = c1, margin = margin(t = 60))
)
# guardo
ggsave(
plot = g,
filename = "2024/s09/viz.png",
width = 30,
height = 37,
units = "cm")
# abro
browseURL("2024/s09/viz.png")