library(Quandl) Quandl.api_key(Sys.getenv("Q_key")) library(tidyverse) library(lubridate) library(readxl) library(xts)
codes = tribble( ~name,~code, "gdp_pc_nominal_lcu","NGDPPC", "gdp_pc_real_lcu","NGDPRPC", "gdp_pc_nominal_usd","NGDPDPC", "gdp_total_nominal_lcu", "NGDP", "gdp_total_real_lcu", "NGDP_R", "gdp_total_nominal_usd", "NGDPD" ) df = map2(codes$name,codes$code, function(name,code){ temp_df = Quandl(paste0("ODA/ISR_", code),type = "raw") temp_df = temp_df %>% rename_all(tolower) %>% rename(!!name := 2) }) df = df %>% reduce(inner_join, by = "date") df = df %>% pivot_longer(-date) %>% separate(col = name, into = c("indicator","population","deflator","currency"), sep = "_") rm(codes)
gdp_components_table = tribble(~component, ~code, "consumption","OECD/SNA_TABLE1_ISR_P3_CPC", "government","OECD/SNA_TABLE1_ISR_P3S13_CPC", "capital","OECD/SNA_TABLE2_ISR_P5S1_CPC", "external_balance","OECD/SNA_TABLE1_ISR_B11_CPC") gdp_components_df = map2(gdp_components$code, gdp_components$component, function(temp_code, temp_name){ temp_df = Quandl(temp_code, type = "raw") temp_df = temp_df %>% mutate(Date = as_date(Date)) %>% set_names(c("date", temp_name)) return(temp_df) }) %>% reduce(inner_join, "date") %>% pivot_longer(-date) rm(gdp_components_table) gdp_components_df = gdp_components_df %>% mutate(name = factor( name, levels = c("consumption", "government", "capital", "external_balance"), labels = c("Private consumption", "Government", "Investment", "External Balance (Net Export)") ))
df %>% filter(year(date) >= 1995) %>% filter(year(date) < year(Sys.Date())) %>% filter(population == "total") %>% filter(currency == "lcu") %>% filter(deflator == "nominal") %>% ggplot(aes(x = as_factor(year(date)), y = value)) + geom_col() + scale_y_continuous(labels = scales::comma_format()) + xlab(NULL) + ylab(NULL) + ggtitle("Israel GDP (billions of ILS)") + theme(legend.title = element_blank())
price_converter = df %>% filter(year(date) == 1995) %>% filter(population == "total") %>% filter(currency == "lcu") %>% select(deflator, value) price_converter = price_converter$value[price_converter$deflator == "real"] / price_converter$value[price_converter$deflator == "nominal"] df %>% filter(year(date) >= 1995) %>% filter(year(date) < year(Sys.Date())) %>% filter(population == "total") %>% filter(currency == "lcu") %>% select(-c(population, currency, indicator)) %>% mutate(value = if_else(deflator == "real", value / price_converter, value)) %>% pivot_wider(names_from = deflator) %>% mutate(nominal = nominal - real) %>% pivot_longer(-date, names_to = "deflator") %>% ggplot(aes(x = as_factor(year(date)), y = value, fill = deflator)) + geom_col() + scale_fill_manual(values = c("lightblue", "gray30")) + scale_y_continuous(labels = scales::comma_format()) + xlab(NULL) + ylab(NULL) + ggtitle("Israel GDP (billions of ILS)") + theme(legend.title = element_blank(), legend.position = "right") rm(price_converter)
df %>% filter(year(date) >= 1995) %>% filter(year(date) < year(Sys.Date())) %>% filter(population == "pc") %>% select(-c(population, indicator)) %>% group_by(deflator, currency) %>% arrange(date) %>% mutate(diff = value / lag(value) - 1) %>% mutate( indicator = case_when( deflator == "nominal" & currency == "usd" ~ "USD", deflator == "nominal" & currency == "lcu" ~ "Nominal", deflator == "real" & currency == "lcu" ~ "Real" ) ) %>% ggplot(aes( x = as_factor(year(date)), y = diff, fill = (diff > 0) )) + geom_col() + scale_y_continuous(labels = scales::percent_format()) + facet_wrap( ~ indicator, scales = "free", ncol = 1) + xlab(NULL) + ylab(NULL) + ggtitle("Changes in GDP per capita") + theme(legend.position = "none", axis.text.x = element_text(angle = 90, vjust = 0.5))
gdp_components_df %>% filter(date >= as_date("2000-01-01")) %>% group_by(date) %>% mutate(share = value / sum(value),.groups = "drop") %>% ggplot(aes(x = date, y = share, fill = name)) + geom_col() + scale_y_continuous(labels = scales::percent_format()) + xlab(NULL) + ylab(NULL) + ggtitle("GDP components") + scale_fill_viridis_d(direction = -1) + theme(legend.title = element_blank())
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.