-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualiz_curvas.R
187 lines (152 loc) · 6.35 KB
/
visualiz_curvas.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
### LIBRARY's ----
library(tidyverse)
library(ggthemes)
library(gganimate)
library(av)
library(gifski)
library(readxl)
library(ggrepel)
library(directlabels)
library(ggpubr)
library(hrbrthemes)
library(extrafont)
loadfonts(quiet = TRUE)
### INPUTS ----
dir_base <- "~/dev_Py/juros_futuros_susep"
arquivo <- "historico_curvas.xlsx"
n_row <- 50
setwd(dir_base)
### IMPORTAÇÃO ----
hist_pre <- arquivo %>%
# Extrai os dados históricos de cada aba do arquivo .xlsx
read_excel(sheet = "curvas_pre", n_max = n_row) %>%
# Cria a variável curva com o cupom correspondente
mutate(curva = "PREFIXADO") %>%
# retira dados com dias úteis menores que 126du
tail(44) %>%
# transforma os dados em formato 'long'
gather(key = "anomes", value = "taxa", -c(nseq, dias, curva))
hist_ipca <- arquivo %>%
read_excel(sheet = "curvas_ipca", n_max = n_row) %>%
mutate(curva = "IPCA") %>%
tail(44) %>%
gather(key = "anomes", value = "taxa", -c(nseq, dias, curva))
hist_igpm <- arquivo %>%
read_excel(sheet = "curvas_igpm", n_max = n_row) %>%
mutate(curva = "IGPM") %>%
tail(44) %>%
gather(key = "anomes", value = "taxa", -c(nseq, dias, curva))
hist_tr <- arquivo %>%
read_excel(sheet = "curvas_tr", n_max = n_row) %>%
mutate(curva = "TR") %>%
tail(44) %>%
gather(key = "anomes", value = "taxa", -c(nseq, dias, curva))
### LIMPEZA ----
# Une as 4 tibbles em uma única tibble
hist_curvas <- rbind(hist_pre, hist_ipca, hist_igpm, hist_tr)
# Cria a variável data a partir da variável anomes
hist_curvas$data <- as.Date(paste0(as.character(hist_curvas$anomes), '01'), format='%Y%m%d')
# Transforma a variável curva em fator
hist_curvas$curva <- factor(hist_curvas$curva)
# cria nível nos fatores para ajustar a ordem da visualizacao
neworder <- c("PREFIXADO", "TR", "IPCA", "IGPM")
hist_curvas <- arrange(transform(hist_curvas,curva=factor(curva,levels=neworder)),curva)
hist_curvas <- hist_curvas %>% as_tibble()
### facet_plot .GIF ----
facet_plot <- hist_curvas %>%
mutate(taxa1 = taxa*100) %>%
select(-taxa) %>%
rename(taxa = taxa1) %>%
ggplot(aes(x = dias, y = taxa)) +
ylab("Juros % a.a") +
xlab("Dias") +
geom_line(size = 1.5,
show.legend = FALSE) +
facet_wrap(~curva,
scales = "free") +
theme_bw() +
theme(strip.text.x = element_text(size = 12, color = "black", face = "bold")) +
labs(caption = "Elaborado por: @AugustoCL\n Fonte: Coeficientes SUSEP")
# Adiciona as funções de animação da visualizacao
facet_plot <- facet_plot + transition_time(data)+
ease_aes(y = 'cubic-in-out') +
labs(title = "Interpolação e Extrapolação das Curvas de Juros (201512 - 2020202)",
subtitle = 'Modelo: Nelson Siegel Svensson\nPeríodo: {format(frame_time,"%Y%m")}')
# Gera a animação em .gif no diretório base
gganimate::animate(facet_plot,
start_pause = 3,
end_pause = 3,
renderer = gifski_renderer("curvas_201512_202002_facet.gif"))
### color_plot .GIF ----
p <- hist_curvas %>%
mutate(taxa1 = taxa*100) %>%
select(-taxa) %>%
rename(taxa = taxa1) %>%
# filter(anomes == "201912") %>%
mutate(label = if_else(dias == max(dias), as.character(curva), NA_character_)) %>%
ggplot(aes(x = dias, y = taxa, color = curva)) +
geom_line(size = 2, linejoin = "bevel") +
geom_label_repel(aes(label = label),
nudge_x = 1,
na.rm = TRUE,
inherit.aes = TRUE) +
ylab("Juros % a.a") +
xlab(NULL) +
labs(title = 'Interpolação e Extrapolação das Curvas de Juros - {format(frame_time,"%Y%m")}',
subtitle = 'Modelo: Nelson Siegel Svensson',
color = NULL,
caption = "Elaboração: @AugustoCL") +
# scale_y_continuous(breaks = scales::pretty_breaks(7)) +
scale_x_continuous(breaks = scales::pretty_breaks(6),
labels = scales::dollar_format(prefix = NULL, suffix = " du")) +
scale_color_brewer(palette = "Set1") +
theme_minimal(base_family = "Trebuchet MS") +
theme(strip.text.x = element_text(size = 12, color = "black", face = "bold"),
legend.position = "none",
axis.title.x = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5),
text = element_text(family = "Trebuchet MS"))
p <- p + transition_time(data)+
ease_aes(y = 'linear') # 'cubic-in-out'
gganimate::animate(p1, start_pause = 3,
end_pause = 3,
renderer = gifski_renderer("curvas_201512_202002.gif"))
### dark_plot.GIF ----
p_dark <- hist_curvas %>%
mutate(taxa1 = taxa*100) %>%
select(-taxa) %>%
rename(taxa = taxa1) %>%
# filter(anomes == "201912") %>%
mutate(label = if_else(dias == max(dias), as.character(curva), NA_character_)) %>%
ggplot(aes(x = dias, y = taxa, color = curva)) +
geom_line(size = 2, linejoin = "bevel") +
geom_label_repel(aes(label = label),
nudge_x = 1,
na.rm = TRUE,
inherit.aes = TRUE) +
ylab("Juros % a.a") +
xlab(NULL) +
labs(title = 'Interpolação das Curvas de Juros - {format(frame_time,"%Y%m")}',
subtitle = 'Modelo: Nelson Siegel Svensson',
color = NULL,
caption = "Elaboração: @AugustoCL") +
# scale_y_continuous(breaks = scales::pretty_breaks(7)) +
scale_x_continuous(breaks = scales::pretty_breaks(6),
labels = scales::dollar_format(prefix = NULL, suffix = " du")) +
scale_color_brewer(palette = "Set1") +
hrbrthemes::theme_modern_rc(base_family = "Trebuchet MS") +
theme(strip.text.x = element_text(size = 10,
face = "bold"),
legend.position = "none",
axis.text = element_text(size = 2),
axis.title.x = element_text(hjust = 0.5),
axis.title.y = element_text(),
title = element_text(family = "Trebuchet MS"),
text = element_text(family = "Trebuchet MS"))
p_dark <- p_dark +
transition_time(data) +
ease_aes(y = 'linear') # 'cubic-in-out'
gganimate::animate(p_dark, start_pause = 3,
end_pause = 3,
# height = 600, width = 800,
renderer = gifski_renderer("curvas_201512_202002_dark.gif"))