#load your packages
library(kableExtra)
library(readxl)
library(janitor)
library(tidyverse)
library(lubridate)
library(scales)
library(viridis)
library(cowplot)
library(zoo)
get_pipe_data <- function(pipe_name = "Trans-Mountain") {
#Checking if the open data site has these names
names <- c("Alliance", "Cochin", "Enbridge-Mainline", "Norman-Wells",
"Keystone", "MNP", "Trans-Mountain", "TQM", "tcpl-mainline", "Westcoast", "ngtl")
#start with a default of returning "no data"
pipe_data <- "No data"
#Check if the name we have sent is one of the specified pipeline names
if (pipe_name %in% names) {
file_name <- paste("https://www.cer-rec.gc.ca/open/energy/throughput-capacity/",
pipe_name, "-throughput-and-capacity.csv", sep = "")
local_file <- paste(pipe_name, ".csv", sep = "")
if (!file.exists(local_file)) {
download.file(file_name, local_file, mode = "wb")
}
pipe_data <- read_csv(local_file) %>%
clean_names() %>%
mutate(pipe_name = pipe_name)
}
#Returns Pipe data if the names we've sent is one the pipeline names and no data otherwise.
pipe_data
}
manpreet_graph<-function(){
theme_classic() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "gray",linetype="dotted"),
#Set colour of x-axis to black
axis.line.x = element_line(color = "black"),
#Set colour of y-axis to black
axis.line.y = element_line(color = "black"),
#Font size of axis = 12
axis.text = element_text(size = 12),
#adds extra space above x-axis
axis.text.x = element_text(margin = margin(t = 12)),
#Axis Title - size 12, centrally aligned
axis.title = element_text(size = 12, hjust = 0.5),
#subtitle - size 12, centrally aligned
plot.subtitle = element_text(size = 14,hjust=0.5),
#Caption - italic, size 12, left aligned
plot.caption = element_text(face="italic",size = 12,hjust=1),
#Legend keys wider
legend.key.width=unit(2,"line"),
#Legend position - bottom
legend.position = "bottom",
legend.text = element_text(size = 12),
plot.title = element_text(size = 16))
}
Downloading the data
#Downloading pipelines data using function
TransMountain_Data<- get_pipe_data("Trans-Mountain")
NGTL_Data<- get_pipe_data("ngtl")
#Downloading US imports data and Alberta Revenue data using read_csv
US_Imports_Data <- read_csv("US_Imports.csv") %>% clean_names()
AB_Res_Royalties <- read_csv("Alberta_Revenue.csv") %>% clean_names()
Deliverable 1
TransMountain_Data <- TransMountain_Data %>%
mutate(date = as.Date(date)) %>%
filter(key_point %in% c("Burnaby", "Sumas", "Westridge"))
#your code here
#graph
ggplot(TransMountain_Data) +
geom_area(aes(date, throughput_1000_m3_d, fill = product), colour="black",linewidth=0.5)+
facet_wrap(~key_point, ncol = 1, scales = "free_y") +
scale_fill_manual("",
values = c("domestic heavy" = "navy", "domestic light" = "slateblue", "refined petroleum products" = "grey50"),
) +
scale_y_continuous(expand = c(0, 0),
sec.axis = sec_axis(
transform = ~.*1/.16, name="Shipments (Monthly, Thousands of Barrels per Day)"))+
scale_x_date(date_break="1 year",date_labels = "%b\n%Y", expand=c(0,0))+
manpreet_graph() +
labs(
title = "Trans-Mountain Pipeline Shipments by Product and Destination",
caption = "Source: CER Data for Trans-Mountain, graph by Manpreet Singh.",
x = NULL,
y = "Shipments (Monthly, Thousands of Cubic Metres per Day)"
)

Deliverable 2
#your code here (I set it up to make a nice image for you)
NGTL_Data <- NGTL_Data %>%
filter(key_point %in% c("OSDA Liege", "OSDA Kirby", "Upstream of James River")) %>%
mutate(date = as.Date(date))%>%
filter(date >= as.Date("2010-11-29"))
ggplot(NGTL_Data) +
#For throughput line, and changing the unit to millions of cubic meter
geom_line(aes(date, throughput_1000_m3_d/1000, color = "Throughput"), linewidth = 0.8, alpha=0.9) +
#For capacity line, and changing the unit to millions of cubic meter
geom_line(aes(date, capacity_1000_m3_d/1000, color = "Available Capacity"), linewidth = 1.5, lty=24) +
facet_grid(rows=vars(key_point), scales = "free_y") +
scale_color_manual("", values = c("black", "red3")) +
scale_y_continuous(expand = c(0, 0), breaks = pretty_breaks())+
scale_x_date(date_break="1 year",date_labels = "%b\n%Y", expand=c(0,0))+
theme_classic() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(2, "lines"),
panel.grid.major.y = element_line(color = "gray"),
axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"),
axis.text = element_text(size = 12),
axis.text.x = element_text(margin = margin(t = 12)),
plot.caption = element_text(face="italic",size = 12,hjust=0),
legend.key.width=unit(2,"line"),
legend.position = "bottom",
legend.text = element_text(size = 13),
plot.title = element_text(size = 16),
strip.text.y = element_text(size = 14),
#to remove box around Facet names
strip.background = element_blank())+
labs(
title = "NGTL Throughput & Capacity (Montney and Oil Sands)",
y = "Throughput or Capacity (Millions of Cubic Metres per Day)",
caption = "Source: CER Data for NGTL, graph by Manpreet Singh."
)

Deliverable 3
US_Imports_Data <- US_Imports_Data %>%
mutate(destination_name=as_factor(destination_name))
ggplot(US_Imports_Data) +
geom_area(aes(period, quantity, fill = origin_name)) +
facet_grid(rows = vars(grade_name), cols = vars(destination_name), switch = "y") +
scale_fill_manual(values = c("Canada" = "red", "ROW" = "navy"), name = "") +
scale_y_continuous(expand = c(0, 0), breaks = pretty_breaks()) + # pretty_breaks from scales
scale_x_date(date_breaks = "5 years", date_labels = "%Y", expand = c(0,0)) +
manpreet_graph() +
labs(
title = "US Crude Imports from Canada and the Rest of the World (ROW)",
subtitle = "Imports by Refining PADD and Grade",
x = NULL,
y = "Imports (thousands of barrels per day)",
caption = "Data via US Energy Information Administration, current to December 2025. Graph by Manpreet Singh."
)

Deliverable 4
#your code here (I set it up to make a nice image for you)
AB_Res_Royalties <- AB_Res_Royalties%>%
filter(series %in% c("Natural Gas", "Conventional Oil", "Oil Sands", "Coal","Lease Sales and Other Revenue (Net)"))%>%
mutate(year = as.numeric(year))%>%
#Converting to billions
mutate(royalties = royalties / 1000)%>%
mutate(series = factor(series, levels = c("Natural Gas", "Conventional Oil", "Oil Sands", "Coal",
"Lease Sales and Other Revenue (Net)")))
ggplot(AB_Res_Royalties) +
geom_bar(aes(year, royalties, fill = series), stat = "identity") +
scale_fill_manual(values = c("Natural Gas" = "#17763a", "Conventional Oil" = "#f5d641",
"Oil Sands" = "#97aab6", "Coal" = "#345473","Lease Sales and Other Revenue (Net)" = "#525251"
),
name = NULL
) +
scale_y_continuous(expand = c(0, 0), breaks = seq(0, 25, by = 5)) +
scale_x_continuous(expand = c(0, 0), breaks = seq(1970, 2025, by = 5)) +
theme_classic() +
theme(axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"),
axis.text = element_text(size = 12),
panel.grid.major.y = element_line(color = "gray"),
axis.title = element_text(size = 14, hjust = 0.5),
plot.caption = element_text(face="italic",size = 12,hjust=0),
legend.key.width=unit(2,"line"),
legend.position = "bottom",
legend.text = element_text(size = 12),
plot.title = element_text(face = "bold",size = 18))+
labs(
title = "Alberta Royalty Revenue",
x = NULL,
y = "Royalty Revenue (CA$ Billions)",
caption = "Source: Government of Alberta data, graph by Manpreet Singh."
)
