library(tidyverse)
library(sidrar)
#remotes::install_github("leripio/tstools", force=T)
library(tstools)
library(ggpubr)
library(ggforce)
library(ggpmisc)
library(knitr)
library(thematic)
thematic_on(bg = "#222222", fg = "white", font = font_spec("Oxanium", scale = 1.5))


serie02 = get_sidra(api= '/t/1419/n1/all/v/63/p/all/c315/12393/d/v63%202')
serie03 = get_sidra(api='/t/7060/n1/all/v/63/p/all/c315/12393/d/v63%202')

dates = seq(as.Date('2012-01-01'), as.Date('2022-05-01'), by='1 month')



data = tibble(date = dates,
              `Variação Mensal` = c(serie02$Valor, 
                                    serie03$Valor),
              `Variação Acumulada em 12 meses` = tstools::acum_p(`Variação Mensal`, 12))

data %>% head(20) %>% kable
date Variação Mensal Variação Acumulada em 12 meses
2012-01-01 -1.13 NA
2012-02-01 -0.55 NA
2012-03-01 0.29 NA
2012-04-01 0.81 NA
2012-05-01 -0.09 NA
2012-06-01 0.33 NA
2012-07-01 -0.57 NA
2012-08-01 0.13 NA
2012-09-01 0.74 NA
2012-10-01 1.05 NA
2012-11-01 2.05 NA
2012-12-01 1.57 4.681207
2013-01-01 0.72 6.639943
2013-02-01 -0.01 7.218983
2013-03-01 1.76 8.790545
2013-04-01 2.91 11.056790
2013-05-01 3.90 15.491947
2013-06-01 4.60 20.407233
2013-07-01 5.06 27.225021
2013-08-01 3.75 31.824588
data_long =  data %>%
  gather(metrica, valor, -date)

item <- gsub("[0-9].|^\\)|\\)$", "", 
             serie03$`Geral, grupo, subgrupo, item e subitem`[1])


mypal <- RColorBrewer::brewer.pal(name = "Dark2", n=8)
a<-data_long %>% filter(metrica == "Variação Acumulada em 12 meses" ) %>% 
  ggplot() +
  geom_line(aes(x=date,y=valor,col=metrica), size=1) + 
  stat_peaks(aes(x = date, y = valor, label=paste(round(valor,2),"% \n",date)), geom = "text", colour = "brown1", vjust = -0.2, check_overlap = TRUE, span = 10, size=6,ignore_threshold = 0.01)+
  stat_valleys(aes(x = date, y = valor, label=paste(round(valor,2),"% \n",date)), geom = "text", colour = "cyan", vjust = 1.1, check_overlap = TRUE, span = 10, size=6)+
  scale_y_continuous(limits = c(-30, 55), breaks = seq(-30, 55, 10)) +
  geom_hline(yintercept = 0, linetype=2) + 
  ylab("") + xlab("") + 
  scale_x_date(
    date_minor_breaks = "1 year",
    breaks = "1 year",
    date_labels = "%Y") + 
  scale_color_manual(values = mypal)+
  ggtitle(paste("Variação do Preço do",item, "dentro do IPCA (%)"))  + 
  labs(caption = "\n",
    subtitle = "Variação acumulada em 12 meses") + 
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 0, hjust = 0.5))



b<-data_long %>% filter(metrica == "Variação Mensal" ) %>% 
  ggplot() +
  geom_line(aes(x=date,y=valor,col=metrica), size=1) + 
  geom_hline(yintercept = 0, linetype=2) + 
  facet_zoom(x = date > as.Date("2020-01-01"))+
  stat_peaks(aes(x = date, y = valor, label=paste(valor,"% \n",date)), geom = "text", colour = "brown1", vjust = -0.2, check_overlap = TRUE, span = NULL, size=6)+
  stat_valleys(aes(x = date, y = valor, label=paste(valor,"% \n",date)), geom = "text", colour = "cyan", vjust = 1.1, check_overlap = TRUE, span = NULL, size=6)+
  scale_y_continuous(limits = c(-30, 30), breaks = seq(-30, 30, 10)) +
  ylab("") + xlab("") +
  scale_color_manual(values = mypal[2])+
  ggtitle("")  + 
  labs(caption = "Fonte: SIDRA IBGE\n Cid Edson Mendonça Póvoas",
       subtitle = "Variação mensal") + 
  theme(legend.position = "none") 
ggarrange(a,b)
## span increased to next odd value:  11 
## span increased to next odd value:  11