Ver código
library(tidyverse)
library(plotly)
library(lubridate)
library(ggrepel)
library(DT)
library(RColorBrewer)
# Encuestas ####
encuestas_ulr_2022 <- "https://raw.githubusercontent.com/nelsonamayad/Elecciones-presidenciales-2022/main/Encuestas%202022/encuestas_2022.csv"
encuestas_2022 <- readr::read_csv(encuestas_ulr_2022)
# Parametros ####
shape_entrada_2022 <- c(22,4,23,16,17,21,12,5,18,19)
colors_entrada_2022 <- c(
"Alejandro Gaviria" = rgb(50,205,50, maxColorValue = 255),
"Sergio Fajardo" = rgb(31,161,46, maxColorValue = 255),
"Juan Manuel Galan" = rgb(213,48,62, maxColorValue = 255),
"Ingrid Betancourt" = rgb(14,185,11, maxColorValue = 255),
"Alex Char" = rgb(228,0,120, maxColorValue = 255),
"David Barguil" = rgb(0,97,169, maxColorValue = 255),
"Enrique Peñalosa" = rgb(0,139,139, maxColorValue = 255),
"Federico Gutierrez" = rgb(0,0,255, maxColorValue = 255),
"Oscar I. Zuluaga" = rgb(30,144,255, maxColorValue = 255),
"Gustavo Petro" = rgb(128,0,128, maxColorValue = 255),
"Rodolfo Hernandez" = rgb(247,190,10, maxColorValue = 255),
"Voto en blanco" = rgb(32,33,36, maxColorValue = 255)
)
# ggplot entrada 2022 ####
entrada_2022 <- encuestas_2022 %>%
# Seleccionar candidatos que encabezan las encuestas
dplyr::select(n,fecha,encuestadora,contains("_"), blanco,otros,muestra,merror=margen_error) %>%
# Pivotear los datos
tidyr::pivot_longer(cols = c(contains("_"),"blanco","otros"),
names_to = "candidato", values_to = "int_voto") %>%
# Crear algunas variables
dplyr::mutate(e_max = int_voto + merror,
e_min = int_voto - merror,
fecha = lubridate::as_date(fecha),
nombres = case_when(candidato=="alejandro_gaviria" ~ "Alejandro Gaviria",
candidato=="sergio_fajardo" ~ "Sergio Fajardo",
candidato=="juan_m_galan" ~ "Juan Manuel Galan",
candidato=="ingrid_betancourt" ~ "Ingrid Betancourt",
candidato=="alex_char" ~ "Alex Char",
candidato=="david_barguil" ~ "David Barguil",
candidato=="enrique_penalosa" ~ "Enrique Peñalosa",
candidato=="federico_gutierrez" ~ "Federico Gutierrez",
candidato=="oscar_i_zuluaga" ~ "Oscar I. Zuluaga",
candidato=="rodolfo_hernandez" ~ "Rodolfo Hernandez",
candidato=="gustavo_petro" ~ "Gustavo Petro",
candidato=="blanco" ~ "Voto en blanco",
) %>%
factor(),
order_colors = as.numeric(nombres)) %>%
# Filter non candidates
dplyr::filter(!is.na(int_voto), !candidato %in% c("otros","ns_nr"),!is.na(nombres)) %>%
# ggplot
ggplot(aes(x=fecha,y=int_voto, color=nombres))+ #,frame=lubridate::month(fecha)
# Antes de consulta 13 de marzo
geom_point(data = . %>% dplyr::filter(fecha<=lubridate::as_date("2022-03-13")),
aes(shape=encuestadora,
text = paste('Candidato: ', nombres,
'<br>Intencion de voto:', int_voto,
'<br>Encuestadora:',encuestadora,
'<br>Fecha:', fecha)),
position = "jitter",
size=4,
show.legend = FALSE)+
# Despues de consulta 13 de marzo
geom_point(data = . %>% dplyr::filter(between(fecha,
lubridate::as_date("2022-03-13"),
lubridate::as_date("2022-05-29")),
!is.na(int_voto)),
aes(shape=encuestadora,
text = paste('Candidato: ', nombres,
'<br>Intencion de voto:', int_voto,
'<br>Encuestadora:',encuestadora,
'<br>Fecha:', fecha)),
position = "jitter",
size=4,
show.legend = FALSE)+
# Despues de 1era vuelta
geom_point(data = . %>% dplyr::filter(fecha>=lubridate::as_date("2022-05-29", !is.na(int_voto))),
aes(shape=encuestadora,
text = paste('Candidato: ', nombres,
'<br>Intencion de voto:', int_voto,
'<br>Encuestadora:',encuestadora,
'<br>Fecha:', fecha)),
position = "jitter",
size=4,
show.legend = FALSE)+
# Resultados primera vuelta
geom_point(data = tribble(~fecha,~int_voto,~nombres,
lubridate::as_date("2022-05-29"),39.84,"Gustavo Petro",
lubridate::as_date("2022-05-29"),23.64,"Federico Gutierrez",
lubridate::as_date("2022-05-29"),4.13,"Sergio Fajardo",
lubridate::as_date("2022-05-29"),27.82,"Rodolfo Hernandez"),
aes(
text = paste('Candidato: ', nombres,
'<br>Resultado:', int_voto)),
position = "jitter",
size=8,
shape=10,
show.legend = FALSE
) +
# Resultados primera vuelta
geom_point(data = tribble(~fecha,~int_voto,~nombres,
lubridate::as_date("2022-06-19"),50.44,"Gustavo Petro",
lubridate::as_date("2022-06-19"),47.31,"Rodolfo Hernandez"),
aes(
text = paste('Candidato: ', nombres,
'<br>Resultado:', int_voto)),
position = "jitter",
size=8,
shape=10,
show.legend = FALSE
) +
# LOESS antes de la primera vuelta
#geom_smooth(
# data = . %>% dplyr::filter(between(fecha,
# lubridate::as_date("2022-03-13"),
# lubridate::as_date("2022-05-29")),
# !is.na(int_voto)),
# method="loess", linetype="dotdash", se=TRUE, show.legend = FALSE)+
# LOESS para segunda vuelta
#geom_smooth(
# data = . %>% dplyr::filter(fecha>=lubridate::as_date("2022-03-13"),
# !is.na(int_voto),candidato %in% c(" rodolfo_hernandez","gustavo_petro")
# ),
# method="loess", linetype="dotdash", fill="orangered",se=TRUE, show.legend = FALSE)+
# LOESS final
geom_smooth(method="loess", linetype="dotdash",se=TRUE, show.legend = FALSE)+
# Margenes de error
geom_linerange(aes(ymax=e_max, ymin=e_min),
position = "jitter",
show.legend = FALSE) +
# Legislativas
geom_vline(xintercept = as.numeric(lubridate::as_date("2022-03-13")), color="royalblue2")+
# 1era vuelta
geom_vline(xintercept = as.numeric(lubridate::as_date("2022-05-29")), color="orangered")+
# 2da vuelta
geom_vline(xintercept = as.numeric(lubridate::as_date("2022-06-19")), color="red")+
# Opciones
labs(x="",
y="% Intención voto",
caption="Fuente: Encuestas de intención de voto")+
scale_shape_manual(values=shape_entrada_2022)+
scale_color_manual(values=colors_entrada_2022)+
coord_cartesian(ylim=c(0,60))+
theme(
legend.position="none",
panel.background=element_rect(fill="white", color="white")
)
#ggplotly ####
ggplotly(entrada_2022, tooltip = "text")Entrada 2022: Todas las encuestas juntas