Created
September 20, 2022 03:15
-
-
Save gomesfellipe/7cde3d99b1cc6db18c278a03fed4f915 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
library(tidyverse) | |
library(lubridate) | |
library(sidrar) | |
# Obter IPCA -------------------------------------------------------------- | |
ipca_indice = get_sidra(api='/t/1737/n1/all/v/2266/p/all/d/v2266%2013') %>% | |
as_tibble() %>% | |
select(-`Nível Territorial`, -`Nível Territorial (Código)`, | |
-`Unidade de Medida`, -`Unidade de Medida (Código)`, | |
-Brasil, -`Brasil (Código)`, | |
-Variável, -`Variável (Código)` | |
) %>% | |
mutate(date = ymd(paste0(`Mês (Código)`, '01')))%>% | |
filter(date >= as.Date('2007-06-01'))%>% | |
mutate(`Inflação variação 30 dias` = round((Valor/lag(Valor, 1)-1)*100, 2), | |
`Inflação variação 1 ano` = round((Valor/lag(Valor, 12)-1)*100, 2)) %>% | |
rename(IPCA = Valor) | |
# Grafico IPCA, INFLACAO 30 dias e 1 ano ---------------------------------- | |
ipca_indice %>% | |
select(date, IPCA, `Inflação variação 30 dias`, `Inflação variação 1 ano`) %>% | |
gather(key, value, -date) %>% | |
mutate(key = factor(key, c("IPCA", | |
"Inflação variação 30 dias", | |
"Inflação variação 1 ano"))) %>% | |
ggplot(aes(x = date, y = value, col = key))+ | |
geom_line()+ | |
facet_wrap(~key, scale="free_y", ncol=1)+ | |
labs(title = "Raio-X da Inflação no Brasil", | |
subtitle = "Diretamente para Os tintas", | |
x = "", y = "", | |
caption = "Fonte: Dados do Banco Central do Brasil | |
Ano de referência: 2007")+ | |
scale_x_date(breaks = "1 year",date_labels = "%Y")+ | |
guides(col=guide_legend(title="")) | |
# Zoom na inflacao -------------------------------------------------------- | |
ipca_indice %>% | |
ggplot(aes(x = date, y = `Inflação variação 30 dias`))+ | |
geom_line()+ | |
labs(x = "Data", y = "% ao mês", subtitle = "Ano de referência: 1980")+ | |
scale_x_date(breaks = "1 year",date_labels = "%Y")+ | |
geom_hline(yintercept=0, linetype="dashed", color = "red") | |
ipca_indice %>% | |
ggplot(aes(x = date, y = `Inflação variação 30 dias`))+ | |
geom_line()+ | |
labs(x = "Data", y = "% ao mês", subtitle = "Ano de referência: 1980")+ | |
scale_x_date(breaks = "1 year",date_labels = "%Y")+ | |
geom_hline(yintercept=0, linetype="dashed", color = "red") | |
# Componentes do IPCA ----------------------------------------------------- | |
variacao = | |
'/t/7060/n1/all/v/63/p/all/c315/7170,7445,7486,7558,7625,7660,7712,7766,7786/d/v63%202' %>% | |
get_sidra(api=.) %>% | |
mutate(date = parse_date(`Mês (Código)`, format='%Y%m')) %>% | |
select(date, "Geral, grupo, subgrupo, item e subitem", Valor) %>% | |
pivot_wider(names_from = "Geral, grupo, subgrupo, item e subitem", | |
values_from = Valor) | |
peso = | |
'/t/7060/n1/all/v/66/p/all/c315/7170,7445,7486,7558,7625,7660,7712,7766,7786/d/v66%204' %>% | |
get_sidra(api=.) %>% | |
mutate(date = parse_date(`Mês (Código)`, format='%Y%m')) %>% | |
select(date, "Geral, grupo, subgrupo, item e subitem", Valor) %>% | |
pivot_wider(names_from = "Geral, grupo, subgrupo, item e subitem", | |
values_from = Valor) | |
contribuicao = (variacao[,-1]*peso[,-1]/100) %>% | |
mutate(date = variacao$date) %>% | |
select(date, everything()) | |
# criar grafico | |
contribuicao %>% | |
left_join(ipca_indice %>% | |
select(date, `Inflação variação 30 dias`)) %>% | |
filter(date >= '2022-01-01') %>% | |
gather(key, value, -date, -`Inflação variação 30 dias`) %>% | |
ggplot(aes(x = date, y = value, fill = key)) + | |
geom_bar(position="stack", stat="identity")+ | |
geom_line(aes(y=`Inflação variação 30 dias`))+ | |
theme(legend.position = "right")+ | |
labs(title = "Raio-X do IPCA nesse ano", | |
subtitle = "Diretamente para Os tintas", | |
x = "Data", y = "Variação mensal", | |
caption = "Fonte: Dados do Banco Central do Brasil")+ | |
scale_x_date(date_breaks = "1 month", date_labels = "%b") -> p | |
plotly::ggplotly(p) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment