Direction of Trade Statistics - DOT

Data - IMF

Info

DOWNLOAD_TIME

Code
tibble(DOWNLOAD_TIME = as.Date(file.info("~/Library/Mobile\ Documents/com~apple~CloudDocs/website/data/imf/DOT_FR.RData")$mtime)) %>%
  print_table_conditional()
DOWNLOAD_TIME
2024-06-08

Last

Code
DOT_FR %>%
  group_by(TIME_PERIOD) %>%
  summarise(Nobs = n()) %>%
  arrange(desc(TIME_PERIOD)) %>%
  head(1) %>%
  print_table_conditional()
TIME_PERIOD Nobs
2024-02 663

Sub-Datasets

Javascript

Code
DOT_datasets %>%
    mutate(html = paste0('<a  target=_blank href=', id, '.html > html </a>')) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Flat

Code
DOT_datasets %>%
  mutate(html = paste0("[html](", id, '.html)')) %>%
  {if (is_html_output()) print_table(.) else .}
id title html
DOT_FR DOT - France [html]
DOT_DE DOT - Germany [html]

FREQ

Code
DOT_DE %>%
  bind_rows(DOT_FR) %>%
  left_join(FREQ, by = "FREQ") %>%
  group_by(FREQ, Freq) %>%
  summarise(Nobs = n()) %>%
  arrange(-Nobs) %>%
  {if (is_html_output()) print_table(.) else .}
FREQ Freq Nobs
M Monthly 849164
Q Quarterly 292778
A Annual 82570

INDICATOR

Code
DOT_DE %>%
  bind_rows(DOT_FR) %>%
  left_join(INDICATOR, by = "INDICATOR") %>%
  group_by(INDICATOR, Indicator) %>%
  summarise(Nobs = n()) %>%
  arrange(-Nobs) %>%
  {if (is_html_output()) print_table(.) else .}
INDICATOR Indicator Nobs
TBG_USD Goods, Value of Trade Balance, US Dollars 419316
TXG_FOB_USD Goods, Value of Exports, Free on board (FOB), US Dollars 412465
TMG_CIF_USD Goods, Value of Imports, Cost, Insurance, Freight (CIF), US Dollars 392731

REF_AREA

Code
DOT_DE %>%
  bind_rows(DOT_FR) %>%
  left_join(REF_AREA, by = "REF_AREA") %>%
  group_by(REF_AREA, Ref_area) %>%
  summarise(Nobs = n()) %>%
  arrange(-Nobs) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Ref_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

COUNTERPART_AREA

Code
DOT_DE %>%
  bind_rows(DOT_FR) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  group_by(COUNTERPART_AREA, Counterpart_area) %>%
  summarise(Nobs = n()) %>%
  arrange(-Nobs) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

China

Dollar

Code
DOT_CN %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0("$ ", round(./10^9), " Bn"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

% of GDP

Trade Balance

Code
DOT_CN %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Imports

Code
DOT_CN %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TMG_CIF_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0(round(100*., 2), " %"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Exports

Code
DOT_CN %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TXG_FOB_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0(round(100*., 2), " %"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

China, United States, Europe, France

Code
DOT_CN %>%
  filter(COUNTERPART_AREA  %in% c("CN", "US", "E1", "FR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "US", color2, color),
         color = ifelse(COUNTERPART_AREA == "E1", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() + add_3flags +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .5),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("China Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

United States, Germany

Code
DOT_CN %>%
  filter(COUNTERPART_AREA  %in% c("DE", "US"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#FFDC00", "#003399", "#000000", "#B22234")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") + add_2flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .5),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("China Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

France, Italy, Germany

Code
DOT_CN %>%
  filter(COUNTERPART_AREA  %in% c("FR", "IT", "DE", "ES"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() + add_4flags +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .05),
                     labels = percent_format(accuracy = .01)) + 
  xlab("") + ylab("China Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

United Kingdom

Dollar

Code
DOT_GB %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0("$ ", round(./10^9), " Bn"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

% of GDP

Code
DOT_GB %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

China, United States, Europe, France

Code
DOT_GB %>%
  filter(COUNTERPART_AREA  %in% c("CN", "US", "E1", "FR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#FFDC00", "#003399", "#000000", "#B22234")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United Kingom Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Germany, Sweden, Switzerland

Code
DOT_GB %>%
  filter(COUNTERPART_AREA  %in% c("DE", "SE", "CH"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#000000", "#FF0000", "#6E82B5")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United Kingdom Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Australia, Turkey, United Arab Emirates

Code
DOT_GB %>%
  filter(COUNTERPART_AREA  %in% c("TR", "AE", "AU"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United Kingdom Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Spain, Italy, France, Germany

Code
DOT_GB %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "FR", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#000000", "#009246", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United Kingdom Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Germany

Dollar

Code
DOT_DE %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0("$ ", round(./10^9), " Bn"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

% of GDP

Trade Balance

Nobs

Code
DOT_DE %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  filter(!is.na(OBS_VALUE)) %>%
  group_by(COUNTERPART_AREA, Counterpart_area) %>%
  summarise(Nobs = n(),
         first = first(TIME_PERIOD),
         last = last(TIME_PERIOD)) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

2019, 2013, 2010, 2007, 2004

Code
DOT_DE %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2011", "2007", "2004", "2000", "1997"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

1997-2011

Code
DOT_DE %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2011", "2007", "2004", "2000", "1997"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Change

Code
DOT_DE %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("1997", "2019", "2011"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  transmute(COUNTERPART_AREA, Counterpart_area, 
            `1997-2011` = `2011` - `1997`) %>%
  arrange(`1997-2011`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Imports

Code
DOT_DE %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TMG_CIF_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0(round(100*., 2), " %"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Exports

Code
DOT_DE %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TXG_FOB_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0(round(100*., 2), " %"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

China, United States, Europe, France

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("CN", "US", "E1", "FR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "FR", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

China, United States, France, United Kingdom, UK, Switzerland

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("CN", "US", "E1", "FR", "GB", "CH"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "FR", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_6flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

United Kingdom, Sweden, Switzerland

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("GB", "SE", "CH"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#FECC00", "#FF0000", "#6E82B5")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Australia, Turkey, United Arab Emirates

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("TR", "AE", "AU"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#00008B", "#E30A17", "#00732F")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Spain, Italy, France, United Kingdom

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "FR", "GB"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#009246", "#FFC400", "#CF142B")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

United States, France, United Kingdom

Annual

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("US", "FR", "GB"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#CF142B", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Quarterly

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("US", "FR", "GB"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "Q") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  quarter_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#CF142B", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)")

Spain, Italy, Portugal

Code
DOT_DE %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "PT", "GR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#009246", "#FF0000", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Germany Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

France

Table - 2019, 2013, 2010, 2007, 2004

Code
DOT_FR %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2011", "2007", "2004", "2000", "1997"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

China, Germany, Europe

1980-

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "DE", "U2"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1980-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, 1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

1990-

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "DE", "U2"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1990-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, 1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

China, Germany, Europe

All

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "DE", "U2", "W00"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1960-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, 1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

1980-

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "DE", "U2", "W00"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1980-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, 1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

1990-

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "DE", "U2", "W00"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1990-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, 1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

China, US, Spain, France

All

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("NL", "DE", "ES", "IT"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Dollar

Code
DOT_FR %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0("$ ", round(./10^9, 1), " Bn"))) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F) else .}

% of GDP

Trade Balance

Nobs

Code
DOT_FR %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  filter(!is.na(OBS_VALUE)) %>%
  group_by(COUNTERPART_AREA, Counterpart_area) %>%
  summarise(Nobs = n(),
         first = first(TIME_PERIOD),
         last = last(TIME_PERIOD)) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

1997, 2013, 2011

Code
DOT_FR %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2011", "2007", "2004", "2000", "1997"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Change 1997-2011

Code
DOT_FR %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("1997", "2019", "2011"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  transmute(COUNTERPART_AREA, Counterpart_area, 
            `1997-2011` = `2011` - `1997`) %>%
  arrange(`1997-2011`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Imports

Code
DOT_FR %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TMG_CIF_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0(round(100*., 2), " %"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Exports

Code
DOT_FR %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TXG_FOB_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0(round(100*., 2), " %"))) %>%
  mutate(Flag = gsub(" ", "-", str_to_lower(gsub(" ", "-", Counterpart_area))),
         Flag = paste0('<img src="../../icon/flag/vsmall/', Flag, '.png" alt="Flag">')) %>%
  select(Flag, everything()) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F, escape = F) else .}

Sub-Saharan, Algeria

Sub-Saharan, Korea, Japan

Trade Balance

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("JP", "F6", "KR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = case_when(COUNTERPART_AREA == "KR" ~ "Korea",
                                      T ~ Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Sub-Saharan, Korea, Japan

Trade Balance

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("JP", "F6", "KR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = case_when(COUNTERPART_AREA == "KR" ~ "Korea",
                                      T ~ Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Exports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("JP", "F6", "KR"),
         INDICATOR %in% c("TXG_FOB_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = case_when(COUNTERPART_AREA == "KR" ~ "Korea",
                                      T ~ Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Imports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("JP", "F6", "KR"),
         INDICATOR %in% c("TMG_CIF_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = case_when(COUNTERPART_AREA == "KR" ~ "Korea",
                                      T ~ Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

China, US, Spain, Germany

All

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "US", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

1980-

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("CN", "US", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              filter(REF_AREA == "FR",
                     FREQ == "A") %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = "TIME_PERIOD") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1980-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

United Kingdom, Sweden, Switzerland

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("GB", "SE", "CH"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Australia, Turkey, United Arab Emirates

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("TR", "AE", "AU"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .1),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Spain, Italy, Germany, United Kingdom

Trade Balance

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "DE", "GB"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Imports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "DE", "GB"),
         INDICATOR %in% c("TMG_CIF_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Imports (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Exports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "DE", "GB"),
         INDICATOR %in% c("TXG_FOB_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Exports (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Imports and Exports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "DE", "GB"),
         INDICATOR %in% c("TMG_CIF_USD", "TXG_FOB_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#000000", "#009246", "#FFC400", "#CF142B")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area, linetype = INDICATOR)) + 
  theme(legend.position = "none") +
  geom_image(data = . %>%
               filter(date == as.Date("2014-01-01")) %>%
               mutate(image = paste0("../../icon/flag/", str_to_lower(gsub(" ", "-", Counterpart_area)), ".png")),
             aes(x = date, y = OBS_VALUE, image = image), asp = 1.5) +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Imports (% of GDP)")

Spain, Italy, Portugal, Greece

Trade Balance

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "PT", "GR"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  ggplot(.) + theme_minimal() + scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") + add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("France Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Imports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "PT", "GR"),
         INDICATOR %in% c("TMG_CIF_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#009246", "#FF0000", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Imports (% of GDP)")

Exports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "PT", "GR"),
         INDICATOR %in% c("TXG_FOB_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1991-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#0D5EAF", "#009246", "#FF0000", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Exports (% of GDP)")

Germany

Imports and Exports

Code
DOT_FR %>%
  filter(COUNTERPART_AREA  %in% c("DE"),
         INDICATOR %in% c("TMG_CIF_USD", "TXG_FOB_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  #filter(date >= as.Date("1970-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(tibble(INDICATOR = c("TMG_CIF_USD", "TXG_FOB_USD"),
                   Indicator = c("Imports", "Exports")), by = "INDICATOR") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#000000", "#009246", "#FFC400", "#CF142B")) +
  geom_line(aes(x = date, y = OBS_VALUE, linetype = Indicator)) + 
  theme(legend.position = c(0.3, 0.9)) +
  geom_image(data = . %>%
               filter(date == as.Date("2014-01-01")) %>%
               mutate(image = paste0("../../icon/flag/", str_to_lower(gsub(" ", "-", Counterpart_area)), ".png")),
             aes(x = date, y = OBS_VALUE, image = image), asp = 1.5) +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .5),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("% of GDP")

Annual Germany’s Trade Surpluses Decomposed

% of GDP: To China and China - Annual

All

Europe

Trade Surpluses Decomposed

Dollar

Code
DOT_U2 %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0("$ ", round(./10^9, 1), " Bn"))) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F) else .}

% of GDP

Code
DOT_U2 %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F) else .}

Spain, Italy, France, Germany

Trade Balance (Dollars)

Year

Code
DOT_U2 %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "FR", "DE", "NL"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  year_to_date2 %>%
  filter(date >= as.Date("1995-01-01")) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(OBS_VALUE = OBS_VALUE/1000) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#002395", "#000000", "#009246", "#AE1C28", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") + add_5flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = seq(-500, 500, 50)) + 
  xlab("") + ylab("Europe Bilateral Trade Surplus (% of GDP)")

Quarterly

Code
DOT_U2 %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "FR", "DE", "NL"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "Q") %>%
  quarter_to_date2 %>%
  filter(date >= as.Date("1995-01-01")) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(OBS_VALUE = OBS_VALUE/1000) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#002395", "#000000", "#009246", "#AE1C28", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") + add_5flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = seq(-500, 500, 10)) + 
  xlab("") + ylab("Europe Bilateral Trade Surplus (% of GDP)")

Monthly

Code
DOT_U2 %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "FR", "DE", "NL"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "M") %>%
  month_to_date2 %>%
  filter(date >= as.Date("1995-01-01")) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(OBS_VALUE = OBS_VALUE/1000) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#002395", "#000000", "#009246", "#AE1C28", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") + add_5flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = seq(-500, 500, 5)) + 
  xlab("") + ylab("Europe Bilateral Trade Surplus (% of GDP)")

Trade Balance (% of GDP)

Code
DOT_U2 %>%
  filter(COUNTERPART_AREA  %in% c("ES", "IT", "FR", "DE", "NL"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1995-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#002395", "#000000", "#009246", "#AE1C28", "#FFC400")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  geom_image(data = . %>%
               filter(date == as.Date("2005-01-01")) %>%
               mutate(image = paste0("../../icon/flag/", str_to_lower(gsub(" ", "-", Counterpart_area)), ".png")),
             aes(x = date, y = OBS_VALUE, image = image), asp = 1.5) +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Europe Bilateral Trade Surplus (% of GDP)")

% of GDP: To China and China - Annual

Code
DOT_U2 %>%
  filter(COUNTERPART_AREA  %in% c("CN", "FR", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1995-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  ggplot(.) + theme_minimal() + 
  scale_color_manual(values = c("#FFDC00", "#003399", "#000000", "#B22234")) +
  geom_line(aes(x = date, y = OBS_VALUE, color = Counterpart_area)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("Europe Bilateral Trade Surplus (% of GDP)")

United States

Trade Surpluses Decomposed

Dollar

Code
DOT_US %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(paste0("$ ", round(./10^9, 1), " Bn"))) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F) else .}

% of GDP

Code
DOT_US %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  filter(TIME_PERIOD %in% c("2019", "2013", "2010", "2007", "2004"),
         INDICATOR == "TBG_USD") %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  select(COUNTERPART_AREA, Counterpart_area, TIME_PERIOD, OBS_VALUE) %>%
  spread(TIME_PERIOD, OBS_VALUE) %>%
  arrange(-`2019`) %>%
  mutate_at(vars(-1, -2), funs(round(100*., 2))) %>%
  {if (is_html_output()) datatable(., filter = 'top', rownames = F) else .}

Europe, France, Germany, China

All

Code
DOT_US %>%
  filter(COUNTERPART_AREA  %in% c("CN", "FR", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1950-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "FR", color2, color)) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United States Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

1960-

Code
DOT_US %>%
  filter(COUNTERPART_AREA  %in% c("CN", "FR", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1960-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "FR", color2, color)) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United States Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

1980-

Code
DOT_US %>%
  filter(COUNTERPART_AREA  %in% c("CN", "FR", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)) %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1980-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "FR", color2, color)) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United States Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Europe, Germany, China, World

1980-

Code
DOT_US %>%
  filter(COUNTERPART_AREA  %in% c("CN", "E1", "DE", "W00"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1980-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_4flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United States Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")

Europe, Germany, China, World

1980-

Code
DOT_US %>%
  filter(COUNTERPART_AREA  %in% c("CN", "E1", "DE"),
         INDICATOR %in% c("TBG_USD"),
         FREQ == "A") %>%
  left_join(NGDP_USD %>%
              select(FREQ, REF_AREA, TIME_PERIOD, NGDP_USD = OBS_VALUE),
            by = c("TIME_PERIOD", "REF_AREA", "FREQ")) %>%
  year_to_date2 %>%
  filter(date >= as.Date("1980-01-01")) %>%
  mutate(OBS_VALUE = OBS_VALUE*10^(UNIT_MULT)/NGDP_USD) %>%
  left_join(COUNTERPART_AREA, by = "COUNTERPART_AREA") %>%
  mutate(Counterpart_area = ifelse(COUNTERPART_AREA == "U2", "Europe", Counterpart_area),
         Counterpart_area = ifelse(COUNTERPART_AREA == "W00", "World", Counterpart_area)) %>%
  left_join(colors, by = c("Counterpart_area" = "country")) %>%
  mutate(color = ifelse(COUNTERPART_AREA == "DE", color2, color)) %>%
  ggplot(.) + theme_minimal() + 
  scale_color_identity() +
  geom_line(aes(x = date, y = OBS_VALUE, color = color)) + 
  theme(legend.position = "none") +
  add_3flags +
  scale_x_date(breaks = seq(1950, 2100, 5) %>% paste0("-01-01") %>% as.Date,
               labels = date_format("%Y")) +
  scale_y_continuous(breaks = 0.01*seq(-500, 500, .2),
                     labels = percent_format(accuracy = .1)) + 
  xlab("") + ylab("United States Bilateral Trade Surplus (% of GDP)") + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black")