-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_001.R
175 lines (147 loc) · 5.34 KB
/
script_001.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
# inspiración
browseURL("https://nrennie.rbind.io/blog/creating-typewriter-maps-r/")
# uso de {emojifont}
browseURL("https://cran.r-project.org/web/packages/emojifont/vignettes/emojifont.html")
browseURL("https://guangchuangyu.github.io/2015/12/use-emoji-font-in-r/")
# paquetes ----------------------------------------------------------------
library(showtext)
library(glue)
library(emojifont)
library(ggtext)
library(tidyverse)
# fuentes -----------------------------------------------------------------
c1 <- "#FFCE00"
c2 <- "white"
c3 <- "#122361"
# texto gral
font_add_google(name = "Ubuntu", family = "ubuntu")
# rango de alturas
font_add_google(name = "Victor Mono", family = "victor", db_cache = FALSE)
# título
font_add_google(name = "Tsukimi Rounded", family = "tsukimi", db_cache = FALSE)
# emojis
load.emojifont('OpenSansEmoji.ttf')
# íconos
font_add("fa-brands", "icon/Font Awesome 6 Brands-Regular-400.otf")
showtext_auto()
showtext_opts(dpi = 300)
# caption
autor <- glue("<span style='color:{c1};'>**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:{c1};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{autor} {sep} {icon_github} {icon_twitter} {icon_mastodon} {usuario}")
# datos -------------------------------------------------------------------
# mapa de Argentina, POSGAR 2007
arg_sf <- sf::st_read("mapa_topografico/arg_continental.gpkg") |>
sf::st_transform(crs = 5346)
# elevación de Argentina
altura_raster <- elevatr::get_elev_raster(
locations = arg_sf,
z = 1, # el mínimo tamaño de zoom, el de menos datos
clip = "locations")
# convierto el ráster a matriz
altura_matriz <- terra::as.matrix(altura_raster, wide = TRUE)
# cambio los nombres
colnames(altura_matriz) <- 1:ncol(altura_matriz)
# convierto a tabla larga y tibble
altura_tbl <- altura_matriz |>
as_tibble() |>
mutate(y = row_number()) |>
pivot_longer(cols = -y, names_to = "x", values_to = "altura") |>
mutate(x = as.numeric(x)) |>
drop_na()
# emojis de animales
animales <- emoji(
c("rabbit", "cow", "bird", "snake", "mouse", "horse", "bear", "cat"))
# tibble con números consecutivos y emojis
animales_tbl <- data.frame(
rango_numero = seq_len(length(animales)),
valor_emoji = animales)
# combino las alturas con los emojis
d <- altura_tbl |>
mutate(rango = cut_number(altura, n = length(animales))) |>
mutate(rango_numero = as.numeric(rango)) |>
left_join(animales_tbl, by = join_by(rango_numero))
# figura ------------------------------------------------------------------
# paletas de colores/relleno, como rampas de color
f_color <- colorRampPalette(
MoMAColors::moma.colors(palette_name = "Palermo") |> rev())
f_fill <- colorRampPalette(
terrain.colors(8))
# rangos de alturas
d_rangos <- d |>
distinct(rango) |>
separate_wider_delim(
cols = rango, delim = ",", names = c("minimo", "maximo")) |>
mutate(minimo = parse_number(minimo), maximo = parse_number(maximo)) |>
mutate(minimo = if_else(minimo < 0, 0, minimo)) |>
arrange(minimo) |>
mutate(across(everything(), round)) |>
mutate(
label_rango = case_when(
minimo == min(minimo) ~ glue("< {min(maximo)}"),
minimo == max(minimo) ~ glue("> {max(minimo)}"),
.default = glue("{minimo} - {maximo}"))) |>
select(label_rango)
# escala de alturas
d_escala <- tibble(
x = 30,
y = seq(50, 65, length.out = length(animales)),
label = animales,
valor = 1:8) |>
bind_cols(d_rangos)
# figura
g <- ggplot(d, aes(x, y)) +
# topografía de Argentina
geom_tile(
aes(fill = rango_numero), color = c3, linewidth = .7, show.legend = FALSE) +
geom_text(
aes(label = valor_emoji, color = rango_numero), family = "OpenSansEmoji",
show.legend = FALSE, size = 5) +
# escala de topografía de Argentina
geom_tile(
data = d_escala, aes(x, y, fill = valor), color = c3, linewidth = .3,
show.legend = FALSE, width = 2, height = 2) +
geom_text(
data = d_escala, aes(x, y, label = label, color = valor), size = 9,
family = "OpenSansEmoji", show.legend = FALSE) +
# aclaración de la escala
geom_text(
data = d_escala, aes(x+1.3, y, label = label_rango), color = c2, hjust = 0,
family = "victor", show.legend = FALSE, size = 6) +
annotate(
geom = "text", x = min(d_escala$x), y = min(d_escala$y),
label = "Rangos de\naltura (m)", family = "ubuntu", hjust = 0, vjust = -.6,
size = 10, color = c2) +
scale_y_reverse() +
scale_colour_gradientn(colors = f_color(8)) +
scale_fill_gradientn(colors = f_fill(8)) +
labs(title = "ARGENTINA", caption = mi_caption) +
coord_fixed() +
theme_void() +
theme(
plot.margin = margin(t = 8, b = 8),
plot.background = element_rect(
fill = c3, color = c1, linewidth = 3),
plot.title.position = "plot",
plot.title = element_text(
family = "tsukimi", size = 80, hjust = .5, color = c1,
margin = margin(t = 10, b = -50)),
plot.caption = element_markdown(
family = "ubuntu", color = c2, size = 18,
margin = margin(r = 10, b = 10)
)
)
# guardo
ggsave(
plot = g,
filename = "mapa_letras/viz.png",
width = 30,
height = 68,
units = "cm")
# abro
browseURL("mapa_letras/viz.png")