Canadian Trade - Imports and Exports
Graphs of trade surplus/deficits with other countries using STATCAN data
Data
STATCAN Table: 12-10-0011-01
Prepare Data
# devtools::install_github("derekmichaelwright/agData")
library(agData)
# Prep data
<- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: STATCAN"
myCaption <- read.csv("1210001101_databaseLoadingData.csv") %>%
dd select(Year=1, 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")))
<- dd %>% filter(Trade == "Trade Balance", Date == "2022-11-15") %>%
xx arrange(desc(Value))
<- dd %>%
dd mutate(Trading.Partner = factor(Trading.Partner, levels = xx$Trading.Partner))
Trade Balance
Canada
# Prep data
<- dd %>%
xx filter(Trading.Partner == "All countries", Trade == "Trade Balance") %>%
mutate(Balance = ifelse(Value > 0, "Net Export", "Net Import"))
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000)) +
mp 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
<- dd %>%
xx filter(Trading.Partner != "All countries", Trade == "Trade Balance") %>%
mutate(Balance = ifelse(Value > 0, "Net Export", "Net Import"))
# Plot
<- ggplot(xx, aes(x = Date, y = Value)) +
mp 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 = 7) +
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 = "Million $", caption = myCaption)
ggsave("canada_trade_02.png", mp, width = 14, height = 8)
Import & Export
Canada
# Prep data
<- dd %>%
xx filter(Trading.Partner == "All countries", Trade != "Trade Balance") %>%
mutate(Value = ifelse(Trade == "Import", -Value, Value))
# Plot
<- ggplot(xx, aes(x = Date, y = Value, fill = Trade)) +
mp 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 = "Million $", caption = myCaption)
ggsave("canada_trade_03.png", mp, width = 6, height = 4)
Provinces & Cities
# Prep data
<- dd %>%
xx filter(Trading.Partner != "All countries", Trade != "Trade Balance") %>%
mutate(Value = ifelse(Trade == "Import", -Value, Value))
# Plot
<- ggplot(xx, aes(x = Date, y = Value, fill = Trade)) +
mp 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 = "Million $", caption = myCaption)
ggsave("canada_trade_04.png", mp, width = 14, height = 8)
Countries
Create plotting function
# Create plotting function
<- function(myArea = "India") {
gg_Trade # Prep data
<- dd %>%
xx filter(Trading.Partner == myArea, Trade != "Trade Balance") %>%
mutate(Value = ifelse(Trade == "Import", -Value, Value))
# Plot
<- ggplot(xx, aes(x = Date, y = Value, fill = Trade)) +
mp1 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 = "Million $", caption = "")
#
<- dd %>%
xx filter(Trading.Partner == myArea, Trade == "Trade Balance") %>%
mutate(Balance = ifelse(Value > 0, "Net Export", "Net Import"))
# Plot
<- ggplot(xx, aes(x = Date, y = Value)) +
mp2 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 = "Million CAD", caption = myCaption)
ggarrange(mp1, mp2)
}
United States
<- gg_Trade(myArea = "United States")
mp ggsave("canada_trade_us.png", mp, width = 8, height = 4)
United Kingdom
<- gg_Trade(myArea = "United Kingdom")
mp ggsave("canada_trade_uk.png", mp, width = 8, height = 4)
Japan
<- gg_Trade(myArea = "Japan")
mp ggsave("canada_trade_japan.png", mp, width = 8, height = 4)
Algeria
<- gg_Trade(myArea = "Algeria")
mp ggsave("canada_trade_algeria.png", mp, width = 8, height = 4)
Norway
<- gg_Trade(myArea = "Norway")
mp ggsave("canada_trade_norway.png", mp, width = 8, height = 4)
India
<- gg_Trade(myArea = "India")
mp ggsave("canada_trade_india.png", mp, width = 8, height = 4)
Iraq
<- gg_Trade(myArea = "Iraq")
mp ggsave("canada_trade_iraq.png", mp, width = 8, height = 4)
Russia
<- gg_Trade(myArea = "Russian Federation")
mp ggsave("canada_trade_russia.png", mp, width = 8, height = 4)
Saudi Arabia
<- gg_Trade(myArea = "Saudi Arabia")
mp ggsave("canada_trade_saudi_arabia.png", mp, width = 8, height = 4)
China
<- gg_Trade(myArea = "China")
mp ggsave("canada_trade_china.png", mp, width = 8, height = 4)
Germany
<- gg_Trade(myArea = "Germany")
mp ggsave("canada_trade_germany.png", mp, width = 8, height = 4)
Mexico
<- gg_Trade(myArea = "Mexico")
mp ggsave("canada_trade_mexico.png", mp, width = 8, height = 4)