Last active
January 30, 2020 13:31
-
-
Save sergiospagnuolo/96c3f537e14eb50d241eaedca13dee6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(cepespR) | |
library(httr) | |
library(jsonlite) | |
library(knitr) | |
codmun <- read.csv("http://cepespdata.io/static/docs/cod_municipios.csv", header = T, sep = ";") | |
codmun <- codmun %>% filter(ANO_ELEICAO == 2016) | |
# do CEPESP: retorna apenas prefeitos eleitos em 2016 | |
# como a tabela retornada get_candidates não traz dados do codigo municipal do IBGE | |
# precisamos carregar a tabela com a informação e fazer o cruzamento | |
prefeitos_eleitos <- get_candidates(year=2016, position="Prefeito", only_elected = T) | |
prefeitos_eleitos$codtse <- as.integer(prefeitos_eleitos$SIGLA_UE) | |
codmun$COD_MUN_TSE <- as.integer(codmun$COD_MUN_TSE) | |
prefeitos_eleitos <- inner_join(prefeitos_eleitos, codmun, by = c("codtse" = "COD_MUN_TSE"), keep = FALSE) | |
# do Atlas da Notícia | |
# gerar token | |
# é necessário criar uma conta no Atlas dA Notícia (www.atlas.jor.br) | |
token = content( | |
POST(url = "https://api.atlas.jor.br/api/v1/auth/login", | |
body = list("email" = "", "password" = ""), | |
encode = "json", handle = NULL), as = "text") | |
token = fromJSON(token) | |
tk <- token[[1]] | |
# extrai dados sobre desertos | |
desertos <- fromJSON(content(GET(url = "https://api.atlas.jor.br/api/v1/data/cities-without-media", | |
add_headers(Authorization = paste("Bearer", tk, sep = " "))), | |
as = "text")) | |
desertos$codmun <- as.integer(desertos$codmun) | |
d <- inner_join(desertos, prefeitos_eleitos, by = c("codmun" = "COD_MUN_IBGE"), keep = FALSE) | |
# começa a análise | |
# prefeitos eleitos, por partido | |
por_partido <- prefeitos_eleitos %>% | |
group_by(SIGLA_PARTIDO) %>% | |
tally(name = "eleitos", sort = T) %>% | |
mutate(pct_total = round((eleitos/sum(eleitos))*100,1)) #%>% | |
#ggplot() + geom_bar(aes(reorder(SIGLA_PARTIDO, -pct_total), pct_total), stat = "identity") | |
kable(por_partido) | |
desertos_partidos <- d %>% | |
group_by(SIGLA_PARTIDO) %>% | |
tally(name = "eleitos_desertos", sort = T) %>% | |
mutate(pct_total_desertos = round((eleitos_desertos/sum(eleitos_desertos))*100,1)) # %>% | |
#ggplot() + geom_bar(aes(reorder(SIGLA_PARTIDO, -pct_total), pct_total), stat = "identity") | |
kable(desertos_partidos) | |
concatenada <- inner_join(por_partido, desertos_partidos) | |
dash_partidos <- concatenada %>% | |
mutate(proporcao = round((eleitos_desertos/eleitos)*100,1)) %>% | |
arrange(desc(proporcao)) %>% | |
# mediana de eleitos | |
filter(eleitos > 62) | |
kable(dash_partidos) | |
# populacao e indices | |
d %>% summarise(media = mean(populacao), mediana = median(populacao)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment