-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
170 lines (149 loc) · 4.81 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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#312A56"
c2 <- "#11C638"
c3 <- "#95D69A"
c4 <- "#EF9708"
c5 <- "#F0BC95"
c6 <- "#E2E2E2"
# 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")
# bebas
font_add(
family = "bebas",
regular = "fuente/BebasNeue-Regular.ttf"
)
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{c3};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {21}, ",
"Carbon Majors.</span>")
autor <- glue("<span style='color:{c3};'>**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:{c3};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} {icon_instagram} ",
"{icon_mastodon} {usuario}")
# datos -------------------------------------------------------------------
tuesdata <- tidytuesdayR::tt_load(2024, 21)
emissions <- tuesdata$emissions
# me interesa la relación entre las emisiones de empresas privadas y estatales
d <- emissions |>
filter(parent_type != "Nation State") |>
reframe(
s = sum(total_emissions_MtCO2e),
.by = c(year, parent_type)
) |>
filter(year >= 1900) |>
rename(
año = year,
tipo = parent_type
)
# año en el que las empresas estatales superan el 50% de las emisiones de CO2
d_50 <- d |>
pivot_wider(
names_from = tipo,
values_from = s
) |>
mutate(
tot = `State-owned Entity` + `Investor-owned Company`
) |>
mutate(
estatal = `State-owned Entity`/tot,
privado = `Investor-owned Company`/tot
) |>
arrange(año) |>
filter(between(estatal, .49, .52))
# figura ------------------------------------------------------------------
# subtítulo y aclaración
mi_subtitle <- glue(
"Las empresas productoras de cemento, petróleo, gas y carbón son grandes<br>",
" generadoras de CO<sub>2</sub>. Desde el s. XX, la proporción de las ",
"emisiones causadas<br>por empresas <b style='color: {c4};'>estatales</b>",
" ha aumentado respecto de las <b style='color: {c2};'>privadas</b>."
)
mi_label <- glue(
"A partir del <b>{d_50$año}</b>, las empresas
<b style='color: {c4};'>estatales</b><br>",
"han producido más del <b>50%</b> de las emisiones."
)
# figura
g <- ggplot(d, aes(año, s, fill = tipo, color = tipo)) +
geom_col(position = position_fill(), linewidth = .5, alpha = 1, color = c1) +
geom_hline(
yintercept = .5, color = c6, linewidth = 1, alpha = .6, linetype = "33") +
annotate(
geom = "richtext", x = 1945, y = .6, hjust = .5, vjust = 0,
color = c6, family = "ubuntu", size = 6, label = mi_label, fill = c1,
label.color = NA, label.r = unit(0, "mm"), label.padding = unit(5, "mm")
) +
annotate(
geom = "curve", x = 1975, y = .6, xend = d_50$año-1, yend = d_50$estatal,
color = c6, linewidth = 1,
arrow = arrow(length = unit(3, "mm"), type = "closed", angle = 20)
) +
scale_x_continuous(
breaks = scales::breaks_width(20)
) +
scale_y_continuous(
breaks = scales::breaks_width(.25),
labels = scales::label_percent()
) +
scale_fill_manual(
values = c(c2, c4)
) +
coord_cartesian(expand = FALSE) +
labs(subtitle = mi_subtitle, caption = mi_caption) +
theme_void() +
theme(
aspect.ratio = 1,
plot.margin = margin(22, 30, 10, 23),
plot.background = element_rect(fill = c1, color = c3, linewidth = 3),
plot.subtitle = element_markdown(
family = "ubuntu", size = 20, margin = margin(b = 20), color = c6,
lineheight = unit(1.3, "line")
),
plot.caption = element_markdown(
family = "ubuntu", color = c5, size = 15, margin = margin(t = 25),
lineheight = unit(1.3, "line")
),
panel.grid.major = element_line(
color = c6, linewidth = .2, linetype = "44"
),
axis.text.x = element_text(
family = "bebas", size = 30, color = c6, margin = margin(t = 10)
),
axis.text.y = element_text(
family = "jet", size = 15, color = c6, margin = margin(r = 10), vjust = 0,
hjust = 1
),
legend.position = "none"
)
# guardo
ggsave(
plot = g,
filename = "2024/s21/viz.png",
width = 30,
height = 34,
units = "cm")
# abro
browseURL("2024/s21/viz.png")