Canadian Trade - Imports and Exports
Graphs of trade surplus/deficits with other countries using STATCAN data
Data
STATCAN Table: 12-10-0011-01 (International merchandise trade for all countries)
Prepare Data
# Prep data
myCaption <- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: STATCAN"
dd <- read.csv("1210001101_databaseLoadingData.csv") %>%
select(Year=REF_DATE, Area=GEO, Trade, Basis, Seasonal.adjustment,
Trading.Partner=Principal.trading.partners,
Unit=UOM, Scale=SCALAR_FACTOR, Value=VALUE) %>%
separate(Year, c("Year","Month"), "-") %>%
mutate(Date = as.Date(paste0(Year, "-", Month, "-15")))
xx <- dd %>% group_by(Trading.Partner) %>%
summarise(Value = max(Value, na.rm = T)) %>% arrange(desc(Value))
dd <- dd %>%
mutate(Trading.Partner = factor(Trading.Partner, levels = xx$Trading.Partner))
Trade Balance
Canada
# Prep data
xx <- dd %>%
filter(Trading.Partner == "All countries", Trade == "Trade Balance") %>%
mutate(Balance = ifelse(Value > 0, "Net Export", "Net Import"))
# Plot
mp <- ggplot(xx, aes(x = Date, y = Value / 1000)) +
geom_col(aes(fill = Balance), color = "black", lwd = 0.1, alpha = 0.8) +
stat_smooth(geom = "line", alpha = 0.7) +
scale_fill_manual(name = NULL, values = c("steelblue", "darkred")) +
scale_x_date(date_minor_breaks = "1 year", expand = c(0,0)) +
scale_y_continuous(minor_breaks = -6:6) +
theme_agData(legend.position = "bottom") +
labs(title = "Canada's Trade Balance",
x = NULL, y = "Billion CAD", caption = myCaption)
ggsave("canada_trade_01.png", mp, width = 6, height = 4)
Provinces & Cities
# Prep data
xx <- dd %>%
filter(Trading.Partner != "All countries", Trade == "Trade Balance") %>%
mutate(Balance = ifelse(Value > 0, "Net Export", "Net Import"))
# Plot
mp <- ggplot(xx, aes(x = Date, y = Value / 1000)) +
geom_col(aes(fill = Balance), alpha = 0.9) +
stat_smooth(geom = "line", alpha = 0.7) +
scale_fill_manual(name = NULL, values = c("steelblue", "darkred")) +
facet_wrap(Trading.Partner ~ ., scales = "free_y", ncol = 6) +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
theme_agData(legend.position = "bottom") +
labs(title = "Canada's Trade Balance With Other Countries",
x = NULL, y = "Billion $", caption = myCaption)
ggsave("canada_trade_02.png", mp, width = 14, height = 8)
Import & Export
Canada
# Prep data
xx <- dd %>%
filter(Trading.Partner == "All countries", Trade != "Trade Balance") %>%
mutate(Value = ifelse(Trade == "Import", -Value, Value))
# Plot
mp <- ggplot(xx, aes(x = Date, y = Value / 1000, fill = Trade)) +
geom_col(alpha = 0.9) +
facet_wrap(Trading.Partner ~ ., scales = "free_y", ncol = 7) +
scale_fill_manual(name = NULL, values = c("steelblue", "darkred")) +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
theme_agData(legend.position = "bottom") +
labs(title = "Canada's Trade Balance With Other Countries",
x = NULL, y = "Billion $", caption = myCaption)
ggsave("canada_trade_03.png", mp, width = 6, height = 4)
Provinces & Cities
# Prep data
xx <- dd %>%
filter(Trading.Partner != "All countries", Trade != "Trade Balance") %>%
mutate(Value = ifelse(Trade == "Import", -Value, Value))
# Plot
mp <- ggplot(xx, aes(x = Date, y = Value / 1000, fill = Trade)) +
geom_col(alpha = 0.9) +
facet_wrap(Trading.Partner ~ ., scales = "free_y", ncol = 6) +
scale_fill_manual(name = NULL, values = c("steelblue", "darkred")) +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
theme_agData(legend.position = "bottom") +
labs(title = "Canada's Trade Balance With Other Countries",
x = NULL, y = "Billion $", caption = myCaption)
ggsave("canada_trade_04.png", mp, width = 14, height = 8)
Countries
Create plotting function
# Create plotting function
gg_Trade <- function(myArea = "India") {
# Prep data
xx <- dd %>%
filter(Trading.Partner == myArea, Trade != "Trade Balance") %>%
mutate(Value = ifelse(Trade == "Import", -Value, Value))
# Plot
mp1 <- ggplot(xx, aes(x = Date, y = Value / 1000, fill = Trade)) +
geom_col(alpha = 0.9) +
scale_fill_manual(name = NULL, values = c("steelblue", "darkred")) +
scale_x_date(date_breaks = "10 years", date_labels = "%Y") +
theme_agData(legend.position = "bottom") +
labs(subtitle = paste("Canada's Trade Balance With", myArea),
x = NULL, y = "Billion $", caption = "")
#
xx <- dd %>%
filter(Trading.Partner == myArea, Trade == "Trade Balance") %>%
mutate(Balance = ifelse(Value > 0, "Net Export", "Net Import"))
# Plot
mp2 <- ggplot(xx, aes(x = Date, y = Value / 1000)) +
geom_col(aes(fill = Balance), alpha = 0.9) +
stat_smooth(geom = "line", alpha = 0.7) +
scale_fill_manual(name = NULL, values = c("steelblue", "darkred")) +
scale_x_date(date_minor_breaks = "1 year", expand = c(0,0)) +
scale_y_continuous(minor_breaks = -6:6) +
theme_agData(legend.position = "bottom") +
labs(subtitle = "", x = NULL, y = "Billion CAD", caption = myCaption)
ggarrange(mp1, mp2)
}
United States
United Kingdom
Algeria
Norway
Russia
mp <- gg_Trade(myArea = "Russian Federation")
ggsave("canada_trade_russia.png", mp, width = 8, height = 4)
Saudi Arabia
mp <- gg_Trade(myArea = "Saudi Arabia")
ggsave("canada_trade_saudi_arabia.png", mp, width = 8, height = 4)