Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Created November 1, 2024 11:12
Show Gist options
  • Save fernandobarbalho/fe7b3e1c355cdc9a566c217a9d0d9683 to your computer and use it in GitHub Desktop.
Save fernandobarbalho/fe7b3e1c355cdc9a566c217a9d0d9683 to your computer and use it in GitHub Desktop.
Gráficos de resíduos de teste chi-quadrado nas eleições para prefeito 2024
resultado_eleicao_2024 <- read_delim("20241007_151828_eleicao24_prefeitos_vereadores_finalizados.csv",
delim = ";", escape_double = FALSE, trim_ws = TRUE)
candidatos_prefeitos_resultado<-
resultado_eleicao_2024 %>%
filter(cargo == "Prefeito") %>%
select(sg_partido, situacao_candidato_turno)
teste_chisq<-
chisq.test(candidatos_prefeitos_resultado$sg_partido, candidatos_prefeitos_resultado$situacao_candidato_turno,simulate.p.value = TRUE )
tabela_chisq<-
as.data.frame(teste_chisq[["stdres"]])
names(tabela_chisq) <- c("partido", "resultado", "residuo_padrao")
grafico<-
tabela_chisq %>%
filter(resultado == "Eleito") %>%
mutate(partido = reorder(partido, residuo_padrao),
sinal = sign(residuo_padrao)) %>%
ggplot(aes(x= residuo_padrao, y=partido)) +
geom_col(aes(fill = as.character(sinal)), show.legend = FALSE) +
geom_vline(aes(xintercept = -2), linetype= "dashed", color = "black") +
geom_vline(aes(xintercept = 2), linetype= "dashed", color = "black") +
geom_text(aes(y="REDE", x= 8, label= str_wrap("Valores > 2 são diferenças positivas significativas",20))) +
geom_text(aes(y="UNIÃO", x= -8, label= str_wrap("Valores < -2 são diferenças negativas significativas",20))) +
geom_text(aes(y="PC do B", x= 8, label= str_wrap("Cinco partidos desempenharam melhor do que o estatisticamente esperado",20))) +
geom_text(aes(y="MOBILIZA", x= -10, label= str_wrap("PT: pior desempenho comparando o estatisticamente esperado com o realizado",20))) +
scale_fill_discrete_qualitative(palette = "Dark 2") +
theme_light() +
theme(
panel.grid = element_blank()
) +
annotate("segment", x=2, y="REDE", yend = "REDE", xend = 4.5, arrow = arrow(length = unit(0.2, "cm")), linewidth = 1) +
annotate("segment", x=-2, y="UNIÃO", yend = "UNIÃO", xend = -4.5, arrow = arrow(length = unit(0.2, "cm")), linewidth = 1) +
annotate("segment", x=-11, y="REDE", yend = "PSOL", xend = -12, arrow = arrow(length = unit(0.2, "cm")), linewidth = 1)+
labs(title = "Prefeitos eleitos: diferença estatística entre o número esperado e o observado",
subtitle = "Ranking das discrepâncias",
x="Diferença estatística (resíduo padrão de teste chi-quadrado)",
y="",
caption = "Fonte: TSE. Elaboração: Fernando Barbalho")
candidatos_prefeitos_resultado_ne<-
resultado_eleicao_2024 %>%
filter(cargo == "Prefeito",
regiao == "NE") %>%
select(sg_partido, situacao_candidato_turno)
teste_chisq_ne<-
chisq.test(candidatos_prefeitos_resultado_ne$sg_partido, candidatos_prefeitos_resultado_ne$situacao_candidato_turno,simulate.p.value = TRUE )
tabela_chisq_ne<-
as.data.frame(teste_chisq_ne[["stdres"]])
names(tabela_chisq_ne) <- c("partido", "resultado", "residuo_padrao")
grafico<-
tabela_chisq_ne %>%
filter(resultado == "Eleito") %>%
mutate(partido = reorder(partido, residuo_padrao),
sinal = sign(residuo_padrao)) %>%
ggplot(aes(x= residuo_padrao, y=partido)) +
geom_col(aes(fill = as.character(sinal)), show.legend = FALSE) +
geom_vline(aes(xintercept = -2), linetype= "dashed", color = "black") +
geom_vline(aes(xintercept = 2), linetype= "dashed", color = "black") +
geom_text(aes(y="REDE", x= 4.5, label= str_wrap("Valores > 2 são diferenças positivas significativas",20))) +
geom_text(aes(y="UNIÃO", x= -5, label= str_wrap("Valores < -2 são diferenças negativas significativas",20))) +
geom_text(aes(y="PSDB", x= 4.3, label= str_wrap("PSB teve o segundo melhor desempenho",20))) +
scale_fill_discrete_qualitative(palette = "Dark 2") +
theme_light() +
theme(
panel.grid = element_blank()
) +
annotate("segment", x=2, y="REDE", yend = "REDE", xend = 3, arrow = arrow(length = unit(0.2, "cm")), linewidth = 1) +
annotate("segment", x=-2, y="UNIÃO", yend = "UNIÃO", xend = -3, arrow = arrow(length = unit(0.2, "cm")), linewidth = 1) +
annotate("curve", x=5, y="PC do B", yend = "PSB", xend = 5.2, arrow = arrow(length = unit(0.2, "cm")), linewidth = 1)+
labs(title = "Prefeitos eleitos: diferença estatística entre o número esperado e o observado",
subtitle = "Ranking das discrepâncias no Nordeste",
x="Diferença estatística (resíduo padrão de teste chi-quadrado)",
y="",
caption = "Fonte: TSE. Elaboração: Fernando Barbalho")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment