-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-s40_script.R
170 lines (143 loc) · 5.8 KB
/
2023-s40_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(tidyverse)
library(glue)
library(ggrepel)
library(ggtext)
library(showtext)
# fuente ------------------------------------------------------------------
# colores, Isfahan
c1 <- "#E3C28B"
c2 <- "#AE8448"
c3 <- "#175F5D"
c4 <- "#4E3810"
c5 <- "#054544"
# agencias, texto gral
font_add_google(name = "Ubuntu", family = "ubuntu")
# ejes vertical horizontal
font_add_google(name = "Victor Mono", family = "victor", db_cache = FALSE)
# título
font_add_google(name = "Lora", family = "lora")
# í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:victor;'>{{<b>tidytuesdayR</b>}}</span> semana 40. US Government Grant Opportunities, *grants.gov*</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-10-03/readme.md")
grants <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-10-03/grants.csv')
# me interesa la relación entre cantidad de oportunidades de las agencias, y
# la cantidad de dinero estimada, por oprtunidad
# selecciono las 100 agencias con mayor presencia
agencia <- grants |>
drop_na(estimated_funding) |>
count(agency_name, sort = TRUE) |>
slice(1:100) |>
pull(agency_name)
# calculo el cociente entre dinero estimado y número de presentaciones
# obtengo la distancia de cada punto respecto del origen
d <- grants |>
filter(agency_name %in% agencia) |>
drop_na(estimated_funding) |>
summarise(
cantidad = n(),
tot = sum(estimated_funding),
.by = agency_name) |>
mutate(ratio = tot/cantidad) |>
mutate(distancia = sqrt(tot^2 + ratio^2))
# puntos extremos
e <- d |>
filter(
cantidad == max(d$cantidad) |
cantidad == min(d$cantidad) |
ratio == max(d$ratio) | ratio == min(d$ratio) |
distancia == max(d$distancia) | distancia == min(d$distancia)) |>
mutate(label = str_wrap(agency_name, width = 20)) |>
mutate(hjust = c())
# selecciono dos casos extremos, de muchas presentaciones con poca plata,
# y pocas presentaciones con mucha plata
nps <- e |>
filter(agency_name == "National Park Service")
nps_cantidad <- nps$cantidad |>
gt::vec_fmt_number(sep_mark = ".", decimals = 0, dec_mark = ",")
nps_ratio <- nps$ratio |>
gt::vec_fmt_number(sep_mark = ".", decimals = 0, dec_mark = ",")
afo <- e |>
filter(agency_name == "Air Force Office of Scientific Research")
afo_cantidad <- afo$cantidad |>
gt::vec_fmt_number(sep_mark = ".", decimals = 0, dec_mark = ",")
afo_ratio <- afo$ratio |>
gt::vec_fmt_number(sep_mark = ".", decimals = 0, dec_mark = ",")
# figura ------------------------------------------------------------------
# tpitulo & subtítulo
mi_title <- "Subvenciones by *grants.gov*"
mi_sub <- glue(
"Cuantas más oportunidades de subvención ofrece un organismo del
Gobierno de<br>**EEUU**, menor es el monto disponible. *{nps$agency_name}*
presentó {nps_cantidad}<br>oportunidades de financiamiento,
con un valor medio de USD $*{nps_ratio}*. En cambio,<br>en solo {afo_cantidad}
veces, *{afo$agency_name}* ofreció USD $*{afo_ratio}*.")
# figura
g <- d |>
mutate(tipo = agency_name %in% e$agency_name) |>
ggplot(aes(cantidad, ratio, color = tipo, alpha = tipo)) +
geom_point(size = 8, show.legend = FALSE) +
geom_text_repel(
data = e, aes(cantidad, ratio, label = label), inherit.aes = FALSE,
hjust = 0, color = c5, seed = 2024, nudge_y = .175, family = "ubuntu",
size = 5) +
scale_x_log10(
labels = scales::label_number(decimal.mark = ",", big.mark = ".")) +
scale_y_log10(
breaks = c(1e5, 1e6, 10e6, 100e6),
labels = glue("USD {c('0,1', '1', '10', '100')}M")) +
scale_color_manual(values = c(c2, c3)) +
scale_alpha_manual(values = c(.7, 1)) +
coord_cartesian(clip = "off") +
labs(
title = mi_title,
subtitle = mi_sub,
x = "# de oportunidades",
y = "$ estimado, por oportunidad",
caption = mi_caption) +
theme_minimal() +
theme(
aspect.ratio = 1,
plot.margin = margin(15, 32, 15, 12),
plot.background = element_rect(fill = c1, color = c2, linewidth = 3),
plot.title = element_markdown(
size = 62, color = c5, family = "lora", hjust = .5),
plot.title.position = "plot",
plot.subtitle = element_markdown(
family = "ubuntu", size = 21, color = c3, margin = margin(5, 0, 25, 0),
lineheight = unit(1.1, "line"), hjust = .5),
plot.caption = element_markdown(color = c2, family = "ubuntu", size = 12),
axis.text = element_text(family = "victor", color = c4, size = 15),
axis.text.x = element_text(hjust = 1),
axis.text.y = element_text(vjust = 0),
axis.title.x = element_text(
family = "ubuntu", size = 22, hjust = 1, color = c5,
margin = margin(20, 0, 20, 0)),
axis.title.y = element_text(
family = "ubuntu", size = 22, hjust = 1, color = c5,
margin = margin(0, 20, 0, 20)),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(
color = c2, linewidth = .3, linetype = "8f")
)
# guardo
ggsave(
plot = g,
filename = "2023/semana_40/viz.png",
width = 30,
height = 35,
units = "cm")
# abro
browseURL("2023/semana_40/viz.png")