Created
June 3, 2020 19:16
-
-
Save fernandobarbalho/5ef55fafa19d1e14afd091a93440f2ab to your computer and use it in GitHub Desktop.
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
#Criar gráfico de sankey | |
df_desp <- rsiconfi::get_dca(2018,"I-D","1") | |
df_desp_trabalho<- | |
df_desp %>% | |
#filter(stringr::str_starts(cod_conta,"R")) %>% | |
filter(coluna == "Despesas Liquidadas") %>% | |
mutate(nivel_0 = cod_conta =="TotalDespesas" ) %>% | |
mutate(nivel_1= (stringr::str_ends(cod_conta,"0.00.00.00.00"))) %>% | |
mutate(nivel_2= ((stringr::str_sub(cod_conta,5,5)!= "0") & (stringr::str_ends(cod_conta,"00.00.00.00")))) %>% | |
mutate(nivel_3= ((stringr::str_sub(cod_conta,7,8)!= "00") & (stringr::str_ends(cod_conta,"00.00.00")))) %>% | |
filter(nivel_0 | nivel_1 | nivel_2 | nivel_3 ) %>% # | |
mutate(pai = case_when( | |
nivel_1 ~ "TotalDespesas", | |
nivel_2 ~ paste0(stringr::str_sub(cod_conta,1,3), ".0.00.00.00.00"), | |
nivel_3 ~ paste0(stringr::str_sub(cod_conta,1,5), ".00.00.00.00") | |
)) %>% | |
filter(str_sub(cod_conta,1,2)!="DI")%>% | |
mutate (source = row_number() -1) | |
v_pai <- | |
df_desp_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_desp_trabalho$cod_conta==a_pai) | |
}) | |
pos_pai<- pos_pai -1 | |
df_desp_trabalho$destination <- pos_pai | |
nodes<- df_desp_trabalho %>% | |
mutate(conta= case_when( | |
cod_conta == "TotalDespesas" ~ "Total de despesas liquidadas", | |
input$ano_desp == "2018" ~ stringr::str_sub(conta, 16,str_length(conta)), | |
input$ano_desp < "2018" ~ stringr::str_sub(conta, 18,str_length(conta)) | |
)) %>% | |
select(conta) | |
links<- | |
df_desp_trabalho %>% | |
filter(!is.na(pai)) %>% | |
select(source, | |
destination, | |
valor) | |
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