From 9b5952d54e8aca2c2b1c5b136c1239885c4f230e Mon Sep 17 00:00:00 2001 From: Diego H Date: Tue, 30 Jul 2024 09:39:26 +0000 Subject: [PATCH] Update population indicator --- NEWS.md | 6 +- R/indicators.R | 16 ++-- tests/testthat/test-indicators.R | 3 + .../articles/Main_Macroeconomic_Series.Rmd | 86 +++++++++++++++++-- vignettes/articles/Series_Index.Rmd | 9 +- 5 files changed, 101 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 46f76c55..6f3dcd89 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,10 +1,10 @@ # tidyBdE (development version) -- Update `bde_ind_unemployment_rate()` since the underlying identifier - changed. -- `bde_ind_population()` under review, not providing results as of July 2024. +- Update `bde_ind_unemployment_rate()` and `bde_ind_population()` since the + underlying identifier changed. - DOI changed to **CRAN** url: . +- Native encoding when reading the `.csv` files changed to `"latin1"`. # tidyBdE 0.3.6 diff --git a/R/indicators.R b/R/indicators.R index 6a5dc732..f4bd4689 100644 --- a/R/indicators.R +++ b/R/indicators.R @@ -134,15 +134,13 @@ bde_ind_gdp_quarterly <- function(series_label = "GDP_quarterly_value", ...) { #' #' @export bde_ind_population <- function(series_label = "Population_Spain", ...) { - # seq_num <- 3078287 - # - # econom_ind <- bde_series_load(seq_num, series_label = series_label, ...) - # econom_ind <- econom_ind[!is.na(econom_ind[[2]]), ] - # - # return(econom_ind) - - message("Under review") - return(NULL) + # was 3078287 + seq_num <- 4637737 + + econom_ind <- bde_series_load(seq_num, series_label = series_label, ...) + econom_ind <- econom_ind[!is.na(econom_ind[[2]]), ] + + return(econom_ind) } #' @export diff --git a/tests/testthat/test-indicators.R b/tests/testthat/test-indicators.R index fccecb96..f3016cb9 100644 --- a/tests/testthat/test-indicators.R +++ b/tests/testthat/test-indicators.R @@ -27,6 +27,9 @@ test_that("Indicators", { n7 <- expect_silent(bde_ind_gdp_quarterly()) expect_true(nrow(n7) > 10) + n8 <- expect_silent(bde_ind_population()) + expect_true(nrow(n8) > 10) + n9 <- expect_silent(bde_ind_ibex_daily()) expect_true(nrow(n9) > 10) }) diff --git a/vignettes/articles/Main_Macroeconomic_Series.Rmd b/vignettes/articles/Main_Macroeconomic_Series.Rmd index 7a51b1e7..5e6c21bb 100644 --- a/vignettes/articles/Main_Macroeconomic_Series.Rmd +++ b/vignettes/articles/Main_Macroeconomic_Series.Rmd @@ -42,7 +42,7 @@ br <- seq(nd, Sys.Date(), "6 months") ### Aggregated (last 4 quarters) -```{r echo=FALSE} +```{r gdp_agg, echo=FALSE, fig.alt="GDP of Spain - Aggregated last 4 quarters"} dataset <- bde_ind_gdp_quarterly(series_label = "data") %>% drop_na() @@ -74,7 +74,7 @@ ggplot(dataset, aes(x = Date, y = data)) + ### Year-on-year variation -```{r echo=FALSE} +```{r gdpyoy, echo=FALSE, fig.alt="GDP of Spain - Year-on-year variation"} dataset <- bde_ind_gdp_var(series_label = "data") %>% filter(Date >= nd) %>% drop_na() @@ -101,9 +101,52 @@ ggplot(dataset, aes(x = Date, y = data)) + theme_tidybde() ``` +### GDP per capita + +```{r gdppercap, echo=FALSE, fig.alt="GDP per Capita of Spain"} +pop <- bde_ind_population(series_label = "pop") +pib <- bde_ind_gdp_quarterly(series_label = "data") + +pib$LastY <- pib$data + lag(pib$data, 1) + lag(pib$data, 2) + lag(pib$data, 3) + +pib <- inner_join(pib, pop, by = "Date") + +pib <- pib %>% mutate(data = 1000 * LastY / pop) + +dataset <- pib %>% + select(Date, data) %>% + filter(Date >= nd) %>% + drop_na() + + +l <- dataset[nrow(dataset), ] + +ggplot(dataset, aes(x = Date, y = data)) + + geom_line(color = col) + + geom_text( + data = l, + size = 3, + aes(label = paste( + x = prettyNum(data, big.mark = " "), + "€\n", format(Date, "%b-%Y") + )) + ) + + labs( + title = "GDP per capita of Spain", + subtitle = "€", + caption = "Source: BdE" + ) + + scale_y_continuous(labels = scales::label_number()) + + scale_x_date( + date_labels = "%b-%Y", + breaks = br + ) + + theme_tidybde() +``` + ## Unemployment Rate -```{r echo=FALSE} +```{r unempl, echo=FALSE, fig.alt="Unemployment rate"} dataset <- bde_ind_unemployment_rate(series_label = "data") %>% filter(Date >= nd) %>% drop_na() @@ -132,7 +175,7 @@ ggplot(dataset, aes(x = Date, y = data)) + ## Consumer Price Index -```{r echo=FALSE} +```{r cprix, echo=FALSE, fig.alt="Consumer Price Index"} dataset <- bde_ind_cpi_var(series_label = "data") %>% filter(Date >= nd) %>% drop_na() @@ -161,7 +204,7 @@ ggplot(dataset, aes(x = Date, y = data)) + ## Monthly Euribor -```{r echo=FALSE} +```{r eur, echo=FALSE, fig.alt="Monthly Euribor"} dataset <- bde_ind_euribor_12m_monthly(series_label = "data") %>% filter(Date >= nd) %>% drop_na() @@ -187,3 +230,36 @@ ggplot(dataset, aes(x = Date, y = data)) + ) + theme_tidybde() ``` + +## Population + +```{r pop, echo=FALSE, fig.alt="Population in thousands"} +dataset <- bde_ind_population(series_label = "data") %>% + filter(Date >= nd) %>% + drop_na() + + +l <- dataset[nrow(dataset), ] + +ggplot(dataset, aes(x = Date, y = data)) + + geom_line(color = col) + + geom_text( + data = l, + size = 3, + aes(label = paste( + x = prettyNum(data, big.mark = " "), + "\n", format(Date, "%b-%Y") + )) + ) + + labs( + title = "Population of Spain", + subtitle = "thousands", + caption = "Source: BdE" + ) + + scale_x_date( + date_labels = "%b-%Y", + breaks = br + ) + + scale_y_continuous(labels = scales::label_number()) + + theme_tidybde() +``` diff --git a/vignettes/articles/Series_Index.Rmd b/vignettes/articles/Series_Index.Rmd index b6c94281..33c9ae43 100644 --- a/vignettes/articles/Series_Index.Rmd +++ b/vignettes/articles/Series_Index.Rmd @@ -49,8 +49,12 @@ series_clean <- series %>% Tipo_de_variable, Fecha_de_la_primera_observacion, Fecha_de_la_ultima_observacion, - Frecuencia_de_la_serie - ) + Frecuencia_de_la_serie, Fuente + ) %>% + as_tibble() + + +names(series_clean) <- gsub("_", " ", names(series_clean)) library(reactable) @@ -59,6 +63,7 @@ library(reactable) reactable(series_clean, filterable = TRUE, searchable = TRUE, + resizable = TRUE, showPageSizeOptions = TRUE, striped = TRUE, defaultColDef = colDef(