tidyBdE is an API package that helps to retrieve data from Banco de España. The data is provided as tibble and the package tries to guess the format of every time-series (dates, characters and numbers).
Banco de España (BdE) provides several time-series, either produced by the institution itself or compiled for another sources, as Eurostat or INE.
The basic entry point for searching time-series are the catalogs (indexes) of information. You can search any series by name:
library(tidyBdE)
# Search GBP on "TC" (exchange rate) catalog
<- bde_catalog_search("GBP", catalog="TC")
XR_GBP
c(2,5)]
XR_GBP[#> # A tibble: 1 x 2
#> Numero_secuencial Descripcion_de_la_serie
#> <dbl> <chr>
#> 1 573214 Tipo de cambio. Libras esterlinas por euro (GBP/EUR).Datos ~
Note that BdE files are only provided in Spanish, for the time being, the organism is working on the English version. By now, search terms should be provided in Spanish in order to get search results.
After we have found our series, we can load the series for the GBP/EUR exchange rate using the sequential number reference (Numero_Secuencial
) as:
# Load tidyverse for better handling
library(tidyverse)
<- bde_series_load(573214, series_label = "EUR_GBP_XR") %>%
time_series filter(Date >= "2010-01-01" & Date <= "2020-12-31") %>%
drop_na()
The package also provides a custom ggplot2
theme based on the publications of BdE:
ggplot(time_series, aes(x = Date, y = EUR_GBP_XR)) +
geom_line(colour = bde_vivid_pal()(1)) +
geom_smooth(method = "gam", colour = bde_vivid_pal()(2)[2]) +
labs(title = "EUR/GBP Exchange Rate (2010-2020)",
subtitle = "%",
caption = "Source: BdE") +
geom_vline(xintercept = as.Date("2016-06-23"),
linetype = "dotted") +
geom_label(aes(
x = as.Date("2016-06-23"),
y = .95,
label = "Brexit"
+
)) coord_cartesian(ylim = c(0.7, 1)) +
theme_bde()
The package provides also several “shortcut” functions for a selection of the most relevant macroeconomic series, so there is no need to look for them in advance:
<- bde_ind_gdp_var("values")
gdp $label <- "GDP YoY"
gdp
<- bde_ind_unemployment_rate("values")
UnempRate $label <- "Unemployment Rate"
UnempRate
<- bind_rows(gdp, UnempRate) %>%
plotseries drop_na() %>%
filter(Date >= "2010-01-01" & Date <= "2019-12-31")
ggplot(plotseries, aes(x = Date, y = values)) +
geom_line(aes(color = label)) +
labs(title = "Spanish Economic Indicators (2010-2019)",
subtitle = "%",
caption = "Source: BdE") +
theme_bde() +
scale_color_bde_d() # Custom palette on the package
Two custom palettes, based on the used by BdE on some publications are available:
# Load GDP Series
<- bde_series_load(
GDP series_code = c(3777251,
3777265,
3777259,
3777269,
3777060),
series_label = c("Agriculture",
"Industry",
"Construction",
"Services",
"Total")
)
# Manipulate data - tidyverse style
<- GDP %>%
GDP_all # Filter dates
filter(Date <= "2020-12-31") %>%
# Create 'Other' column and convert Date to year
mutate(Other = Total - rowSums(across(Agriculture:Services)),
Date = as.numeric(format(Date, format = "%Y"))) %>%
# Sum by year
group_by(Date) %>%
summarise_at(vars(-group_cols()), sum) %>%
# Create percentage
relocate(Total, .after = Other) %>%
mutate(across(Agriculture:Other, ~ .x / Total)) %>%
# Move cols to rows for plotting
select(-Total) %>%
pivot_longer(Agriculture:Other,
names_to = "serie",
values_to = "value")
ggplot(data = GDP_all, aes(x = Date,
y = value,
fill = serie)) +
geom_bar(position = "stack",
stat = "identity",
alpha = 0.8) +
scale_fill_bde_d(palette = "bde_rose_pal") + # Custom palette on the package
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_bde() +
labs(title = "Spain: Gross domestic product by industry",
subtitle = "%",
caption = "Source: BdE")
You can use tidyBdE to create your own local repository at a given local directory passing the following option:
options(bde_cache_dir = "./path/to/location")
When this option is set, tidyBdE would look for the cached file on the bde_cache_dir
directory and it will load it, speeding up the process.
It is possible to update the data (i.e. after every monthly or quarterly data release) with the following commands:
bde_catalog_update()
# On most of the functions using the option update_cache = TRUE
bde_series_load("SOME ID", update_cache = TRUE)
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=C LC_CTYPE=Spanish_Spain.1252
#> [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
#> [5] LC_TIME=Spanish_Spain.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.6 purrr_0.3.4
#> [5] readr_1.4.0 tidyr_1.1.3 tibble_3.1.2 ggplot2_3.3.3
#> [9] tidyverse_1.3.1 tidyBdE_0.1.1
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.6 lubridate_1.7.10 lattice_0.20-44 ps_1.6.0
#> [5] assertthat_0.2.1 digest_0.6.27 utf8_1.2.1 R6_2.5.0
#> [9] cellranger_1.1.0 backports_1.2.1 reprex_2.0.0 evaluate_0.14
#> [13] highr_0.9 httr_1.4.2 pillar_1.6.1 rlang_0.4.11
#> [17] readxl_1.3.1 rstudioapi_0.13 jquerylib_0.1.4 Matrix_1.3-3
#> [21] rmarkdown_2.8 labeling_0.4.2 splines_4.1.0 munsell_0.5.0
#> [25] broom_0.7.6 compiler_4.1.0 modelr_0.1.8 xfun_0.23
#> [29] pkgconfig_2.0.3 mgcv_1.8-35 htmltools_0.5.1.1 tidyselect_1.1.1
#> [33] fansi_0.5.0 crayon_1.4.1 dbplyr_2.1.1 withr_2.4.2
#> [37] grid_4.1.0 nlme_3.1-152 jsonlite_1.7.2 gtable_0.3.0
#> [41] lifecycle_1.0.0 DBI_1.1.1 magrittr_2.0.1 scales_1.1.1
#> [45] cli_2.5.0 stringi_1.6.2 farver_2.1.0 fs_1.5.0
#> [49] xml2_1.3.2 bslib_0.2.5.1 ellipsis_0.3.2 generics_0.1.0
#> [53] vctrs_0.3.8 tools_4.1.0 glue_1.4.2 hms_1.1.0
#> [57] yaml_2.2.1 colorspace_2.0-1 rvest_1.0.0 knitr_1.33
#> [61] haven_2.4.1 sass_0.4.0
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=C LC_CTYPE=Spanish_Spain.1252
#> [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
#> [5] LC_TIME=Spanish_Spain.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.6 purrr_0.3.4
#> [5] readr_1.4.0 tidyr_1.1.3 tibble_3.1.2 ggplot2_3.3.3
#> [9] tidyverse_1.3.1 tidyBdE_0.1.1
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.6 lubridate_1.7.10 lattice_0.20-44 ps_1.6.0
#> [5] assertthat_0.2.1 digest_0.6.27 utf8_1.2.1 R6_2.5.0
#> [9] cellranger_1.1.0 backports_1.2.1 reprex_2.0.0 evaluate_0.14
#> [13] highr_0.9 httr_1.4.2 pillar_1.6.1 rlang_0.4.11
#> [17] readxl_1.3.1 rstudioapi_0.13 jquerylib_0.1.4 Matrix_1.3-3
#> [21] rmarkdown_2.8 labeling_0.4.2 splines_4.1.0 munsell_0.5.0
#> [25] broom_0.7.6 compiler_4.1.0 modelr_0.1.8 xfun_0.23
#> [29] pkgconfig_2.0.3 mgcv_1.8-35 htmltools_0.5.1.1 tidyselect_1.1.1
#> [33] fansi_0.5.0 crayon_1.4.1 dbplyr_2.1.1 withr_2.4.2
#> [37] grid_4.1.0 nlme_3.1-152 jsonlite_1.7.2 gtable_0.3.0
#> [41] lifecycle_1.0.0 DBI_1.1.1 magrittr_2.0.1 scales_1.1.1
#> [45] cli_2.5.0 stringi_1.6.2 farver_2.1.0 fs_1.5.0
#> [49] xml2_1.3.2 bslib_0.2.5.1 ellipsis_0.3.2 generics_0.1.0
#> [53] vctrs_0.3.8 tools_4.1.0 glue_1.4.2 hms_1.1.0
#> [57] yaml_2.2.1 colorspace_2.0-1 rvest_1.0.0 knitr_1.33
#> [61] haven_2.4.1 sass_0.4.0
#> ***
#> vignette running time:
#> Time difference of 42.1443 secs