9 International Trade of EU
Libraries we will need:
library(tidyverse)
library(eurostat)
library(scales)
library(leaflet)
library(sf)
library(ggrepel)
library(ggthemes)
library(wesanderson)
library(igraph)
As usual, we will modify the EU countries tables:
And we will get the shapefiles of EU countries to be used in maos and choropleths:
EU_SHP_0 <- get_eurostat_geospatial(resolution = 10, nuts_level = 0, year = 2016) %>%
filter(geo %in% EU_countries$geo) %>%
select(geo, geometry)
9.1 Exports of EU countries inside and outside EU
Table ext_tec03 contains annual data about international trade of EU countries, trade by partner country and NACE Rev. 2 activity. Let’s download:
Let us examine a little bit the data. We choose Germany for the year 2016 as example:
## # A tibble: 1,560 × 7
## unit stk_flow nace_r2 partner geo time values
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 NR_ENT EXP A_F_H-U AE DE 2016 1173
## 2 NR_ENT EXP A_F_H-U AFR_N DE 2016 1115
## 3 NR_ENT EXP A_F_H-U AFR_OTH DE 2016 1545
## 4 NR_ENT EXP A_F_H-U AME_C_CRB DE 2016 1049
## 5 NR_ENT EXP A_F_H-U AME_N DE 2016 4458
## 6 NR_ENT EXP A_F_H-U AME_S DE 2016 1247
## 7 NR_ENT EXP A_F_H-U AR DE 2016 269
## 8 NR_ENT EXP A_F_H-U ASI_NME DE 2016 3084
## 9 NR_ENT EXP A_F_H-U ASI_OTH DE 2016 5361
## 10 NR_ENT EXP A_F_H-U AT DE 2016 1778
## # … with 1,550 more rows
We get a lot of data because we the dataset provides multiple entries for : - unit - flow - nace_r2 - partner
## # A tibble: 780 × 7
## unit stk_flow nace_r2 partner geo time values
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 THS_EUR EXP A_F_H-U AE DE 2016 307960.
## 2 THS_EUR EXP A_F_H-U AFR_N DE 2016 345850.
## 3 THS_EUR EXP A_F_H-U AFR_OTH DE 2016 583489.
## 4 THS_EUR EXP A_F_H-U AME_C_CRB DE 2016 531009.
## 5 THS_EUR EXP A_F_H-U AME_N DE 2016 3561899.
## 6 THS_EUR EXP A_F_H-U AME_S DE 2016 676355.
## 7 THS_EUR EXP A_F_H-U AR DE 2016 73745.
## 8 THS_EUR EXP A_F_H-U ASI_NME DE 2016 1123428.
## 9 THS_EUR EXP A_F_H-U ASI_OTH DE 2016 5338715.
## 10 THS_EUR EXP A_F_H-U AT DE 2016 2723769.
## # … with 770 more rows
9.2 Intra EU trade
Get the data
Filter:
ext_tec03 %>%
filter(unit == 'THS_EUR') %>%
filter(stk_flow == 'EXP') %>%
filter(nace_r2 == 'TOTAL') %>%
filter(partner %in% c('INT_EU', 'WORLD')) %>%
filter(time == 2017)
## # A tibble: 70 × 7
## unit stk_flow nace_r2 partner geo time values
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 THS_EUR EXP TOTAL INT_EU AT 2017 104803571.
## 2 THS_EUR EXP TOTAL INT_EU BA 2017 4021610.
## 3 THS_EUR EXP TOTAL INT_EU BE 2017 273351525.
## 4 THS_EUR EXP TOTAL INT_EU BG 2017 16985170.
## 5 THS_EUR EXP TOTAL INT_EU CH 2017 120059878.
## 6 THS_EUR EXP TOTAL INT_EU CY 2017 1111658.
## 7 THS_EUR EXP TOTAL INT_EU CZ 2017 91528346.
## 8 THS_EUR EXP TOTAL INT_EU DE 2017 634176947.
## 9 THS_EUR EXP TOTAL INT_EU DK 2017 54134399.
## 10 THS_EUR EXP TOTAL INT_EU EE 2017 9242929.
## # … with 60 more rows
Join with eu_countries:
ext_tec03 %>%
filter(unit == 'THS_EUR') %>%
filter(stk_flow == 'EXP') %>%
filter(nace_r2 == 'TOTAL') %>%
filter(partner %in% c('INT_EU', 'WORLD')) %>%
filter(time == 2017) %>%
inner_join(EU_countries, by = "geo")
## # A tibble: 56 × 8
## unit stk_flow nace_r2 partner geo time values country
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 THS_EUR EXP TOTAL INT_EU AT 2017 104803571. Austria
## 2 THS_EUR EXP TOTAL INT_EU BE 2017 273351525. Belgium
## 3 THS_EUR EXP TOTAL INT_EU BG 2017 16985170. Bulgaria
## 4 THS_EUR EXP TOTAL INT_EU CY 2017 1111658. Cyprus
## 5 THS_EUR EXP TOTAL INT_EU CZ 2017 91528346. Czechia
## 6 THS_EUR EXP TOTAL INT_EU DE 2017 634176947. Germany
## 7 THS_EUR EXP TOTAL INT_EU DK 2017 54134399. Denmark
## 8 THS_EUR EXP TOTAL INT_EU EE 2017 9242929. Estonia
## 9 THS_EUR EXP TOTAL INT_EU EL 2017 15134594. Greece
## 10 THS_EUR EXP TOTAL INT_EU ES 2017 172651570. Spain
## # … with 46 more rows
Pivot the the table to add more columns:
eu_trade_intra <- ext_tec03 %>%
filter(unit == 'THS_EUR') %>%
filter(stk_flow == 'EXP') %>%
filter(nace_r2 == 'TOTAL') %>%
filter(partner %in% c('INT_EU', 'WORLD')) %>%
filter(time == 2017) %>%
inner_join(EU_countries, by = "geo") %>%
pivot_wider(id_cols = c(geo, country),
names_from = partner,
values_from = values)
Mutate and store the data:
eu_trade_intra <- ext_tec03 %>%
filter(unit == 'THS_EUR') %>%
filter(stk_flow == 'EXP') %>%
filter(nace_r2 == 'TOTAL') %>%
filter(partner %in% c('INT_EU', 'WORLD')) %>%
filter(time == 2017) %>%
inner_join(EU_countries, by = "geo") %>%
pivot_wider(id_cols = c(geo, country),
names_from = partner,
values_from = values) %>%
mutate(perc = round(100*INT_EU/WORLD, 1)) %>%
select(geo, country, perc)
Plot the data as points:
ggplot(data = filter(eu_trade_intra, country != 'Malta'),
aes(x = reorder(country, perc), y = perc)) +
geom_point(size = 3, color = wes_palette("BottleRocket1", 1)[[1]]) +
coord_flip() +
labs(x = "Country", y = "% of intra-EU exports",
title = "Intra-EU exports of EU countries",
subtitle = "% of total exports during 2017",
caption = "Source: Eurostat table ext_tec03
Apllied Informatics and Computational Economics Lab") +
theme_wsj() +
theme(text = element_text(size = 14),
axis.text = element_text(size = 12),
plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 14, face = "bold"),
plot.caption = element_text(size = 12))
Or plot the data as chorepleth map:
ggplot(eu_trade_intra_SHP) +
geom_sf() +
geom_sf(aes(fill = perc), color = "white", size = 0) +
geom_sf(data = EU_SHP_0, fill = NA, color = "white", size = 0.5) +
scale_fill_continuous_tableau(palette = "Classic Red", breaks = seq(0, 100, by = 10), na.value = "gray60") +
xlim(-10.0, 38.0) + ylim(35.5, 66.0) +
labs(title = "EU Country exports to other EU countries",
subtitle = "% of Total Exports",
caption = "Source: Eurostat database ext_tec01
Apllied Informatics and Computational Economics Lab",
fill = "% Exports.") +
theme_void() +
theme(text = element_text(size = 16, family = "Arial")) +
theme(legend.key.height = unit(2, "cm")) +
theme(legend.position = c(0.95, 0.5))+
theme(plot.title = element_text(hjust = 0.0, vjust = -5)) +
theme(plot.subtitle = element_text(hjust = -0.1, vjust = -14))
9.3 Network of connected countries
x1 <- ext_tec03 %>%
inner_join(EU_countries, by = "geo") %>%
filter(unit == 'THS_EUR') %>%
filter(stk_flow == 'EXP') %>%
filter(nace_r2 == 'TOTAL') %>%
filter(partner != 'WORLD') %>%
filter(time == 2017) %>%
pivot_wider(id_cols = c(geo, country),
names_from = partner,
names_prefix = "p_",
values_from = values)
x2 <- ext_tec03 %>%
inner_join(EU_countries, by = "geo") %>%
filter(unit == 'THS_EUR') %>%
filter(stk_flow == 'EXP') %>%
filter(nace_r2 == 'TOTAL') %>%
filter(partner == 'WORLD') %>%
filter(time == 2017) %>%
select(geo, WORLD = values)
trade_partner <- inner_join(x1, x2, by = "geo") %>%
mutate_at(vars(starts_with("p_")), list( ~round(. *100 / WORLD, 1))) %>%
select(-WORLD) %>%
pivot_longer(cols = starts_with("p_"),
names_to = "partner",
values_to = "values",
values_drop_na = TRUE
) %>%
mutate(partner = str_sub(partner, 3, -1)) %>%
filter(partner %in% EU_countries$geo) %>%
filter(values > 5) %>%
select(from = geo, to = partner, weight = values)