-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
235 lines (207 loc) · 6.72 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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(ggarchery)
library(ggsvg)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#399483"
c2 <- "#B54EA3"
c3 <- "#F2FBFC"
c4 <- "black"
c5 <- "#00186D"
c6 <- "#06C7FF"
# 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:{c2};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {30}, ",
"artículo <b>American Idol</b> en Wikipedia.</span>")
autor <- glue("<span style='color:{c2};'>**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:{c2};'>**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, 30)
ratings <- tuesdata$ratings
# me interesa la audiencia en todas las temporadas, y si aumenta/baja la
# audiencia con los episodios
d <- ratings |>
select(season, show_number, airdate, viewers_in_millions) |>
mutate(fecha_X = mdy(airdate)) |>
mutate(airdate = if_else(
is.na(fecha_X),
glue("{airdate}, 2014"),
airdate
)) |>
mutate(fecha = mdy(airdate)) |>
select(-fecha_X) |>
drop_na(viewers_in_millions) |>
mutate(año = year(fecha))
# el aumento/baja de la audiencia con los episodios lo defino con la pendiente
# positiva/negativa de un modelos lineal: audiencia = f(episodios)
d_mod <- d |>
mutate(año = year(fecha)) |>
nest(.by = año) |>
mutate(mod = map(.x = data, ~lm(viewers_in_millions ~ fecha, data = .x))) |>
mutate(
mod = map(mod, broom::tidy)
) |>
unnest(mod) |>
filter(term == "fecha") |>
mutate(sube = if_else(
estimate > 0,
c1,
c2
))
# identifico las temporadas más populares
d |>
mutate(año = year(fecha)) |>
reframe(
viewers_in_millions = median(viewers_in_millions),
fecha = mean(fecha),
.by = c(año, season)
) |>
arrange(desc(viewers_in_millions))
# combino fechas y audiencias con pendientes del modelo lineal
d2 <- inner_join(d, d_mod, by = join_by(año)) |>
select(any_of(names(d)), sube)
# número de temporada ubicado debajo y en el medio de cada conjunto
d_season <- d |>
mutate(año = year(fecha)) |>
reframe(
fecha = mean(fecha),
.by = c(season, año)
) |>
mutate(
label = if_else(
season < 10,
glue("T0{season}"),
glue("T{season}")
)
) |>
inner_join(d2, by = join_by(season, año)) |>
rename(fecha = fecha.x) |>
reframe(
fecha = median(fecha),
viewers_in_millions = min(viewers_in_millions)-.6,
.by = c(label, sube)
)
# 1er programa
fecha_1 <- d |>
slice(1) |>
pull(fecha) |>
format("%d de %B de %Y")
fecha_1_label <- glue(
"El 1<sup>er</sup> programa se emite<br>",
"el {fecha_1}."
)
# figura ------------------------------------------------------------------
# título de eje vertical y subtítulo
titulo_y <- "Espectadores<br><span style='font-size:17px'>(en millones)</span>"
mi_subtitulo <- glue(
"La popularidad de <b style='color:{c6}'>AMERICAN IDOL</b> tuvo su máximo en",
"las temporadas <b>5</b> y <b>6</b>. Por lo general, la audiencia",
"<b style='color:{c2}'>disminuye</b> con el progreso de los episodios.",
"Únicamente en 3 temporadas (1, 2 y 17) la popularidad fue en",
"<b style='color:{c1}'>aumento</b>.",
.sep = " "
)
# logo (.svg) de American Idol
svg_url <-
"https://upload.wikimedia.org/wikipedia/commons/8/87/American_Idol_logo.svg"
svg_txt <- paste(readLines(svg_url), collapse = "\n")
# figura
g <- ggplot(d2, aes(fecha, viewers_in_millions)) +
# tendencia
geom_smooth(
se = FALSE, color = alpha(c1, .3), method = loess, formula = y ~ x,
linetype = 1, linewidth = 4, lineend = "round") +
geom_smooth(
se = FALSE, color = alpha(c2, .3), method = loess, formula = y ~ x,
linetype = "33", linewidth = 4) +
# puntos
geom_point(
fill = d2$sube, shape = 21, size = 4, color = c4, alpha = .8) +
# temporadas
geom_richtext(
data = d_season, aes(fecha, viewers_in_millions, label = label), size = 6,
family = "bebas", vjust = 1, fill = d_season$sube, color = c3,
label.padding = unit(0.2, "lines"), label.colour = c4,
label.r = unit(0, "mm")
) +
# American Idol logo
geom_point_svg(
x = ymd(20210101),y = 40, svg = svg_txt, size = 45, hjust = 1, vjust = 1) +
# 1er programa
annotate(
geom = "richtext", x = d_season$fecha[1]-months(5),
y = d_season$viewers_in_millions[1]-1, label = fecha_1_label, fill = c3,
label.color = NA, family = "ubuntu", size = 5, hjust = 0, vjust = 1,
color = c5
) +
scale_x_date(
date_breaks = "1 year", date_labels = "'%y", position = "bottom",
limits = ymd(20020101, 20210101), expand = c(0, 0)) +
scale_y_continuous(
breaks = seq(5, 40, 5),
limits = c(3, 40),
expand = c(0, 0)) +
labs(x = NULL, y = titulo_y, subtitle = mi_subtitulo, caption = mi_caption) +
theme_linedraw() +
theme(
aspect.ratio = 1,
plot.margin = margin(r = 34, l = 14),
plot.background = element_rect(
fill = c3, color = c5, linewidth = 3),
plot.subtitle = element_textbox_simple(
family = "ubuntu", size = 19, color = c5,
margin = margin(b = 10, t = 14.2)),
plot.caption = element_markdown(
family = "ubuntu", size = 13, color = c1,
margin = margin(t = 20, b = 10)),
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(linetype = "FF"),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(
family = "bebas", size = 27, color = c5),
axis.text.y = element_text(family = "jet", size = 15, color = c5),
axis.title.y = element_markdown(
family = "ubuntu", size = 20, color = c5),
legend.position = "none"
)
# guardo
ggsave(
plot = g,
filename = "2024/s30/viz.png",
width = 30,
height = 32,
units = "cm")
# abro
browseURL("2024/s30/viz.png")