-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-s33_script.R
194 lines (167 loc) · 6.39 KB
/
2023-s33_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(tidyverse)
library(glue)
library(ggtext)
library(showtext)
# fuente ------------------------------------------------------------------
# colores, Nord
c1 <- "#AD8CAE"
c2 <- "#EDDAEB"
c3 <- "#222B4C"
c4 <- "white"
c5 <- "black"
# texto gral
font_add_google(name = "Ubuntu", family = "ubuntu")
# números, porcentajes, cantidades
font_add_google(name = "Victor Mono", family = "victor", db_cache = FALSE)
# eje horizontal, temporadas
font_add_google(name = "Tilt Prism", family = "tilt", db_cache = FALSE)
# íconos
font_add("fa-brands", "icon/Font Awesome 6 Brands-Regular-400.otf")
font_add("fa-solids", "icon/Font Awesome 6 Free-Solid-900.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 33. *spam* dataset en {{kernlab}}</span>")
autor <- glue("Autor: <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>")
usuario <- glue("<span style='color:{c3};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue("{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} {usuario}")
# datos -------------------------------------------------------------------
browseURL("https://github.com/rfordatascience/tidytuesday/blob/master/data/2023/2023-08-15/readme.md")
spam <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-08-15/spam.csv')
# me interesan los elementos relativos al dinero: money, dollar, n000
# cómo se diferencian los e-mails que son o NO son spam
# tiene que tener los elementos: porcentajes > 0
d <- spam |>
select(dollar, money, n000, yesno) |>
pivot_longer(cols = -yesno, names_to = "param", values_to = "valor") |>
filter(valor != 0)
# figura ------------------------------------------------------------------
# líneas de referencia horizontales
lineas_tbl <- tibble(
y = c(120, 60, 80, 40, 80, 40),
param = c("dollar", "dollar", "money", "money", "n000", "n000")
)
# cantidad de datos, para agregar en las esquinas
d_cantidad <- d |>
count(yesno, param) |>
inner_join(
slice_max(
lineas_tbl,
order_by = y,
n = 3),
by = join_by(param))
# cantidad de e-mails que SÍ son spam
d_yes <- d_cantidad |>
filter(yesno == "y") |>
mutate(label = glue("n = {n}"))
# cantidad de e-mails que NO son spam
d_no <- d_cantidad |>
filter(yesno == "n") |>
mutate(label = glue("n = {n}"))
# etiquetas para los strips
etiq <- c(
dollar = "**$**",
money = "**money**",
n000 = "**000**"
)
# colores para representar SÍ/NO es spam
yesno_colores <- c(SÍ = alpha("white", .9), NO = alpha("black", .9))
# título y subtítulo
# palabra SPAM en blanco y negro
spam_blanco <- "<b style='color:white;'>SPAM</b>"
spam_negro <- "<b style='color:black;'>SPAM</b>"
mi_title <- glue("{spam_blanco}{spam_negro}{spam_blanco}{spam_negro}")
mi_subt <- glue(
"¿Qué caracteriza al **spam**? Por lo visto, hablar de plata.
Al analizar {nrow(d)} e-mails, se
contabilizó el porcentaje de términos relativos al dinero:
el signo **$**, la palabra **money** y tres ceros **000**.
Se indican las cantidades (n) de e-mails en cada panel.")
# figura
g <- d |>
mutate(yesno = if_else(yesno == "y", "SÍ", "NO")) |>
mutate(yesno = factor(yesno, levels = c("SÍ", "NO"))) |>
ggplot(aes(valor, fill = yesno, color = yesno)) +
# horizontales
geom_hline(
data = lineas_tbl, aes(yintercept = y),
linetype = 3, color = c2) +
# cantidad, eje vertical, línea horizontal
geom_text(
data = lineas_tbl, aes(x = 0, y = y, label = y), inherit.aes = FALSE,
hjust = 0, vjust = 0, nudge_y = 1, family = "victor", color = c2, size = 3) +
# línea de frecuencia
geom_freqpoly(
binwidth = .075, alpha = .9) +
# cantidades, n, YES
geom_text(
data = d_yes, aes(2, y, label = label), inherit.aes = FALSE,
hjust = 0, vjust = 0, color = yesno_colores["SÍ"], nudge_y = +1,
family = "victor", size = 4, fontface = "bold") +
# cantidades, n, NO
geom_text(
data = d_no, aes(2, y, label = label), inherit.aes = FALSE,
hjust = 0, vjust = 1, color = yesno_colores["NO"], nudge_y = -1,
family = "victor", size = 4, fontface = "bold") +
# facetas
facet_wrap(vars(param), scales = "free", labeller = as_labeller(etiq)) +
# escalas
scale_x_log10(
breaks = c(.01, .1, 1, 10),
labels = c("0,01%", "0,1%", "1%", "10%")) +
scale_y_continuous(expand = c(0, 0)) +
scale_color_manual(
values = yesno_colores,
labels = c("<b style='color:white;'>SÍ</b>", "<b style='color:black;'>NO</b>")) +
coord_cartesian(clip = "off") +
labs(
title = mi_title,
subtitle = mi_subt,
x = "Porcentaje del total de caracteres del e-mail<br><span style='font-size:10pt'>*escala logarítmica*</span>",
y = "Cantidad",
color = "¿Es spam? ",
caption = mi_caption) +
guides(
color = guide_legend(
override.aes = list(linewidth = 4))
) +
theme_minimal() +
theme(
aspect.ratio = 1,
plot.background = element_rect(
fill = c1, color = c2, linewidth = 3),
plot.margin = margin(15, 20, 10, 15),
plot.title = element_markdown(size = 76, family = "tilt"),
plot.subtitle = element_textbox_simple(
family = "ubuntu", size = 14, color = c3),
plot.caption = element_markdown(
hjust = 1, color = c2, margin = margin(10, 0, 0, 0)),
axis.text.x = element_text(color = c2, family = "victor"),
axis.text.y = element_blank(),
axis.title.x = element_markdown(
color = c3, margin = margin(10, 0, 5, 0), family = "ubuntu", size = 17),
axis.title.y = element_markdown(color = c3, family = "ubuntu", size = 17),
axis.ticks.x = element_line(color = c2),
panel.spacing.x = unit(1, "cm"),
panel.grid = element_blank(),
strip.text = element_markdown(
color = c3, family = "ubuntu", size = 15,
margin = margin(5, 0, 30, 0)),
legend.position = "top",
legend.margin = margin(10, 0, 5, 0),
legend.title = element_text(family = "ubuntu", color = c3, size = 14),
legend.text = element_markdown(family = "ubuntu", size = 12)
)
# guardo
ggsave(
plot = g,
filename = "2023/semana_33/viz.png",
width = 30,
height = 19.17,
units = "cm")
# abro
browseURL("2023/semana_33/viz.png")