library(Quandl)

Quandl.api_key("Vd7t2TFEjCvipyX7zPmh")

library(tidyverse)

library(lubridate)
ca_df = Quandl("ODA/ISR_BCA",type = "raw")

ca_df = ca_df %>% 
  rename_all(tolower)
indicators_table = tribble(~indicator,~code,
                           "imports-services","OECD/SNA_TABLE1_ISR_P72_CXC",
                           "imports-goods","OECD/SNA_TABLE1_ISR_P71_CXC",
                           "exports-goods","OECD/SNA_TABLE1_ISR_P61_CXC",
                           "exports-services","OECD/SNA_TABLE1_ISR_P62_CXC")

import_export = map2(indicators_table$code, indicators_table$indicator,
                     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) %>% 
  separate(name,sep = "-",into = c("direction","component"))

rm(indicators_table)
trade_df = Quandl.datatable('WB/DATA', series_id='NE.RSB.GNFS.ZS,NE.RSB.GNFS.CD',
                 country_code='ISR')
ca_df %>% 
  filter(year(date) <= year(Sys.Date())) %>% 
  ggplot(aes(x = as.character(year(date)), y = value, fill = (value >=0))) + 
  geom_col() + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  xlab(NULL) + ylab(NULL) + ggtitle("Israel current account (USD billions)") + 
  theme(axis.text.x = element_text(angle = 90), legend.position = "none")
import_export %>% 
  filter(date >= as_date("1999-01-01")) %>% 
  group_by(date, direction) %>% 
  summarise(value = sum(value), .groups = "drop") %>% 
  ggplot(aes(x = date, y = value, fill = direction)) + 
  geom_col(position = "dodge") + 
  xlab(NULL) + ylab("USD billions") + ggtitle("Israel Imports and Exports") + 
  theme(legend.title = element_blank()) + 
  scale_y_continuous(labels = scales::comma_format(scale = 0.001))
import_export %>% 
  filter(date >= as_date("1999-01-01")) %>% 
  filter(direction == "exports") %>% 
  ggplot(aes(x = date, y = value, fill = component  )) + 
  geom_col() + 
  xlab(NULL) + ylab("USD billions") + ggtitle("Israel Exports") + 
  theme(legend.title = element_blank()) + 
  scale_y_continuous(labels = scales::comma_format(scale = 0.001)) + 
  scale_fill_viridis_d(direction = -1)
trade_df %>% 
  ggplot(aes(x = year, y = value)) + 
  geom_col()


MichaelGurkov/LearningMaterials documentation built on July 9, 2022, 5:17 p.m.