-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
206 lines (175 loc) · 6.08 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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(ggthemes)
library(patchwork)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
pal <- c("#F28AAA", "#A1C2ED", "#9CC184", "#F9D14A", "#DF9ED4")
c1 <- "#EAF3FF"
c2 <- "white"
c3 <- "grey10"
c4 <- "grey90"
# 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"
)
font_add_google(
name = "Ultra",
family = "ultra"
)
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{pal[4]};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {35}, ",
"Power Rangers Dataset.</span>"
)
autor <- glue("<span style='color:{pal[4]};'>**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>")
icon_imdb <- glue(
"<span style='font-family:jet; font-size:40px'></span>")
usuario <- glue("<span style='color:{pal[4]};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} ",
"{icon_instagram} {icon_mastodon} {usuario}"
)
url_pr <- "https://upload.wikimedia.org/wikipedia/en/b/bd/Power_Rangers_Logo.webp"
# datos -------------------------------------------------------------------
tuesdata <- tidytuesdayR::tt_load(2024, 35)
episodes <- tuesdata$power_rangers_episodes
# me interesa la popularidad y el puntaje de los episodios al inicio y en la
# actualidad de la serie
# agrego el año de inicio a la temporada
d <- episodes |>
mutate(season_title = str_replace(season_title, "Season ", "T")) |>
mutate(año_i = year(min(air_date)), .by = season_title) |>
mutate(
año_i = glue(
"<span style='font-family: jet; font-size: 15px; color: {pal[1]}'>",
"{año_i}</span>"
)
) |>
mutate(season_title = glue("{season_title}<br>{año_i}")) |>
mutate(season_title = fct_reorder(season_title, air_date))
# cantidad de temporadas
n_season <- length(unique(d$season_title))
# figura ------------------------------------------------------------------
# título de los ejes
eje_horizontal <- glue("Votos<br>{icon_imdb}")
eje_vertical <- glue("Puntaje<br>{icon_imdb}")
# función que genera una figura para las primeras/últimas temporadas
f_gg <- function(tbl, subtitulo) {
g <- ggplot(tbl, aes(total_votes, IMDB_rating, fill = season_title)) +
# todas las temporadas
geom_point(
data = select(d, -season_title), aes(total_votes, IMDB_rating),
inherit.aes = FALSE, alpha = .1, size = .6, show.legend = FALSE,
color = c2, shape = 20
) +
# temporadas de interés, a destacar
geom_point(
alpha = .9, size = 2, show.legend = FALSE, shape = 21
) +
facet_wrap(vars(season_title), ncol = 5, scales = "free") +
scale_x_log10(
limits = c(10, 1000), expand = c(0, 0), breaks = c(10, 100, 1000)
) +
scale_y_continuous(limits = c(4, 10), expand = c(0, 0), breaks = 4:10) +
scale_fill_manual(
values = rep(pal, length.out = n_season)
) +
labs(x = eje_horizontal, y = eje_vertical, subtitle = subtitulo) +
coord_cartesian(clip = "off") +
theme_linedraw() +
theme(
aspect.ratio = 1,
plot.margin = margin(b = -20),
plot.background = element_blank(),
plot.subtitle = element_markdown(
size = 16, family = "ubuntu", color = c1, margin = margin(b = 5, t = 20)
),
panel.background = element_blank(),
panel.grid = element_blank(),
panel.spacing.x = unit(1.3, "line"),
axis.ticks = element_blank(),
axis.title.x = element_markdown(
family = "ubuntu", hjust = .1, margin = margin(t = 10), color = c2
),
axis.title.y = element_markdown(
family = "ubuntu", angle = 0, vjust = .5, margin = margin(r = 10),
color = c2
),
axis.text = element_text(family = "jet", color = c2),
axis.text.y = element_text(vjust = 0),
strip.text = element_markdown(
hjust = 0, family = "ubuntu", color = pal[2], size = 13, face = "bold"
),
strip.background = element_blank()
)
return(g)
}
# número de las primeras/últimas temporadas
n_head <- unique(as.numeric(d$season_title))[1:5]
n_tail <- unique(as.numeric(d$season_title))[(n_season-4):n_season]
# filtro a partir del número de las primeras/últimas temporadas
d_head <- d |>
filter(as.numeric(season_title) %in% n_head)
d_tail <- d |>
filter(as.numeric(season_title) %in% n_tail)
# subtítulo de cada figura
pr <- glue(
"<span style='font-family: ultra; color: {pal[4]};'>POWER RANGERS</span>"
)
sub_head <- glue(
"Las primeras temporadas de {pr} tuvieron la máxima popularidad, ",
"con puntajes mixtos."
)
sub_tail <- glue(
"Las últimas temporadas cayeron en popularidad, pero la calidad de los ",
"episodios mejoró."
)
# logo de los Power Rangers
g_pr <- ggplot() +
ggpath::geom_from_path(
data = tibble(x = 0, y = 0, path = url_pr),
aes(x, y, path = path)) +
coord_cartesian(xlim = c(-.02, .02212), clip = "off") +
theme_void()
# composición final de la figura
g <- f_gg(d_head, sub_head) / g_pr / f_gg(d_tail, sub_tail) +
plot_layout(heights = c(.43, .13, .43)) +
plot_annotation(
caption = mi_caption,
theme = theme(
plot.margin = margin(r = 20, l = 10, t = 10, b = 5),
plot.background = element_rect(fill = c3, color = pal[3], linewidth = 3),
plot.caption = element_markdown(
family = "ubuntu", size = 11, color = pal[1], margin = margin(t = -20)
)
)
)
# guardo
ggsave(
plot = g,
filename = "2024/s35/viz.png",
width = 30,
height = 22,
units = "cm")
# abro
browseURL(glue("{getwd()}/2024/s35/viz.png"))