Skip to content

Instantly share code, notes, and snippets.

@cndesantana
Created November 26, 2020 11:03
Show Gist options
  • Save cndesantana/5dd23d05e183557dcf9482c3d0041228 to your computer and use it in GitHub Desktop.
Save cndesantana/5dd23d05e183557dcf9482c3d0041228 to your computer and use it in GitHub Desktop.
library(data.table)
library(tidyverse)
library(sf)
library(readxl)
library(OpenStreetMap)
library(rgdal)
library(osmar)
library(leaflet)
library(htmlwidgets)
library(viridis)
setwd("~/Dadoscope/eleicoes_sp/R")
# ler dados
votos <- fread("../dados/votacao_secao_2020_SP.csv")
mapa <- read_sf("../dados/zonaseleitorais2.shp")
# codigo da cidade de Sao Paulo
votos <- votos %>% filter(CD_MUNICIPIO == "71072")
votos$NR_ZONA <- as.character(votos$NR_ZONA)
# partidos progressistas
progressistas <- c(13,50,40,65,16,29)
branco_nulo <- c(95, 96)
## mapa progressistas
mapa_progressistas <- mapa %>% left_join(
votos %>% ungroup()%>%
group_by(NR_ZONA) %>% mutate(total = sum(QT_VOTOS)) %>% ungroup() %>%
filter(DS_CARGO == "PREFEITO", NR_VOTAVEL %in% progressistas) %>%
select(NR_ZONA,QT_VOTOS,total)%>%
group_by(NR_ZONA, total) %>% summarise(votos = sum(QT_VOTOS)) %>% ungroup() %>% mutate(perc = signif(100*votos/total,4)), by = c("Name"="NR_ZONA"))
## mapa branco_nulo
mapa_branco_nulo <- mapa %>% left_join(
votos %>% ungroup()%>%
group_by(NR_ZONA) %>% mutate(total = sum(QT_VOTOS)) %>% ungroup() %>%
filter(DS_CARGO == "PREFEITO", NR_VOTAVEL %in% branco_nulo) %>%
select(NR_ZONA,QT_VOTOS,total)%>%
group_by(NR_ZONA, total) %>% summarise(votos = sum(QT_VOTOS)) %>% ungroup() %>% mutate(perc = signif(100*votos/total,4)), by = c("Name"="NR_ZONA"))
## mapa conservadores
mapa_conservadores <- mapa %>% full_join(
votos %>% ungroup()%>%
group_by(NR_ZONA) %>% mutate(total = sum(QT_VOTOS)) %>% ungroup() %>%
filter(DS_CARGO == "PREFEITO", !NR_VOTAVEL %in% branco_nulo, !NR_VOTAVEL %in% progressistas) %>%
select(NR_ZONA,QT_VOTOS,total)%>%
group_by(NR_ZONA, total) %>% summarise(votos = sum(QT_VOTOS)) %>% ungroup() %>% mutate(perc = signif(100*votos/total,4)), by = c("Name"="NR_ZONA"))
library(leaflet)
library(mapview)
plotMapa <- function(newmap,sufix,cand){
goodmap <- st_zm(newmap, drop=T, what="ZM")
pal <- colorBin("RdBu", domain = goodmap$perc, bins = 10,reverse = TRUE )
labels <- paste0("ZONA ", goodmap$Name,": ", goodmap$perc,"%") %>%
lapply(htmltools::HTML)
leaflet(goodmap) %>%
addPolygons(fillColor = ~ pal(perc),
weight = 1,
color=~ "white",
fillOpacity = 0.8,
dashArray = "1",
label = labels) %>%
addTiles() %>%
addLegend("bottomright",
pal = pal,
values=n,
title =paste0("% de votos em ",cand," - 2020")) %>%
htmlwidgets::saveWidget(file=paste0("mapa_votos_",sufix,".html"))
}
plotMapa(mapa_branco_nulo,"branco_nulo_2020","Branco e Nulo")
plotMapa(mapa_conservadores,"conservadores_2020","Conservadores")
plotMapa(mapa_progressistas,"progressistas_2020","Progressistas")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment