Last active
January 31, 2020 20:45
-
-
Save sergiospagnuolo/773d041d7e0945a8e3998cd525ca7db9 to your computer and use it in GitHub Desktop.
Itera entre diversos anos para buscar resultados sobre benefícios pagos pelo governo federal
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(jsonlite) | |
library(lubridate) | |
library(deflateBR) | |
library(scales) | |
# contexto: http://www.agricultura.gov.br/noticias/decreto-permite-pagamento-do-seguro-defeso-aos-pescadores-afetados-pelo-oleo | |
# http://www.portaltransparencia.gov.br/beneficios/consulta | |
url1 <- "http://www.portaltransparencia.gov.br/beneficios/consulta/resultado?paginacaoSimples=false&tamanhoPagina=250000&offset=0&direcaoOrdenacao=desc&colunaOrdenacao=mesAno&de=" | |
url2 <- "&colunasSelecionadas=linkDetalhamento%2ClinguagemCidada%2CmesAno%2Cuf%2Cmunicipio%2Cvalor&_=1574703194664" | |
anos <- c('01%2F01%2F2019&ate=31%2F12%2F2019', | |
'01%2F01%2F2018&ate=31%2F12%2F2018', | |
'01%2F01%2F2017&ate=31%2F12%2F2017', | |
'01%2F01%2F2016&ate=31%2F12%2F2016', | |
'01%2F01%2F2015&ate=31%2F12%2F2015') | |
lista <- list() | |
for(ano in anos){ | |
# monta a URL | |
url <- paste0(url1, ano, url2) | |
# puxa os dados | |
df <- jsonlite::fromJSON(url) | |
# cria o data frame | |
d <- df[["data"]] | |
# limpa coluna que dá pau | |
lista[[ano]] <- subset(d, select = -c(filtros)) | |
} | |
agregado <- bind_rows(lista) | |
# limpa os valores | |
agregado$valor <- gsub("\\.", "", agregado$valor) | |
agregado$valor <- gsub("\\,", ".", agregado$valor) | |
agregado$valor <- as.numeric(agregado$valor) | |
agregado$mesAno <- gsub("\\/", "-", agregado$mesAno) | |
# formata as datas | |
agregado$mesAno <- as.Date(paste0('01-', agregado$mesAno), format="%d-%m-%Y") | |
agregado$ajustado <- deflate(agregado$valor, agregado$mesAno, "12/2019", "ipca") | |
unique(agregado$linguagemCidada) | |
# & uf %in% c("BA","CE","PE", "RN", "MA", "PI", "SE", "PB", "AL")) | |
agregado %>% | |
drop_na() %>% | |
filter(linguagemCidada == 'Seguro Defeso' & uf %in% c("BA","CE","PE", "RN", "MA", "PI", "SE", "PB", "AL")) %>% | |
select(mesAno, uf, ajustado) %>% | |
mutate(ano = format(mesAno, "%Y"), ajustado = ajustado/1000000) %>% | |
group_by(uf, ano) %>% | |
summarise(total = sum(ajustado)) %>% | |
ggplot(aes(ano, total, fill = uf, color = uf)) + geom_bar(stat = "identity", position = "dodge") + | |
facet_wrap( ~ uf, ncol = 3, scales = "free") + | |
labs( title = "Desembolsos do Seguro Defeso", | |
subtitle = "Valor anual desembolsado no Nordeste pelo programa Seguro Defeso (para pescadores artesanais)\nValores deflacinados pelo IPCA // Escalas variam entre UFs", | |
caption = "Fonte: Portal da Transparência", | |
x = "", | |
y = "em milhões de R$") | |
#scale_x_date(date_labels = "%b %Y", breaks = pretty_breaks(10)) + | |
# scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE)) + | |
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(jsonlite) | |
library(lubridate) | |
library(deflateBR) | |
library(scales) | |
url <- "http://aplicacoes.mds.gov.br/sagi/servicos/misocial?q=*&fq=anomes_s:" | |
url2 <- "*&fq=tipo_s:mes_mu&wt=csv&fl=ibge:codigo_ibge,anomes:anomes_s,qtd_familias_beneficiarias_bolsa_familia,valor_repassado_bolsa_familia&rows=10000000&sort=anomes_s%20asc,%20codigo_ibge%20asc" | |
anos <- c(2004:2019) | |
bf <- list() | |
for(p in seq_along(anos)){ | |
# monta a URL | |
url0 <- paste0(url, p, url2) | |
# puxa os dados | |
dfa <- read.csv(url0, header = T) | |
# preenche a lista | |
bf[[p]] = dfa | |
} | |
total <- bind_rows(bf) | |
total$ano <- as.numeric(str_sub(total$anomes, 1,4)) | |
total$mes <- str_sub(total$anomes, 5,6) | |
total$data <- as.Date(paste0(total$ano, "-", total$mes, "-01")) | |
total$ajustado <- deflate(total$valor_repassado_bolsa_familia, total$data, "12/2019", "ipca") | |
# total de famílias recipientes, por mes | |
total %>% | |
drop_na() %>% | |
#filter(data > "2009-01-01") %>% | |
group_by(ano, mes) %>% | |
summarise(soma = sum(qtd_familias_beneficiarias_bolsa_familia)) %>% | |
summarise(soma = mean(soma)) %>% | |
ggplot(aes(ano,soma)) + geom_line() + | |
geom_smooth(method = "loess", se = FALSE) + | |
scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE)) | |
# total de famílias recipientes, por ano | |
familias <- total %>% | |
drop_na() %>% | |
#filter(data > "2009-01-01") %>% | |
group_by(ano, mes) %>% | |
summarise(soma = sum(qtd_familias_beneficiarias_bolsa_familia)) %>% | |
summarise(soma = mean(soma)) %>% | |
mutate(soma = soma/1000000) %>% | |
#summarise(soma = mean(soma)) %>% | |
ggplot(aes(ano, soma, label = round(soma, 1))) + geom_line() + geom_label() + | |
#geom_smooth(method = "loess", se = FALSE) + | |
#scale_x_date(breaks = scales::pretty_breaks(n = 10)) + | |
scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE)) + | |
labs( title = "Beneficiários do Bolsa Família", | |
subtitle = "Média mensal de beneficiários do Bolsa Família, agrupada por ano", | |
caption = "Fonte: Portal da Transparência", | |
x = "", | |
y = "número de famílias, em milhões") | |
# total R$ desembolsado por ano | |
desembolsos <- total %>% | |
drop_na() %>% | |
#filter(data > "2009-01-01") %>% | |
group_by(ano) %>% | |
summarise(soma = sum(ajustado)) %>% | |
mutate(soma = soma/1000000000) %>% | |
#summarise(soma = mean(soma)) %>% | |
ggplot(aes(ano, soma, label = round(soma, 1))) + geom_line() + geom_label() + | |
#geom_smooth(method = "loess", se = FALSE) + | |
#scale_x_date(breaks = scales::pretty_breaks(n = 10)) + | |
scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE)) + | |
labs( title = "Desembolsos do Bolsa Família", | |
subtitle = "Total anual desembolsado no programa Bolsa Família - valores deflacinados pelo IPCA", | |
caption = "Fonte: Portal da Transparência", | |
x = "", | |
y = "em bilhões de R$") |
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(jsonlite) | |
library(lubridate) | |
# http://www.portaltransparencia.gov.br/beneficios/consulta | |
url3 <- "http://www.transparencia.gov.br/api-de-dados/emendas?ano=2019&pagina=" | |
paginas <- c(1:396) | |
store <- list() | |
for(p in seq_along(paginas)){ | |
# monta a URL | |
url0 <- paste0(url3, p) | |
# puxa os dados | |
dfa <- jsonlite::fromJSON(url0) | |
# preenche a lista | |
store[[p]] = dfa | |
} | |
total <- bind_rows(store) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment