Created
May 27, 2020 16:01
-
-
Save fernandobarbalho/782b69d892ff5b3ed7be30a76792879f to your computer and use it in GitHub Desktop.
Script to generate a sankey graph of the brazilian government revenue
This file contains 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
devtools::install_github("tchiluanda/rsiconfi") | |
library(rsiconfi) | |
library(networkD3) | |
library(tidyverse) | |
#Get data in SICONFI for budget revenue (Ï-C) related to the federal government entity (1) in 2018 | |
df_rec <- rsiconfi::get_dca(2018,"I-C","1") | |
# find hierachical strtucture of budget revenue | |
df_rec_trabalho<- | |
df_rec %>% | |
#filter(stringr::str_starts(cod_conta,"R")) %>% | |
filter(coluna == "Receitas Brutas Realizadas") %>% | |
mutate(nivel_0 = cod_conta =="TotalReceitas" ) %>% | |
mutate(nivel_1= (stringr::str_ends(cod_conta,".0.0.0.00.0.0"))) %>% | |
mutate(nivel_2= ((stringr::str_sub(cod_conta,5,5)!= "0") & (stringr::str_ends(cod_conta,"0.0.00.0.0")))) %>% | |
mutate(nivel_3= ((stringr::str_sub(cod_conta,7,7)!= "0") & (stringr::str_ends(cod_conta,"0.00.0.0")))) %>% | |
filter(nivel_0 | nivel_1 | nivel_2 | nivel_3) %>% | |
mutate(pai = case_when( | |
nivel_1 ~ "TotalReceitas", | |
nivel_2 ~ paste0(stringr::str_sub(cod_conta,1,3), ".0.0.0.00.0.0"), | |
nivel_3 ~ paste0(stringr::str_sub(cod_conta,1,5), ".0.0.00.0.0") | |
)) %>% | |
mutate (source = row_number() -1) | |
v_pai <- | |
df_rec_trabalho %>% | |
mutate(pai = ifelse(is.na(pai),cod_conta,pai)) | |
pos_pai<- | |
map_int(v_pai$pai, function(a_pai){ | |
print(a_pai) | |
which(df_rec_trabalho$cod_conta==a_pai) | |
}) | |
pos_pai<- pos_pai -1 | |
df_rec_trabalho$destination <- pos_pai | |
#identify the nodes for sankey graph | |
nodes<- df_rec_trabalho %>% | |
mutate(conta= ifelse(cod_conta == "TotalReceitas","Total de Receitas Brutas", stringr::str_sub(conta, 18,str_length(conta)))) %>% | |
select(conta) | |
#identify the links for sankey graph | |
links<- | |
df_rec_trabalho %>% | |
filter(!is.na(pai)) %>% | |
select(source, | |
destination, | |
valor) | |
#generate sankey graph | |
#use viewer zoom if you are using RStudio | |
sankeyNetwork(Links = links, Nodes = nodes, Source = "source", | |
Target = "destination", Value = "valor", NodeID = "conta", | |
units = "", fontSize = 12, nodeWidth = 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment