Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Created October 31, 2024 10:53
Show Gist options
  • Select an option

  • Save fernandobarbalho/31a92007d278c1750d810ad09c995ae1 to your computer and use it in GitHub Desktop.

Select an option

Save fernandobarbalho/31a92007d278c1750d810ad09c995ae1 to your computer and use it in GitHub Desktop.

Revisions

  1. fernandobarbalho created this gist Oct 31, 2024.
    48 changes: 48 additions & 0 deletions prefeitos_ce_pi_2024.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    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 ~.)