-
-
Save gugat/5336901432030b46574d425bf366ba5d to your computer and use it in GitHub Desktop.
Diferencia entre @yakuperezg y @LassoGuillermo, tweets de @angiegomeza de los resultados del CNE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rtweet) | |
library(tidyverse) | |
library(lubridate) | |
library(gganimate) | |
# Extraer tweets de Andrea Gomez | |
reconteo = search_tweets( | |
q = "angiegomeza AND Actualización AND Diferencia", | |
include_rts = F | |
) | |
reconteo_tidy = reconteo %>% | |
# Seleccionar columnas de interes | |
select(created_at, text) %>% | |
# Cambiar a zona horaria de Ecuador | |
mutate(hora_ec = with_tz(created_at, tzone = "America/Bogota")) %>% | |
# Dividir texto del tweet en cada salto de linea | |
mutate(tmp_chunks = str_split(text, fixed("\n"), n = 5)) %>% | |
# Crear nuevas columnas en funcion de cada linea | |
mutate( | |
yaku = map_chr(tmp_chunks, function(s) s[which(str_detect(s, "Yaku"))]), | |
lasso = map_chr(tmp_chunks, function(s) s[which(str_detect(s, "Lasso"))][1]), | |
diff = map_chr(tmp_chunks, function(s) s[which(str_detect(s, "Diferencia"))]), | |
por_comp = map_chr( | |
tmp_chunks, | |
function(s) { | |
t = s[which(str_detect(s, "Votos"))] | |
if (length(t) == 0) NA_character_ else t | |
} | |
) | |
) %>% | |
# Seleccionar las columnas de interes | |
select(hora_ec, yaku, lasso, diff, por_comp) %>% | |
# Cambiar el separador "." en diferencia (es en miles) | |
mutate_at(vars(diff, por_comp), str_replace, "\\.", "") %>% | |
# Extraer solo valores numericos | |
mutate_if(is.character, str_extract, "\\d+\\.*\\d*") %>% | |
# Convertirlos en valores numericos | |
mutate_if(is.character, as.numeric) %>% | |
# Remover los dos primeros tweets (datos por candidato en millones y no porcentaje) | |
filter_at(vars(yaku, lasso), any_vars(. > 10)) | |
View(reconteo_tidy) | |
max_x_position = max(reconteo_tidy$hora_ec) | |
min_x_position = min(reconteo_tidy$hora_ec) | |
susp_x_start = min_x_position + (3600*9+550) | |
susp_x_end = min_x_position + (3600*15+550) | |
susp_x_mean = min_x_position + (3600*12+550) | |
# Truco para mantener el texto en el ultimo cuadro | |
# Repetir la primera fila con una hora mas | |
last_lasso = reconteo_tidy$lasso[1] | |
last_yaku = reconteo_tidy$yaku[1] | |
last_diff = reconteo_tidy$diff[1] | |
last_por_comp = reconteo_tidy$por_comp[1] | |
reconteo_tidy2 = reconteo_tidy %>% | |
add_row( | |
tibble_row( | |
hora_ec = max_x_position + 1200, | |
yaku = last_yaku, | |
lasso = last_lasso, | |
diff = last_diff, | |
por_comp = last_por_comp | |
), | |
.before = 1 | |
) | |
g = reconteo_tidy2 %>% | |
mutate(label_diff = paste( | |
"Diferencia:", diff, | |
"\nVotos aproximados\npor computar:\n", por_comp) | |
) %>% | |
pivot_longer( | |
-c(hora_ec, diff, por_comp, label_diff), | |
names_to = "candidato", | |
values_to = "porcentaje" | |
) %>% | |
ggplot(aes(x = hora_ec)) + | |
geom_vline(xintercept = susp_x_start, color = 'grey', size = 2) + | |
geom_vline(xintercept = susp_x_end, color = 'grey', size = 2) + | |
geom_line( | |
aes(color = candidato, y = porcentaje), | |
show.legend = T, size = 1.5 | |
) + | |
geom_ribbon( | |
data = reconteo_tidy2, | |
aes(ymin = lasso, ymax = yaku), | |
fill = "gray", alpha = 0.5 | |
) + | |
geom_text( | |
aes(label = label_diff), y = 19.95, x = max_x_position - 1800, | |
size = 5, nudge_x = -5000, check_overlap = T | |
) + | |
geom_label( | |
data = reconteo_tidy2, | |
aes(label = paste0(yaku, "%"), y = yaku), | |
size = 4.5, nudge_x = -500 | |
) + | |
geom_label( | |
data = reconteo_tidy2, | |
aes(label = paste0(lasso, "%"), y = lasso), | |
size = 4.5, nudge_x = -500 | |
) + | |
scale_x_datetime( | |
date_breaks = "3 hours", expand = expansion(mult = c(0, 0.15), add = 0), | |
labels = scales::date_format("%d-%m\n%H:%M", tz = "America/Bogota") | |
) + | |
scale_y_continuous( | |
labels = scales::percent_format(scale = 1, accuracy = 0.1) | |
) + | |
scale_color_manual( | |
"", | |
values = c("deepskyblue3", "purple"), | |
labels = c("Guillermo Lasso", "Yaku Pérez") | |
) + | |
annotate('text', y = 19.9, | |
x = susp_x_mean, label = "Suspención del conteo") + | |
labs( | |
# title = "La carrera por la segunda vuelta...", | |
caption = "Fuente: CNE, @angiegomeza. Visualización: @loreabad6" | |
) + | |
theme_light() + | |
theme( | |
plot.title = element_text(hjust = 0.5), | |
legend.position = "top", | |
axis.title = element_blank(), | |
text = element_text(size = 16) | |
) + | |
transition_reveal( | |
hora_ec, | |
range(min_x_position, max_x_position), | |
keep_last = F | |
) | |
anim_save("conteo_votos.gif", end_pause = 25, | |
duration = 15, | |
animation = g, rewind = F, | |
width = 650, height = 400) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment