-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-s50_script.R
173 lines (149 loc) · 6.14 KB
/
2023-s50_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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#CC3A6A"
c2 <- "grey90"
c3 <- "#100A2C"
c4 <- "white"
c5 <- "#DBB1D3"
c6 <- "#100A2C"
# texto gral
font_add_google(name = "Ubuntu", family = "ubuntu")
# calificación IMDB y año
font_add_google(name = "Victor Mono", family = "victor", db_cache = FALSE)
# título
font_add_google(name = "Mountains of Christmas", family = "christmas", db_cache = FALSE)
# í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 50. ",
"Internet Movie Database</span>")
autor <- glue("<span style='color:{c3};'>**Víctor Gauto**</span>")
icon_twitter <- 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_mastodon}
{usuario}")
imdb_logo <- glue(
"<span style='font-family:fa-brands; font-size:30pt'>",
"</span>")
# datos -------------------------------------------------------------------
browseURL("https://github.com/rfordatascience/tidytuesday/blob/master/data/2023/2023-12-05/readme.md")
holiday_movies <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-12-12/holiday_movies.csv')
holiday_movie_genres <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-12-12/holiday_movie_genres.csv')
# me interesa ver la distribución de calificaciones de IMDb, por género
d_eng <- holiday_movies |>
select(-genres) |>
full_join(holiday_movie_genres, by = join_by(tconst)) |>
filter(title_type == "movie") |>
select(average_rating, genres) |>
mutate(genres = fct_reorder(genres, average_rating)) |>
drop_na() |>
mutate(m = median(average_rating), .by = genres) |>
mutate(n = n(), .by = genres)
# traducción de los géneros de las peículas
generos_eng <- unique(d_eng$genres) |> sort()
generos_esp <- c(
"Terror", "Thriller", "Misterio", "Acción", "Familiar", "Fantasía", "Comedia",
"Ciencia ficción", "Western", "Romántica", "Deportes", "Policial", "Aventura",
"Drama", "Musical", "Histórica", "Animada", "Bélica", "Música", "Noir",
"Biografía", "Documental")
names(generos_esp) <- generos_eng
d <- d_eng |>
mutate(genres = generos_esp[genres]) |>
mutate(genres = fct_reorder(genres, average_rating))
# películas con mejor puntuación, por género
d_max <- holiday_movies |>
select(-genres) |>
full_join(holiday_movie_genres, by = join_by(tconst)) |>
filter(title_type == "movie") |>
mutate(genres = generos_esp[genres]) |>
mutate(genres = fct_reorder(genres, average_rating)) |>
select(primary_title, year, genres, average_rating) |>
drop_na() |>
slice_max(average_rating, n = 1, by = genres, with_ties = FALSE) |>
mutate(average_rating = str_replace(average_rating, "\\.", ",")) |>
mutate(
titulo_label = glue(
"<span style='font-family:ubuntu; font-size:13pt; color: white'>",
"{primary_title}</span>")) |>
mutate(
año_label = glue(
"<span style='font-family: victor; font-size:10pt; color: grey90'>",
"{year}</span>")) |>
mutate(puntaje_label = glue(
"<span style='font-family: victor; font-size:10pt'>",
"**{average_rating}**/10</span>")) |>
mutate(label = glue("{titulo_label} {año_label}<br>{puntaje_label}"))
# figura ------------------------------------------------------------------
# título y subtítulo
mi_tit <- "En Navidad veamos un documental"
mi_sub <- glue(
"Los **documentales** presentan la mejor calificación entre las películas ",
"navideñas. Caso opuesto,<br>las de **terror** son las peores. ",
"Para cada género se indica la película con el mejor puntaje.")
# figura
g <- ggplot(d, aes(average_rating, genres, group = genres)) +
geom_richtext(
data = d_max, aes(10.2, genres, label = label), hjust = 0, fill = NA,
label.color = NA) +
geom_point( shape = 20, size = 5, alpha = .3, color = c2) +
geom_point(
aes(x = m), shape = 18, size = 6, alpha = 1, color = c4) +
scale_x_continuous(
breaks = seq(1, 10, 1), expand = c(0, 0), limits = c(1, 11)) +
labs(
title = mi_tit, subtitle = mi_sub, x = glue("Calificación {imdb_logo}"),
y = NULL, caption = mi_caption) +
coord_cartesian(clip = "off") +
guides(fill = guide_colorsteps(
frame.colour = c4, ticks = TRUE, ticks.colour = c4,
ticks.linewidth = 3/.pt, frame.linewidth = 1/.pt)) +
theme_void() +
theme(
plot.margin = margin(8.6, 280, 8.6, 5),
plot.background = element_rect(
fill = c1, color = c2, linewidth = 3),
plot.title = element_text(
family = "christmas", size = 60, color = c4,
margin = margin(t = 6)),
plot.title.position = "plot",
plot.subtitle = element_markdown(
family = "ubuntu", size = 18, color = c6,
margin = margin(b = 10, l = 10)),
plot.caption = element_markdown(
family = "ubuntu", color = c2, size = 10,
margin = margin(t = 10, r = -270)),
aspect.ratio = 1.5,
legend.key.height = unit(12, "mm"),
axis.title.x = element_markdown(
size = 20, hjust = .5, family = "ubuntu", color = c2,
margin = margin(t = 10)),
axis.ticks.y = element_blank(),
axis.text.x = element_text(
family = "victor", size = 14, color = c2, face = "bold"),
axis.text.y = element_text(
family = "ubuntu", size = 15, hjust = 1, margin = margin(0, 10, 0, 0),
color = c6),
panel.grid.major.x = element_line(
color = c5, linetype = "ff", linewidth = .1))
# guardo
ggsave(
plot = g,
filename = "2023/semana_50/viz.png",
width = 30,
height = 32,
units = "cm")
# abro
browseURL("2023/semana_50/viz.png")