Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Created October 31, 2024 10:53
Show Gist options
  • Save fernandobarbalho/31a92007d278c1750d810ad09c995ae1 to your computer and use it in GitHub Desktop.
Save fernandobarbalho/31a92007d278c1750d810ad09c995ae1 to your computer and use it in GitHub Desktop.
Mapas resultados eleições no Ceará e Piauí
library(readr)
library(tidyverse)
library(geobr)
library(sf)
library(colorspace)
estados_sf<- geobr::read_state()
resultado_eleicao_2024 <- read_delim("20241007_151828_eleicao24_prefeitos_vereadores_finalizados.csv",
delim = ";", escape_double = FALSE, trim_ws = TRUE)
num_municipios_estado<-
resultado_eleicao_2024 %>%
distinct(uf, cd_municipio_ibge ) %>%
summarise(num_municipios = n(),
.by = uf )
dados_grafico_ce_pi<-
resultado_eleicao_2024 %>%
filter(cargo == "Prefeito",
situacao_candidato_turno == "Eleito",
uf %in% c("CE","PI")
) %>%
summarise( quantidade = n(),
.by =c(sg_partido, uf) ) %>%
mutate(sg_partido =fct_reorder(sg_partido, quantidade, sum, .desc = TRUE)) %>%
inner_join(num_municipios_estado) %>%
mutate(percentual_eleito = (quantidade/num_municipios)*100) %>%
#filter(sg_partido %in% partidos_filtro) %>%
left_join(
estados_sf %>%
rename(uf = abbrev_state)
) %>%
mutate(percentual_eleito = ifelse(is.na(percentual_eleito),0, percentual_eleito))
dados_grafico_ce_pi %>%
ggplot() +
geom_sf( aes(fill= percentual_eleito, geometry = geom)) +
scale_fill_continuous_sequential(palette = "Heat 2")+
theme_void() +
labs(
title = "Proporção de prefeitos eleitos por estado e partido",
subtitle = "Eleições 2024",
fill= "(%)",
caption = "Fonte: TSE. Elaboração: Fernando Barbalho"
) +
facet_wrap(sg_partido ~.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment