-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
187 lines (163 loc) · 5.24 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#4FB6CA"
c2 <- "#178F92"
c3 <- "#175F5D"
c4 <- "#054544"
c5 <- "#4E3810"
c6 <- "grey30"
c7 <- "#EDE2CC"
arcoiris <- c("#E40303", "#FF8C00", "#FFED00", "#008026", "#24408E", "#732982")
# 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 neue
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 {24}, ",
"Campus Pride Index.</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, 24)
pride_index <- tuesdata$pride_index
pride_index_tags <- tuesdata$pride_index_tags
# me interesa analizar el índice agrupado por estado, y la cantidad de
# estudiantes
d <- inner_join(
pride_index_tags,
pride_index,
by = join_by(campus_name, campus_location)
) |>
mutate(
estado = str_sub(campus_location, start = -2, end = -1), .before = 2
) |>
select(rating, estado, students) |>
reframe(
r = median(rating),
s = sum(students),
.by = estado
) |>
mutate(estado = fct_reorder(estado, r)) |>
arrange(estado) |>
mutate(
label_x = if_else(
row_number() %% 2 == 0,
r - .17,
r + .17
)
)
# figura ------------------------------------------------------------------
# estrellas en el eje horizontal
estrella <- glue(
"<span style='font-family:jet;'></span>")
eje_x <- seq(1, 5, 1)
eje_x_nombres <- c(
estrella,
glue("{str_flatten(rep(estrella, 2))}"),
glue("{str_flatten(rep(estrella, 3))}"),
glue("{str_flatten(rep(estrella, 4))}"),
glue("{str_flatten(rep(estrella, 5))}")
)
names(eje_x) <- eje_x_nombres
# subtítulo
mi_subtitulo <- glue(
"<br><br>El <b>Campus Pride Index</b>, de rango 1 a 5,<br>",
"permite puntuar la tolerancia a la comunidad<br>",
"<b style='color:{c1}'>LGBTQ</b> en ámbitos universitarios de EE.UU.<br><br>",
"Tomando {nrow(pride_index)} instituciones, cada círculo<br>",
"representa la mediana del índice en las<br>",
"universidades de cada Estado, siendo el<br>",
"tamaño proporcional al número de<br>",
"estudiantes."
)
# figura
g <- ggplot(d, aes(r, estado)) +
geom_point(alpha = .7, aes(size = s, color = r)) +
geom_text(
aes(label = estado, x = label_x), family = "jet", color = c6,
fontface = "bold", size = 6, show.legend = FALSE) +
annotate(
geom = "richtext", x = I(.01), y = I(nrow(d)), hjust = 0, vjust = 1,
family = "ubuntu", label = mi_subtitulo, size = 8, color = c6, fill = c7,
label.color = NA
) +
scale_x_continuous(
breaks = eje_x,
limits = c(1, 5.2),
labels = names(eje_x)
) +
scale_color_gradientn(colors = arcoiris) +
scale_size_binned(
breaks = c(1e5, 2e5),
labels = c("100.000", "200.000"),
range = c(1, 20)
) +
coord_cartesian(clip = "off", expand = FALSE) +
labs(
x = NULL, y = NULL, caption = mi_caption,
size = "Cantidad total<br>de estudiantes<br>universitarios"
) +
guides(
color = guide_none(),
size = guide_bins(
override.aes = list(fill = NA, color = c6),
position = "inside")
) +
theme_minimal() +
theme(
aspect.ratio = 1.3,
plot.margin = margin(t = 21.3, b = 10, l = 37, r = 37),
plot.background = element_rect(fill = c7, color = c2, linewidth = 3),
plot.caption = element_markdown(
color = c5, family = "ubuntu", size = 16, margin = margin(t = 13)),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(
linewidth = .2, linetype = "66", color = c6),
panel.grid.minor.x = element_line(
linewidth = .1, linetype = "66", color = c6),
axis.text.x = element_markdown(
size = 40, color = c5, margin = margin(t = 14)),
axis.text.y = element_blank(),
legend.justification.inside = c(.01, .4),
legend.text = element_text(family = "jet", size = 20, color = c6),
legend.background = element_rect(fill = c7, color = NA),
legend.title = element_markdown(family = "ubuntu", size = 20, color = c6)
)
# guardo
ggsave(
plot = g,
filename = "2024/s24/viz.png",
width = 30,
height = 40,
units = "cm")
# abro
browseURL("2024/s24/viz.png")