-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
159 lines (139 loc) · 4.45 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
# paquetes ----------------------------------------------------------------
library(glue)
library(ggtext)
library(showtext)
library(ggthemes)
library(tidyverse)
# fuente ------------------------------------------------------------------
# colores
c1 <- "#1F77B4"
c2 <- "#E377C2"
c3 <- "#9467BD"
c4 <- "grey90"
c5 <- "#F0F0F0"
# 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"
)
showtext_auto()
showtext_opts(dpi = 300)
# caption
fuente <- glue(
"Datos: <span style='color:{c3};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana {37}, ",
"<b>Opportunity Insights</b>.</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, 37)
college_admissions <- tuesdata$college_admissions
# me interesa comparar la admisión a universidades top y públicas, según los
# ingresos del hogar
d <- college_admissions |>
select(par_income_lab, par_income_bin, rel_att_cond_app, tier) |>
filter(
tier %in% c("Ivy Plus", "Selective public")
) |>
mutate(par_income_lab = fct_reorder(par_income_lab, par_income_bin)) |>
drop_na()
# figura ------------------------------------------------------------------
# subtítulo y títulos de eje
mi_subtítulo <- glue(
"Las <b style='color:{c1};'>Universidades de Elite</b> tienden a aceptar",
"las solicitudes de inscripción de los más pudientes,",
"comparado con las <b style='color:{c2};'>Universidades Públicas</b>.",
.sep = "<br>"
)
eje_x <- glue(
"Relación entre la asistencia",
"y las aplicaciones universitarias",
.sep = "<br>"
)
eje_y <- glue(
"Percentil de ingresos",
"en el hogar",
.sep = "<br>"
)
# figura
g <- ggplot(
d,
aes(rel_att_cond_app, par_income_lab, color = tier, shape = tier)
) +
geom_jitter(alpha = .4, size = 7, height = .2) +
scale_color_manual(
values = c(c1, c2),
breaks = c("Ivy Plus", "Selective public"),
labels = c("Ivy Plus", "Pública selectiva")
) +
scale_shape_manual(
values = c(18, 20),
breaks = c("Ivy Plus", "Selective public"),
labels = c("Ivy Plus", "Pública selectiva")
) +
scale_x_continuous(
minor_breaks = scales::breaks_width(.1)
) +
labs(
x = eje_x, y = eje_y, color = NULL, shape = NULL, subtitle = mi_subtítulo,
caption = mi_caption) +
guides(
color = guide_legend(
override.aes = list(alpha = 1), ncol = 1)
) +
theme_fivethirtyeight() +
theme(
aspect.ratio = 1,
plot.title.position = "plot",
plot.background = element_rect(fill = c4),
plot.subtitle = element_markdown(
family = "ubuntu", size = 20, lineheight = unit(1.3, "line"), fill = c5,
padding = unit(.2, "cm")
),
plot.caption = element_markdown(
family = "ubuntu", size = 11, color = c1, hjust = 1,
lineheight = unit(1.2, "line")
),
panel.grid = element_line(linetype = "55"),
axis.text = element_text(family = "jet", size = 12),
axis.title.y = element_markdown(
family = "ubuntu", size = 15, angle = 0, vjust = .5, hjust = 1,
lineheight = unit(1.3, "line"), fill = c5, padding = unit(.1, "cm")
),
axis.title.x = element_markdown(
family = "ubuntu", size = 15, lineheight = unit(1.3, "line"), hjust = 0,
margin = margin(t = 5), fill = c5, padding = unit(.1, "cm")
),
legend.text = element_text(family = "ubuntu", size = 12),
legend.position = "inside",
legend.background = element_rect(fill = c5),
legend.key = element_blank(),
legend.justification.inside = c(1, .5),
legend.position.inside = c(1, .5)
)
# guardo
ggsave(
plot = g,
filename = "2024/s37/viz.png",
width = 30,
height = 26,
units = "cm")
# abro
browseURL(glue("{getwd()}/2024/s37/viz.png"))