-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
198 lines (172 loc) · 5.33 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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(tidytext)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#DF9ED4"
c2 <- "#55092A"
c3 <- "#960B35"
c4 <- "#FD8386"
# 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"
)
# Creepster
font_add_google(
name = "Creepster",
family = "creepster"
)
# Girassol
font_add_google(
name = "Moul",
family = "moul"
)
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{c4};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {44}, ",
"<b>Internet Movie Database</b>.</span>"
)
autor <- glue("<span style='color:{c4};'>**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:{c4};'>**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, 44)
monster_movies <- tuesdata$monster_movies
# me interesan cómo se formas los títulos de las películas, las palabras
# anteriores y posteriores a MONSTER
# divido las títulos de las películas entre antes y después de la palabra
# MONSTRUO
mosters_pre_post <- select(monster_movies, primary_title) |>
mutate(primary_title = toupper(primary_title)) |>
mutate(primary_title = str_replace_all(primary_title, "-", "_")) |>
mutate(
div = str_replace(
primary_title, "(.*)MONSTERS|MONSTER(.*)", "\\1-\\2"
)
) |>
separate_wider_delim(
cols = div,
delim = "-",
names = c("pre_palabra", "post_palabra")
)
# selecciono las palabras más frecuentes y remuevo las stopwords
# divido los conjuntos en pre y post palabras
n_top <- 15
stopwords <- filter(stop_words, lexicon == "SMART") |>
pull(word)
d_pre <- mosters_pre_post |>
select(pre_palabra) |>
unnest_tokens(
output = "palabras",
input = pre_palabra
) |>
filter(!palabras %in% stopwords) |>
count(palabras, sort = TRUE) |>
slice_max(order_by = n, n = n_top, with_ties = FALSE) |>
pull(palabras) |>
toupper() |>
str_replace("_", "-")
d_post <- mosters_pre_post |>
select(post_palabra) |>
unnest_tokens(
output = "palabras",
input = post_palabra
) |>
filter(!palabras %in% stopwords) |>
count(palabras, sort = TRUE) |>
slice_max(order_by = n, n = n_top, with_ties = FALSE) |>
pull(palabras) |>
toupper() |>
str_replace("_", "-")
# figura ------------------------------------------------------------------
# MONSTER
label_monster <- str_split("MONSTER", "")[[1]] |>
str_flatten(collapse = "<br>")
# parámetros de la figura
tamaño_label <- 13
tamaño_monster <- 38
ancho_linea <- .3
mi_subtitulo <- glue(
"Términos más frecuentes antes y después de la palabra
<span style='font-family: creepster; color: {c1};'>MONSTER</span> en
<b>{nrow(monster_movies)}</b> películas."
)
# figura
g <- ggplot() +
# líneas
annotate(
geom = "segment", x = -5, xend = 0, y = 1:15, yend = 7.7, color = c3,
linewidth = ancho_linea, linetype = 2
) +
annotate(
geom = "segment", x = 5, xend = 0, y = 1:15, yend = 7.7, color = c3,
linewidth = ancho_linea, linetype = 2
) +
# pre
annotate(
geom = "richtext", x = -5, y = 1:15, label = d_pre, hjust = 1, color = c2,
family = "creepster", size = tamaño_label, fill = c1, label.color = c3,
label.size = unit(.7, "mm"), label.padding = unit(3, "mm"),
label.r = unit(3, "mm")
) +
# post
annotate(
geom = "richtext", x = 5, y = 1:15, label = d_post, hjust = 0, color = c2,
family = "creepster", size = tamaño_label, fill = c1, label.color = c3,
label.size = unit(.7, "mm"), label.padding = unit(3, "mm"),
label.r = unit(3, "mm")
) +
# MONSTER
annotate(
geom = "richtext", x = 0, y = 7.5, label = label_monster, hjust = .42,
family = "moul", size = tamaño_monster, lineheight = unit(.9, "line"),
fill = NA, label.color = NA, color = c1
) +
scale_x_continuous(limits = c(-12, 12)) +
scale_y_continuous(limits = c(0, 15)) +
labs(subtitle = mi_subtitulo, caption = mi_caption) +
theme_void() +
theme(
aspect.ratio = 1,
plot.margin = margin(r = 5, l = 5, t = 1.5),
plot.background = element_rect(color = c3, fill = c2, linewidth = 3),
plot.subtitle = element_markdown(
family = "ubuntu", color = c4, size = 20, hjust = .5,
margin = margin(t = 15, b = 20)
),
plot.caption = element_markdown(
family = "ubuntu", size = 12, color = c1,
margin = margin(b = 10, r = 10, t = -20)
)
)
# guardo
ggsave(
plot = g,
filename = "2024/s44/viz.png",
width = 30,
height = 32,
units = "cm")
# abro
browseURL(glue("{getwd()}/2024/s44/viz.png"))