-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.R
More file actions
251 lines (208 loc) · 7.03 KB
/
script.R
File metadata and controls
251 lines (208 loc) · 7.03 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# paquetes ----------------------------------------------------------------
library(glue)
library(showtext)
library(ggtext)
library(sf)
library(magick)
library(tidyverse)
# fuentes -----------------------------------------------------------------
# colores
c1 <- "#76BE72"
c2 <- "#F04C44"
c3 <- "#442224"
c4 <- "grey95"
c5 <- "grey92"
# fuente: Ubuntu
font_add(
family = "ubuntu",
regular = "fuentes/Ubuntu-Regular.ttf",
bold = "fuentes/Ubuntu-Bold.ttf",
italic = "fuentes/Ubuntu-Italic.ttf")
# íconos y monoespaciada
font_add(
family = "jet",
regular = "fuentes/JetBrainsMonoNLNerdFontMono-Regular.ttf")
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"<b>Datos:</b> <span style='color:{c2};'>OpenCelliD</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} <b>{icon_github} {icon_twitter} {icon_instagram} ",
"{icon_mastodon}</b> {usuario}")
# datos -------------------------------------------------------------------
# referencia de los nombres de las columnas
# browseURL("https://community.opencellid.org/t/documenting-the-columns-in-the-downloadable-cells-database-csv/186")
# Argentina, continental
arg_sf <- st_read("extras/arg_continental.gpkg") |>
st_transform(crs = 5346)
# extensión de Argentina, para ampliar el mapa
bb <- st_bbox(arg_sf)
ext <- terra::vect(arg_sf) |> terra::ext()
bb_sf <- terra::vect(ext*1.1, crs = "EPSG:5346") |>
st_as_sf() |>
st_bbox()
# datos de las torres de radio
d <- read_csv(
"torres_celular/722.csv",
col_names = 1:14,
col_select = c(1, 7, 8, 12, 13)) |>
rename(radio = X1, lon = X7, lat = X8, primero = X12, ultimo = X13) |>
mutate(primero = as_datetime(primero)) |>
mutate(ultimo = as_datetime(ultimo)) |>
mutate(primero = as.Date(primero)) |>
mutate(ultimo = as.Date(ultimo)) |>
mutate(año = year(primero), mes = month(primero)) |>
mutate(radio = fct(radio, levels = c("GSM", "UMTS", "LTE"))) |>
st_as_sf(coords = c("lon", "lat")) |>
st_set_crs(value = 4326) |>
st_transform(crs = 5346)
# extraigo todos los meses y años en la base de datos
v_año_mes <- tibble(d) |>
distinct(año, mes) |>
mutate(dia = 1) |>
mutate(fecha = make_date(year = año, month = mes, day = dia)) |>
arrange(fecha) |>
pull(fecha)
# máxima cantidad de torres de radio
l_max <- count(tibble(d), radio, sort = TRUE) |>
arrange(radio) |>
pull(n) |>
max()
# paleta de colores por tipo de radio
paleta_radio <- c(c1, c2, c3)
names(paleta_radio) <- c("GSM", "UMTS", "LTE")
# posición mínima/máxima horizontal del segmento
eje_x_min <- bb_sf$xmax*.895
eje_x_max <- bb_sf$xmax*.99
# posición vertical, de los segmentos según tipo de radio
eje_y_gsm <- bb_sf$ymin*1.402
eje_y_umts <- eje_y_gsm - 2.15e5
eje_y_lte <- eje_y_gsm - 2*2.15e5
eje_y_radio <- c(eje_y_gsm, eje_y_umts, eje_y_lte)
names(eje_y_radio) <- c("GSM", "UMTS", "LTE")
# función para convertir la cantidad de torres de radio a longitud del segmento
f_escala <- function(x) {
eje_x_min + x*(eje_x_max - eje_x_min)/l_max
}
# función que genera mapa con los sitios de las torres de radio
f_mapa <- function(año_mes) {
# contador para seguimiento en consola
l <- length(v_año_mes)
n <- which(v_año_mes == año_mes)
n <- case_when(
n <= 9 ~ glue("00{n}"),
n <= 99 ~ glue("0{n}"),
.default = glue("{n}")
)
print(glue("-- {n} de {l} --"))
# etiquetas de año y mes
año_label <- year(año_mes)
mes_label <- str_to_upper(format(año_mes, "%B"))
mi_title <- glue(
"<span style='font-size:20px;'>{año_label}</span><br>",
"<span style='font-size:10px;'>{mes_label}</span>")
# filtro los datos
e <- d |>
filter(primero <= año_mes)
# cantidad de torres, de acuerdo al tipo
# n_radio <- count(e, radio) |>
# pull(n)
# color de la leyenda
label_tbl <- count(e, radio) |>
mutate(color = paleta_radio[radio]) |>
mutate(label = glue("{radio}<br><span style='color:{color}'>{n}</span>")) |>
arrange(radio)
scale_color_names <- pull(label_tbl, radio)
scale_color_labels <- pull(label_tbl, label)
scale_color_values <- pull(label_tbl, color)
# extensión
d_l <- count(tibble(e), radio) |>
arrange(radio) |>
mutate(
x = eje_x_min,
xend = f_escala(n),
y = eje_y_radio[radio]
) |>
mutate(yend = y) |>
mutate(color = paleta_radio[radio])
# figura
g <- ggplot() +
# Argentina
geom_sf(data = arg_sf, fill = c5, color = NA, linewidth = .1) +
# torres de radio
geom_sf(data = e, aes(color = radio, size = radio), alpha = .5) +
# líneas horizontales
geom_segment(
data = d_l, aes(x, y, xend = xend, yend = yend), color = d_l$color,
linewidth = 1) +
# año
annotate(
geom = "text", x = I(.85), y = I(.95), label = año_label, vjust = 0,
family = "jet", size = 6, color = c3) +
# mes
annotate(
geom = "text", x = I(.85), y = I(.944), label = mes_label, vjust = 1,
family = "jet", size = 3, color = c3) +
scale_color_manual(
breaks = scale_color_names,
labels = scale_color_labels,
values = scale_color_values) +
scale_size_manual(values = c(.1, .4, .7)) +
labs(color = NULL, caption = mi_caption) +
coord_sf(
xlim = c(bb_sf$xmin, bb_sf$xmax),
ylim = c(bb_sf$ymin, bb_sf$ymax),
expand = FALSE) +
guides(
color = guide_legend(
position = "inside",
override.aes = list(size = 3, alpha = 1)),
size = guide_none()
) +
theme_void() +
theme(
plot.background = element_rect(fill = c4, color = c3, linewidth = 1),
plot.margin = margin(r = 1, b = 1),
plot.caption = element_markdown(
family = "ubuntu", color = c1, size = 7, lineheight = unit(1.1, "line"),
margin = margin(t = -15, r = 5)),
legend.position.inside = c(0.6, 0.4),
legend.key.spacing.y = unit(.7, "line"),
legend.justification.inside = c(0, 1),
legend.text = element_markdown(family = "jet")
)
# guardo
ggsave(
plot = g,
filename = glue("torres_celular/fig/{n}.png"),
width = 1000,
height = 2172,
units = "px"
)
}
# genero todos los 175 mapas
# LLEVA MUCHO TIEMPO
map(v_año_mes, f_mapa)
# archivos .png de los mapas
pngs <- list.files(path = "torres_celular/fig/", full.names = TRUE)[-c(1, 2)]
# duración del video (s)
dur <- 15
framerate <- length(v_año_mes)/dur
# leo las imágenes
i_pngs <- image_read(pngs)
# guardo .gif
image_write_gif(i_pngs, "torres_celular/viz.gif", delay = 1/framerate)
# guardo .mp4
image_write_video(i_pngs, "torres_celular/viz.mp4", framerate = framerate)
# abro .gif
browseURL("torres_celular/viz.gif")
# abro .mp4
browseURL("torres_celular/viz.mp4")