Canadian Fossil Fuel Production and Import
Graphs of crude oil, natural gas, and coal production, import and export using STATCAN data
Data
STATCAN Table: 25-10-0063-01
STATCAN Table: 25-10-0055-01
STATCAN Table: 25-10-0046-01
Prepare Data
# devtools::install_github("derekmichaelwright/agData")
library(agData)
# Prep data
<- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: STATCAN"
myCaption <- c("black", "grey50", "slategray3")
myColors1 <- c("darkgreen", "steelblue", "darkred")
myColors2 <- c("Production", "Exports", "Imports",
myMeasures "Gross withdrawals", "Industrial consumption",
"Residential consumption", "Commercial consumption")
# Oil
<- read.csv("2510006301_databaseLoadingData.csv") %>%
dd_oil mutate(Item = "Crude oil") %>%
select(Date=1, Area=GEO, Item, Measurement=4, Unit=UOM, Value=VALUE) %>%
mutate(Area = factor(Area, levels = agData_STATCAN_Region_Table$Area),
Date = as.Date(paste0(Date,"-01"), format = "%Y-%m-%d"),
Measurement = plyr::mapvalues(Measurement,
"Crude oil production", "Production"),
Measurement = factor(Measurement, levels = myMeasures)) %>%
filter(!is.na(Value))
# Natural gas
<- read.csv("2510005501_databaseLoadingData.csv") %>%
dd_gas mutate(Item = "Natural gas") %>%
select(Date=1, Area=GEO, Item, Measurement=4, Unit=UOM, Value=VALUE) %>%
mutate(Area = factor(Area, levels = agData_STATCAN_Region_Table$Area),
Date = as.Date(paste0(Date,"-01"), format = "%Y-%m-%d"),
Measurement = plyr::mapvalues(Measurement,
"Marketable production", "Production"),
Measurement = factor(Measurement, levels = myMeasures),
Unit = "Thousand cubic meters") %>%
filter(!is.na(Value))
# Coal
<- read.csv("2510004601_databaseLoadingData.csv") %>%
dd_coal mutate(Item = "Coal") %>%
select(Date=1, Area=GEO, Item, Measurement=5, Unit=UOM, Value=VALUE) %>%
mutate(Area = factor(Area, levels = agData_STATCAN_Region_Table$Area),
Date = as.Date(paste0(Date,"-01"), format = "%Y-%m-%d"),
Measurement = factor(Measurement, levels = myMeasures),
Value = 1000 * Value, UOM = "Kilograms") %>%
filter(!is.na(Value))
#
<- bind_rows(dd_oil, dd_gas, dd_coal) dd
Fossil Fuels
Production, Export & Import
# Create plotting function
<- function(myArea) {
gg_PEI # Prep data
<- c("Production", "Exports", "Imports")
myMeasures <- dd %>%
xx filter(Area == myArea, Measurement %in% myMeasures,
> as.Date("2015-12-30"))
Date <- c("Coal (Tonnes)", "Crude oil (Cubic metres)",
myItems "Natural gas (Thousand cubic meters)")
# Plot
ggplot(xx, aes(x = Date, y = Value / 1000000,
color = paste0(Item," (", Unit, ")"))) +
geom_line(size = 1, alpha = 0.7) +
scale_color_manual(name = NULL, values = myColors1, breaks = myItems) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
facet_grid(. ~ Measurement, scales = "free_y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = paste(myArea, "- Fossil Fuel Production, Export & Import"),
y = "Value / 1,000,000", x = NULL, caption = myCaption)
}
Canada
# Plot
<- gg_PEI(myArea = "Canada")
mp ggsave("canada_fossil_fuels_1_01.png", mp, width = 8, height = 4)
Alberta
# Plot
<- gg_PEI(myArea = "Alberta")
mp ggsave("canada_fossil_fuels_1_02.png", mp, width = 8, height = 4)
British Columbia
<- gg_PEI(myArea = "British Columbia")
mp ggsave("canada_fossil_fuels_1_03.png", mp, width = 8, height = 4)
Saskatchewan
<- gg_PEI(myArea = "Saskatchewan")
mp ggsave("canada_fossil_fuels_1_04.png", mp, width = 8, height = 4)
Manitoba
<- gg_PEI(myArea = "Manitoba")
mp ggsave("canada_fossil_fuels_1_05.png", mp, width = 8, height = 4)
Ontario
<- gg_PEI(myArea = "Ontario")
mp ggsave("canada_fossil_fuels_1_06.png", mp, width = 8, height = 4)
Quebec
<- gg_PEI(myArea = "Quebec")
mp ggsave("canada_fossil_fuels_1_07.png", mp, width = 8, height = 4)
Production
Canada
# Prep data
<- dd %>%
xx filter(Area == "Canada", Measurement == "Production",
> as.Date("2015-12-30"))
Date # Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Item)) +
mp geom_line(size = 1, alpha = 0.7) +
stat_smooth(geom = "line", se = F, color = "black", size = 1) +
scale_color_manual(name = NULL, values = myColors1) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
facet_wrap(paste0(Item," (", Unit, ")") ~ ., scales = "free_y") +
theme_agData(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none") +
labs(title = "Canadian Fossil Fuel Production",
y = "Value / 1,000,000", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_1_08.png", mp, width = 8, height = 4)
Provinces
# Prep data
<- dd %>%
xx filter(Area %in% agData_STATCAN_Region_Table$Area[-1],
== "Production", Date > as.Date("2015-12-30"))
Measurement # Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Item)) +
mp geom_line(size = 1, alpha = 0.7) +
scale_color_manual(name = NULL, values = myColors1) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
facet_wrap(Area ~ ., scales = "free_y", ncol = 5) +
theme_agData(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "bottom") +
labs(title = "Canadian Fossil Fuel Production",
y = "Value / 1,000,000", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_1_09.png", mp, width = 12, height = 6)
Crude Oil
Canada
# Prep data
<- dd_oil %>% filter(Area == "Canada")
xx # Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(size = 1.5, alpha = 0.7) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
ylim(c(0, 24)) +
theme_agData(legend.position = "bottom") +
labs(title = "Canadaian Crude Oil Production, Export & Import",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_2_01.png", mp, width = 6, height = 4)
Provinces
# Prep data
<- dd_oil %>% filter(Area != "Canada")
xx # Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(alpha = 0.7, size = 1.25) +
facet_wrap(Area ~ ., scales = "free_y", ncol = 4) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Canadian Crude Oil Production, Export & Import",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_2_02.png", mp, width = 10, height = 5)
AB & SK
# Prep data
<- c("All Other Provinces", "Saskatchewan", "Alberta")
myAreas <- dd_oil %>%
xx filter(Measurement == "Production",
!Area %in% c("Canada", "Atlantic provinces")) %>%
mutate(Area = ifelse(Area %in% myAreas, as.character(Area), "All Other Provinces"),
Area = factor(Area, levels = myAreas)) %>%
group_by(Date, Area) %>%
summarise(Value = sum(Value, na.rm = T))
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, fill = Area)) +
mp geom_col(color = "black", lwd = 0.3,
alpha = 0.7) +
scale_fill_manual(name = NULL, values = myColors2[c(2,1,3)], breaks = rev(myAreas)) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Canadian Crude Oil Production",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_2_03.png", mp, width = 6, height = 4)
Predictions
Using a simple linear model.
# Prep data
<- dd_oil %>% filter(Area == "Canada", Measurement == "Production")
xx <- lm(Value ~ Date, data = xx)
fit <- data.frame(Date = as.Date(c("2022-04-01", "2030-01-01"), format = "%Y-%m-%d"))
x2 $Value <- predict(fit, newdata = x2)
x2# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000)) +
mp geom_line(lwd = 1, alpha = 0.7) +
stat_smooth(geom = "line", method = "lm", lwd = 1.5,
color = "darkred", alpha = 0.7) +
geom_line(data = x2, lwd = 1.5, lty = 2, alpha = 0.7) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Simple Predictions of Crude Oil Production in Canada",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_2_04.png", mp, width = 6, height = 4)
Natural Gas
Canada
# Prep data
<- c("Production", "Exports", "Imports")
myMeasures <- dd_gas %>% filter(Area == "Canada", Measurement %in% myMeasures)
xx # Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(size = 1.5, alpha = 0.7) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
ylim(c(0, 17)) +
theme_agData(legend.position = "bottom") +
labs(title = "Canadaian Natural Gas Production, Export & Import",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_3_01.png", mp, width = 6, height = 4)
Provinces
# Prep data
<- c("Production", "Exports", "Imports")
myMeasures <- dd_gas %>% filter(Area != "Canada", Measurement %in% myMeasures)
xx # Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(alpha = 0.7, size = 1.25) +
facet_wrap(Area ~ ., scales = "free_y", ncol = 5) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Natural Gas Production, Export & Import",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_3_02.png", mp, width = 12, height = 6)
AB & BC
# Prep data
<- c("All Other Provinces", "British Columbia", "Alberta")
myAreas <- dd_gas %>%
xx filter(Measurement == "Production",
!Area %in% c("Canada", "Atlantic provinces")) %>%
mutate(Area = ifelse(Area %in% myAreas, as.character(Area), "All Other Provinces"),
Area = factor(Area, levels = myAreas)) %>%
group_by(Date, Area) %>%
summarise(Value = sum(Value, na.rm = T))
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, fill = Area)) +
mp geom_col(color = "black", lwd = 0.3, alpha = 0.7) +
scale_fill_manual(name = NULL, values = myColors2[c(2,1,3)], breaks = rev(myAreas)) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Canadian Natural Gas Production",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_3_03.png", mp, width = 6, height = 4)
Predictions
Using a simple linear model.
# Prep data
<- dd_gas %>% filter(Area == "Canada", Measurement == "Production")
xx <- lm(Value ~ Date, data = xx)
fit <- data.frame(Date = as.Date(c("2022-04-01", "2030-01-01"), format = "%Y-%m-%d"))
x2 $Value <- predict(fit, newdata = x2)
x2# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000)) +
mp geom_line(lwd = 1, alpha = 0.7) +
stat_smooth(geom = "line", method = "lm", lwd = 1.5,
color = "darkred", alpha = 0.7) +
geom_line(data = x2, lwd = 1.5, lty = 2, alpha = 0.7) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Sinmple Predictions of Crude Oil Production in Canada",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_3_04.png", mp, width = 6, height = 4)
Consumption
# Prep data
<- c("Industrial consumption", "Residential consumption",
myMeasures "Commercial consumption")
<- dd_gas %>%
xx filter(Area == "Canada", Measurement %in% myMeasures) %>%
mutate(Measurement = factor(Measurement, levels = myMeasures))
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(size = 1, alpha = 0.7) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Canadian Natural Gas Consumption",
y = "Million Cubic Meters", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_3_05.png", mp, width = 6, height = 4)
Coal
Canada
# Prep data
<- dd_coal %>% filter(Area == "Canada") %>%
xx spread(Measurement, Value) %>%
gather(Measurement, Value, 6:7)
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(size = 1.5, alpha = 0.7) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
ylim(c(0, 6.5)) +
theme_agData(legend.position = "bottom") +
labs(title = "Canadaian Coal Oil Production, Export & Import",
y = "Million Tonnes", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_4_01.png", mp, width = 6, height = 4)
Provinces
# Prep data
<- dd_coal %>% filter(Area != "Canada") %>%
xx spread(Measurement, Value) %>%
gather(Measurement, Value, 6:7)
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, color = Measurement)) +
mp geom_line(alpha = 0.7, size = 1) +
facet_wrap(Area ~ ., scales = "free_y", ncol = 5) +
scale_color_manual(name = NULL, values = myColors2) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Coal Production, Export & Import",
y = "Million Tonnes", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_4_02.png", mp, width = 10, height = 4)
AB & SK
# Prep data
<- c("British Columbia", "Saskatchewan", "Alberta")
myAreas <- dd_coal %>%
xx filter(Measurement == "Production", Area %in% myAreas) %>%
mutate(Area = factor(Area, levels = myAreas)) %>%
group_by(Date, Area) %>%
summarise(Value = sum(Value, na.rm = T))
# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000, fill = Area)) +
mp geom_col(color = "black", lwd = 0.1, alpha = 0.7) +
scale_fill_manual(name = NULL, values = myColors2[3:1], breaks = rev(myAreas)) +
scale_x_date(date_breaks = "year", date_labels = "%Y") +
theme_agData(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Canadian Coal Production",
y = "Million Tonnes", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_4_03.png", mp, width = 6, height = 4)
Predictions
Using a simple linear model.
# Prep data
<- dd_coal %>% filter(Area == "Canada", Measurement == "Production")
xx <- lm(Value ~ Date, data = xx)
fit <- data.frame(Date = as.Date(c("2022-04-01", "2030-01-01"), format = "%Y-%m-%d"))
x2 $Value <- predict(fit, newdata = x2)
x2# Plot
<- ggplot(xx, aes(x = Date, y = Value / 1000000)) +
mp geom_line(lwd = 1, alpha = 0.5) +
stat_smooth(geom = "line", method = "lm", lwd = 1.5,
color = "darkred", alpha = 0.7) +
geom_line(data = x2, lwd = 1.5, lty = 2, alpha = 0.7) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Simple Predictions of Coal Production in Canada",
y = "Million Tonnes", x = NULL, caption = myCaption)
ggsave("canada_fossil_fuels_4_04.png", mp, width = 6, height = 4)