-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_3.RMD
600 lines (514 loc) · 30.6 KB
/
st_3.RMD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
---
title: ST-3 Oil Production Data
author: Andrew Leach
date: <p>`r format(Sys.time(), '%B, %Y')`</p>
output:
html_document:
code_folding: hide
includes:
after_body:
theme: lumen
always_allow_html: yes
editor: source
---
The Alberta Energy Regulator (AER) provides monthly data for oil production in Alberta, including from the oil sands region in a report known as the ST-3. The data are available [here](https://www.aer.ca/providing-information/data-and-reports/statistical-reports/st3), and you can either download the data and analyze it yourself or use the embedded R code below to download all the data.
This page is also an easy introduction for downloading and manipulating data in R. If you're going to run the R code, you'll need a few basic set-up elements to get everything to work. I've included the code here for your reference.
```{r basics, cache=FALSE,warning=FALSE,message=FALSE}
#packages used
library(tidyverse) #basic set of data wrangling tools from the best part of the R universe
library(readxl) #it does what it says
library(scales) #makes graphs nicer
library(lubridate) #makes dates easier to handle
library(knitr) #using this to make the html document
library(prettydoc) #nice tables in the html doc
library(zoo) #time series data
library(viridis) #color-blind friendly palettes for graphs
library(patchwork) #allows you to combine plots
library(kableExtra) #nice tables in R Markdown
library(curl) #use this for checking internet connections
library(roll) #rolling means
library(ggsci)
#unit conversions
m3_bbl<-function(x) x*6.2898
bbl_m3<-function(x) x/6.2898
#create tableau palettes
colors_tableau10 <- function()
{
return(c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B",
"#E377C2", "#7F7F7F", "#BCBD22", "#17BECF"))
}
colors_tableau10_light <- function()
{
return(c("#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94",
"#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5"))
}
colors_tableau10_medium <- function()
{
return(c("#729ECE", "#FF9E4A", "#67BF5C", "#ED665D", "#AD8BC9", "#A8786E",
"#ED97CA", "#A2A2A2", "#CDCC5D", "#6DCCDA"))
}
#basic graph theme
weekly_small<-function(caption_align=1){
theme_minimal()+theme(
plot.margin = margin(.25, .75, .25, .75, "cm"),
legend.position = "bottom",
legend.margin=margin(c(0,0,0,0),unit="cm"),
legend.text = element_text(colour="black", size = 9),
plot.caption = element_text(size = 9, face = "italic",hjust=caption_align),
plot.title = element_text(size = 12,face = "bold"),
plot.subtitle = element_text(size = 11, face = "italic"),
panel.grid.minor = element_blank(),
text = element_text(size = 11,face = "bold"),
axis.title.x = element_text(size = 11,face = "bold", colour="black",margin = margin(t = 15, b = 0)),
axis.text = element_text(size = 11,face = "bold", colour="black",margin = margin(t = 10, b = 10)),
)
}
```
# Download the Data
Once you've got the preliminaries of the code, downloading the data is fairly easy. Each of the data files are stored by year, with the exception of the current year which has a different naming convention. The first step is to access the data (click on the code button to see how to do things in R if you're interested). The code also includes some fixes for names of projects which are not consistent in the data. This is basically a trial and error process to find broken data series.
```{r st3_data, cache=FALSE,warning=FALSE,message=FALSE}
#I'm going to make a function here so that I can specify whether I want to download all/none/new
st_3_online<-function(download="all"){
#every year is xlss file which makes it easy
#specify the rows we're going to want
#keep_rows<-c("Crude Oil Light", "Crude Oil Medium",
# "Crude Oil Heavy","Crude Oil Ultra Heavy","Total Conventional Oil Production","Crude Oil Ultra-Heavy",
# "Condensate Production","In Situ Production","Mined Production",
# "Non-Upgraded Total","Upgraded Production","Total Oil Sands Production",
# "Total Production","Nonupgraded Total")
#use a list to store each of the data sets we're going to download
data_store <- list()
years<-seq(2010,2016)
i<-1
filename<-paste("Oil_2010-2016.xlsx",sep="")
if(tolower(download)=="all")#if you chose to download the old data. correct case errors in case someone sends "ALL"
if(has_internet()) #if you're connected, go get the file
if(!file.exists(filename))
download.file(paste("https://www.aer.ca/documents/sts/st3/",filename,sep=""),filename,mode="wb")
#year<-2010 #testing
for(year in years){ #loop over 2010-2016, but the data are stored in different sheets
production_data <- read_excel(filename, sheet = as.character(year), skip = 4)
#the sheets are a bit of a mess, so we need to clean them up
names(production_data)[1]<-"product" #name column 1
# the %>% or pipeline is basically a "pass to" command
# start with production data, pass to select and remove cols 2 and 15, pass to filter and keep and rows with
# the names in the keep_rows object we created above
production_data<-production_data %>% select(-2,-15)%>% #%>% filter(product %in% keep_rows)
filter(!is.na(product),!is.na(Jan))%>%
mutate(across(-1, as.numeric))
#rename the column with the annual data s (grep is a search function that, in this case, is looking for
#whatever year we're processing and it will name the column annual
names(production_data)[grep(as.character(year),names(production_data))]<-"annual"
#assign the year to whatver year we're processing
production_data$year<-year
# create a long-form data set that will have product, annual level for that product, the year we're processing, and #the monthly data
production_data<-production_data %>% pivot_longer(cols=-c("product","annual","year"),
names_to="month",values_to = "production")
#store those data in a node in the list
data_store[[i]]<-production_data
#step your list counter one unit
i<-i+1
}
#let's get 2017-2020 data - same process, but they are in seperate files
#https://static.aer.ca/prd/documents/sts/st3/ST3_2021-12_Oil.xlsx
years<-seq(2017,2023)
#year<-2023
for(year in years){ #loop over 2017-2020
filename<-paste("Oil_",year,".xlsx",sep="") #use paste to put the year into the filename
if(year==2023)
filename<-"ST3_2023-12_Oil.xlsx"
if(year==2022)
filename<-"ST3_2022-12_Oil.xlsx"
if(year==2021)
filename<-"ST3_2021-12_Oil.xlsx"
if(tolower(download)=="all")#if you chose to download the old data. correct case errors in case someone sends "ALL"
if(has_internet()) #if you're connected, go get the file
if(!file.exists(filename))
download.file(paste("https://www.aer.ca/documents/sts/st3/",filename,sep=""),filename,mode="wb")
production_data <- read_excel(filename, sheet = "Data", skip = 4)
names(production_data)[1]<-"product"
production_data<-production_data %>% select(-2,-15) %>% filter(!is.na(product),!is.na(Jan))%>%
mutate(across(-1, as.numeric))
names(production_data)[grep(as.character(year),names(production_data))]<-"annual"
production_data$year<-year
production_data<-production_data %>% pivot_longer(cols=-c("product","annual","year"),
names_to="month",values_to = "production")
data_store[[i]]<-production_data
i<-i+1
}
#Most recent data are stored as "current"
year<-"Current"
filename<-paste("Oil_current.xlsx",sep="")
#check for interne
if(has_internet()) #if you're connected, go get the file
download.file(paste("https://www.aer.ca/documents/sts/st3/",filename,sep=""),filename,mode="wb")
production_data <- read_excel(filename, sheet = "Data", skip = 4)
year<-2024
names(production_data)[1]<-"product"
production_data<-production_data %>% select(-2,-15) %>% filter(!is.na(product),!is.na(Jan))%>%
mutate(across(-1, as.numeric))
names(production_data)[grep(as.character(year),names(production_data))]<-"annual"
production_data$year<-year
production_data<-production_data %>% pivot_longer(cols=-c("product","annual","year"),
names_to="month",values_to = "production")
production_data<-production_data %>% filter(production!=0)#keep only non-zero production
data_store[[i]]<-production_data
#now, stack all the elements stored in your list of data into a data frame
all_production<-do.call(rbind,data_store)
#now, we'll manipulate the data. mutate is adding a column based on a calculation
#so, all_production is all_prodcution passed to mutate, where the production variable is set to numeric
#and we format the dates using ymd since that's the format in the spreadsheet
# (remember, the lubridate package makes dates easy)
all_production<-all_production %>% mutate(production=as.numeric(production),
date=ymd(paste(year,as.character(month),1,sep="-")))
#next part is working with factors. Factors basically store data as a numeric code or level (1,2,3,4) and a
#set of labels attached to the levels
#take all_production,and pass to mutate, change the column product to a factor
all_production <-all_production %>% mutate(product=as_factor(product)) %>%
#and now we're going to re-code some of the labels so that we make them common #across the data we loaded
mutate(
product=fct_recode(product,
"In Situ Bitumen Production"="In Situ Production",
"Mined Bitumen Production"="Mined Production",
"Non-Upgraded Bitumen Production"="Non-Upgraded Total",
"Non-Upgraded Bitumen Production"="Nonupgraded Total",
"Synthetic Crude Production"="Upgraded Production",
"Conventional Oil Production"="Total Crude Oil Production",
"Conventional Oil Production"="Total Conventional Oil Production"
),
#and we're going to sort them so that mined bitumen is last
product=fct_relevel(product,"Mined Bitumen Production",after=5))
#last, we're going to collapse factors into on common level, so light and medium go into light, heavy and ultra-heavy
#are classified as heavy
all_production <-all_production %>% mutate(product=fct_collapse(product,
"Conventional Light Oil Production" = c("Crude Oil Light","Crude Oil Medium"),
"Conventional Heavy Oil Production"= c("Crude Oil Heavy","Crude Oil Ultra Heavy")
)) %>%
#now, because for some rows we'll now have 2 entries for production from conventional light oil, for example,
# in a month, we have to collapse them
#group your data by product, year, month, and date
group_by(product,year,month,date)%>%
#and combined the monthly and annual production from all prducts with the same label by summing them
summarize(production=sum(production),annual=sum(as.numeric(annual))) %>%
ungroup()
#last thing - we'll use zoo to create some time series descriptives
#add year and quarter
all_production <-all_production %>% mutate(year=year(date), quarter=quarter(date))%>%
#add rolling quarterly and annual data and lags
group_by(product)%>% arrange(date)%>% mutate(
roll_4m=roll_mean(production,4),
roll_12m=roll_sum(production,12),
growth_12m=(roll_12m-lag(roll_12m,12))/lag(roll_12m,12),
lag12_raw=(production-lag(production,12))/lag(production,12),
lag12_roll=(roll_4m-lag(roll_4m,12))/lag(roll_4m,12))
#and that's it - you've made a data set of Alberta oil production for the last decade
all_production # the last thing the function does is what it returns. We want it to return the data set
}
all_production<-st_3_online(download = "all") #call the function we just made to get the data
#create an annual data set using group_by and summarize
annual<-all_production %>%
mutate(days=days_in_month(date))%>%
group_by(year,product) %>% summarise(production=sum(production),
days=sum(days))%>%
arrange(year)%>% group_by(product)%>%
mutate(yoy_growth=round((production-lag(production,1))/lag(production,1)*100,2))
#create an annual data set using group_by and summarize
#same for a quarterly data_set
quarterly<-all_production %>%
mutate(days=days_in_month(date))%>%
group_by(year,quarter,product) %>% summarise(production=mean(production),days=sum(days))
quarterly <-quarterly %>%arrange(year,quarter)%>% group_by(product)%>%
mutate(q_lasty=round((production-lag(production,4))/lag(production,4)*100,2))
```
With the data downloaded and compiled into a single file, with annual and quarterly subfiles, we can graph some data.
```{r st3_graphs, cache=FALSE,warning=FALSE,message=FALSE}
graph_data<-c("Conventional Light Oil Production","Conventional Heavy Oil Production","Condensate Production",
"In Situ Bitumen Production","Mined Bitumen Production")
all_crude<-ggplot(filter(all_production,product%in%graph_data)%>%
mutate(product=fct_relevel(product,"Condensate Production")))+
geom_area(aes(date,m3_bbl(production)/days_in_month(date)/10^6,group=product,fill=product),color="black", size=0.5)+
scale_fill_manual("",values = pal_jco()(10),guide = "legend")+
#scale_fill_viridis("",discrete = T,option="F",direction = -1,end = .9)+
scale_x_date(date_breaks = "1 year",date_labels = "%Y",expand = c(0,0))+
scale_y_continuous(expand = c(0,0),breaks=pretty_breaks())+
#scale_colour_manual("",values=my_palette,guide = "legend")+
guides(fill=guide_legend(nrow=1))+
expand_limits(y=4.5)+
expand_limits(x=Sys.Date())+
theme(panel.border = element_blank(),
plot.margin=margin(t = 5, r = 15, b = 5, l = 5, unit = "pt"),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "gray",linetype="dotted"),
axis.line.x = element_line(color = "gray"),
axis.line.y = element_line(color = "gray"),
axis.text = element_text(size = 12,),
axis.text.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
axis.title = element_text(size = 12),
#axis.label.x = element_text(size=20,vjust=+5),
plot.subtitle = element_text(size = 12,hjust=0.5),
plot.caption = element_text(face="italic",size = 12,hjust=0),
legend.key.width=unit(2,"line"),
legend.position = "bottom",
#legend.direction = "horizontal",
#legend.box = "horizontal",
legend.text = element_text(size = 12),
plot.title = element_text(hjust=0.5,size = 14))+
labs(y="Oil and Bitumen Production (Millions of barrels per day)",x=NULL,
title="Alberta Conventional Oil and Bitumen Production",
#subtitle="For Operators with Production above 25k bbl/d",
caption=paste("Source: AER ST-3 data current to ",format.Date(max(all_production$date),format = "%B, %Y") ,", graph by @andrew_leach",sep=""))
#if you want to create a png in normal code, use this
#ggsave(all_crude,"oil_prod.png",width=12,height=6)
all_crude_weekly<-ggplot(filter(all_production,product%in%graph_data))+
geom_area(aes(date,6.2898*(production)/days_in_month(date)/10^6,group=product,fill=product))+
scale_fill_manual("",values = colors_tableau10(),guide = "legend")+
scale_x_date(date_breaks = "6 months",date_labels = "%b\n%Y",expand = c(0,0))+
scale_y_continuous(expand = c(0,0),breaks=pretty_breaks())+
#scale_colour_manual("",values=my_palette,guide = "legend")+
guides(fill=guide_legend(nrow=2))+
scale_fill_manual(NULL,values=colors_ua10())+
scale_x_date(date_breaks = "1 year", date_labels = "%b\n%Y",expand=c(0,0))+
scale_y_continuous(expand = c(0, 0)) +
guides(fill=guide_legend(nrow=2))+
labs(y="Oil and Bitumen Production (Millions of barrels per day)",x=NULL,
title="Alberta Conventional Oil and Bitumen Production",
#subtitle="For Operators with Production above 25k bbl/d",
caption=paste("Source: AER ST-3 data current to ",format.Date(max(all_production$date),format = "%B, %Y") ,", graph by @andrew_leach",sep=""))+
weekly_graphs()
#if you want to create a png in normal code, use this
save(all_crude_weekly,file="../weekly_charts/st3_prod.gph")
#ggsave(all_crude_weekly,file="../weekly_charts/st3_prod.gph")
oil_sands<-
ggplot(filter(all_production,product%in%graph_data,product!="Total Conventional Oil Production",product!="Conventional Heavy Oil Production",product!="Conventional Light Oil Production",product!="Condensate Production"))+
geom_area(aes(date,m3_bbl(production)/days_in_month(date)/10^6,group=product,fill=product),color="black",size=0.5)+
#scale_fill_viridis("",discrete = T,option="A",direction = -1,end = .9)+
scale_fill_manual("",values = pal_jco()(2),guide = "legend")+
scale_x_date(date_breaks = "1 year",date_labels = "%Y",,expand = c(0,0))+
scale_y_continuous(expand = c(0,0),breaks=pretty_breaks())+
expand_limits(y=4)+
#scale_colour_manual("",values=my_palette,guide = "legend")+
#guides(fill=FALSE,colour=FALSE)+
theme_classic() +
expand_limits(x=Sys.Date())+
guides(fill=guide_legend(nrow=1))+
theme(plot.margin=margin(t = 5, r = 15, b = 5, l = 5, unit = "pt"),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "gray",linetype="dotted"),
axis.line.x = element_line(color = "gray"),
axis.line.y = element_line(color = "gray"),
axis.text = element_text(size = 12,),
axis.text.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
axis.title = element_text(size = 12),
#axis.label.x = element_text(size=20,vjust=+5),
plot.subtitle = element_text(size = 12,hjust=0.5),
plot.caption = element_text(face="italic",size = 12,hjust=0),
legend.key.width=unit(2,"line"),
legend.position = "bottom",
#legend.direction = "horizontal",
#legend.box = "horizontal",
legend.text = element_text(size = 12),
plot.title = element_text(hjust=0.5,size = 14))+
labs(y="Oil Sands Bitumen Production (millions of barrels per day)",x=NULL,
title="Alberta Oil Sands Bitumen Production",
caption=paste("Source: AER ST-3 data current to ",format.Date(max(all_production$date),format = "%B %Y") ,", graph by @andrew_leach",sep=""),
NULL)
upgraded<-
all_production%>%filter(product%in%c("Non-Upgraded Bitumen Production","Conventional Heavy Oil Production","Conventional Light Oil Production","Synthetic Crude Production","Condensate Production"))%>%
mutate(product=factor(product,levels =c("Non-Upgraded Bitumen Production","Conventional Heavy Oil Production","Conventional Light Oil Production","Synthetic Crude Production","Condensate Production")),
product=fct_rev(product)
)%>%
ggplot()+
geom_area(aes(date,m3_bbl(production)/days_in_month(date)/10^6,group=product,fill=product),color="black",size=0.5)+
#scale_fill_viridis("",discrete = T,option="A",direction = -1,end = .9)+
scale_fill_manual("",values = pal_jco()(5),guide = "legend")+
scale_x_date(date_breaks = "1 year",date_labels = "%Y",,expand = c(0,0))+
scale_y_continuous(expand = c(0,0),breaks=pretty_breaks())+
expand_limits(y=4)+
#scale_colour_manual("",values=my_palette,guide = "legend")+
#guides(fill=FALSE,colour=FALSE)+
theme_classic() +
expand_limits(x=Sys.Date())+
guides(fill=guide_legend(nrow=1))+
theme(plot.margin=margin(t = 5, r = 15, b = 5, l = 5, unit = "pt"),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "gray",linetype="dotted"),
axis.line.x = element_line(color = "gray"),
axis.line.y = element_line(color = "gray"),
axis.text = element_text(size = 12,),
axis.text.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
axis.title = element_text(size = 12),
#axis.label.x = element_text(size=20,vjust=+5),
plot.subtitle = element_text(size = 12,hjust=0.5),
plot.caption = element_text(face="italic",size = 12,hjust=0),
legend.key.width=unit(2,"line"),
legend.position = "bottom",
#legend.direction = "horizontal",
#legend.box = "horizontal",
legend.text = element_text(size = 12),
plot.title = element_text(hjust=0.5,size = 14))+
labs(y="Oil Production (millions of barrels per day)",x=NULL,
title="Alberta Oil Production",
caption=paste("Source: AER ST-3 data current to ",format.Date(max(all_production$date),format = "%B %Y") ,", graph by @andrew_leach",sep=""),
NULL)
#if you want to create a png in normal code, use this
ggsave(plot=upgraded,filename = "st3_oil_sands_upg.png",width=15,height=8,dpi=300,bg="white")
oil_sands_plain<-ggplot(filter(all_production,product%in%graph_data,product!="Total Conventional Oil Production",product!="Conventional Heavy Oil Production",product!="Conventional Light Oil Production",product!="Condensate Production"))+
geom_area(aes(date,m3_bbl(production)/days_in_month(date)/10^6,group=product,fill=product),color="black",size=0.5)+
scale_fill_manual("",values = grey.colors(n=3,end=0.85,start = 0.3),guide = "legend")+
scale_x_date(date_breaks = "1 year",date_labels = "%b\n%Y",,expand = c(0,0))+
scale_y_continuous(expand = c(0,0),breaks=pretty_breaks())+
expand_limits(y=4)+
#scale_colour_manual("",values=my_palette,guide = "legend")+
#guides(fill=FALSE,colour=FALSE)+
theme_classic() +
expand_limits(x=Sys.Date(),y=4.4)+
guides(fill=guide_legend(nrow=1))+
theme(plot.margin=margin(t = 0, r = 15, b = 0, l = 0, unit = "pt"),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "gray",linetype="dotted"),
axis.line.x = element_line(color = "gray"),
axis.line.y = element_line(color = "gray"),
axis.text = element_text(size = 12,),
axis.text.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
axis.title = element_text(size = 12),
#axis.label.x = element_text(size=20,vjust=+5),
plot.subtitle = element_text(size = 12,hjust=0.5),
plot.caption = element_text(face="italic",size = 12,hjust=0),
legend.key.width=unit(2,"line"),
legend.position = "bottom",
#legend.direction = "horizontal",
#legend.box = "horizontal",
legend.text = element_text(size = 12),
plot.title = element_text(hjust=0.5,size = 14))+
labs(y="Oil Sands Bitumen Production (millions of barrels per day)",x=NULL,
#title="Alberta Oil Sands Bitumen Production",
#caption=paste("Source: AER ST-3 data current to ",format.Date(max(all_production$date),format = "%B %Y") ,", graph #by @andrew_leach",sep=""),
NULL)
#if you want to create a png in normal code, use this
ggsave(plot=oil_sands,filename = "st3_oil_sands_prod.png",width=12,height=6,dpi=300,bg="white")
```
### Oil sands
```{r oil_sands, fig.width=12, fig.height=7,dpi=300}
oil_sands
```
```{r oil_sands_plain, fig.width=14,fig.height=7,dpi=300}
oil_sands_plain
```
[Click here for high-resolution, colour image](st3_oil_sands_prod.png)
### All production
```{r all_crude, fig.width=14,fig.height=7,dpi=300}
all_crude
ggsave(plot=all_crude,filename = "st3_oil_prod.png",width=16,height=7,dpi=300,bg="white")
```
[Click here for high-resolution image](st3_oil_prod.png)
```{r oil_upg, fig.width=15, fig.height=8,dpi=300}
upgraded
```
[Click here for high-resolution image](st3_oil_sands_upg.png)
### Recent Production (mmbbl/d)
```{r, echo = T,eval=T}
all_production%>%filter(product%in%c("Conventional Light Oil Production","Conventional Heavy Oil Production","Mined Bitumen Production","In Situ Bitumen Production","Total Production"),date>=max(date)-years(1)) %>%
select(product,date,production)%>%
mutate(production=m3_bbl(production)/10^6/days_in_month(date),
product=gsub(" Production","",product))%>%
pivot_wider(names_from=product,values_from = production)%>%
mutate(date=format(date,"%b %Y")
)%>%rename(Date=date)%>%
kbl(escape = FALSE,table.attr = "style='width:100%;'") %>%
kable_styling(fixed_thead = T,bootstrap_options = c("hover", "condensed","responsive"),full_width = T)%>%
#scroll_box(width = "1000px", height = "600px")%>%
column_spec(1, extra_css = "white-space: nowrap;") %>%
I()
```
### Annual Production (mmbbl/d)
```{r, echo = T,eval=T}
annual%>%filter(product%in%c("Conventional Light Oil Production","Conventional Heavy Oil Production","Mined Bitumen Production","In Situ Bitumen Production","Total Production")) %>%
mutate(production=m3_bbl(production)/10^6/days,
product=gsub(" Production","",product))%>%select(-yoy_growth)%>%pivot_wider(names_from=product,values_from = production)%>%
rename(Year=year)%>%
select(-days)%>%
mutate(Year=ifelse(Year==last(Year),paste(Year,"(to date)"),Year))%>%
kbl(escape = FALSE,table.attr = "style='width:100%;'") %>%
kable_styling(fixed_thead = T,bootstrap_options = c("hover", "condensed","responsive"),full_width = T)%>%
#scroll_box(width = "1000px", height = "600px")%>%
column_spec(1, extra_css = "white-space: nowrap;") %>%
I()
```
```{r,eval=FALSE,echo=FALSE,include=FALSE}
#make an appi
base_year<-2018
all_production%>%
group_by(product)%>%
mutate(index_val=production/max(production*(date==ymd(paste(base_year,"-01-01",sep="-"))))*100)%>%
filter(grepl("Total",product),
year(date)>=2014)%>%
ggplot()+
geom_line(aes(date,index_val,group=product,colour=product),size=1.25)+
#geom_line(data=rspi_data,aes(ref_date,value,colour="RSPI (2013=100)"),size=1.5)+
scale_colour_manual("",values=colors_tableau10())+
geom_vline(aes(xintercept = ymd("2019-01-01")),lty="22",size=.85)+
annotate(geom = "text",x = ymd("2019-01-01"),y=30,label="Before carbon pricing",hjust=1.15,size=2.5)+
annotate(geom = "text",x = ymd("2019-01-01"),y=30,label="After carbon pricing",hjust=-0.15,size=2.5)+
annotate(geom = "text",x = ymd("2022-04-01"),y=30,label="April 1, 2022",hjust=-0.15,size=2.5)+
geom_vline(aes(xintercept = ymd("2022-04-01")),lty="22",size=.85)+
scale_fill_manual("",values=colors_tableau10())+
scale_x_date(date_breaks = "1 year",date_labels = "%Y",expand=c(0,0))+
theme_minimal()+
scale_y_continuous(breaks = pretty_breaks(n=8),expand=c(0,0))+
expand_limits(y=c(0,160))+
#guides(fill = guide_legend(nrow = 3))+
theme(
#legend.position = "none",
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
legend.margin=margin(c(0,0,0,0),unit="cm"),
axis.text.x = element_text(margin=margin(c(.3,.1,.1,.1),unit="cm")),
legend.text = element_text(colour="black", size = 8, face = "bold"),
plot.caption=element_text(hjust = 0,size=7),
plot.title=element_text(hjust = 0,size=18)
)+
labs(x="",y=paste("Index Value (",base_year,"=100)",sep=""),
title="APPI Before and After Carbon Pricing",
#subtitle="Canadian Refinery Production",
caption="Data via Government of Alberta. Calculations and graph by Andrew Leach")
ggsave("appi.png",width = 8,height = 4,bg="white",dpi=300)
```
```{r,fig.width=16,fig.height=8,dpi=300,eval=FALSE}
line_top<-4.2
label_val<-4.3
all_crude+
expand_limits(y=4.8)+
coord_cartesian(clip = 'off') + # This keeps the labels from disappearing
theme(plot.margin = unit(c(4,1,1,3), "cm"),
plot.title = element_text(
size = 16, # Adjust the font size of the title
margin = margin(t = 0, r = 0, b = 40, l = 0)),
panel.background = element_rect(fill = "white")
)+ # Add margins
annotate(geom = "text",x = ymd("2010-05-01"),y=label_val,label="SGER in place ($15/t)",hjust=0.05,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2010-01-01"), xend = ymd("2010-01-01"), y = 0, yend = line_top,linetype = "Alberta Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2016-01-01"),y=label_val,label="Alberta SGER Increased ($20/t)",hjust=0.05,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2016-01-01"), xend = ymd("2016-01-01"), y = 0, yend = line_top,linetype = "Alberta Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2017-01-01"),y=label_val,label="Alberta SGER Increased ($30/t)",hjust=0.05,,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2017-01-01"), xend = ymd("2017-01-01"), y = 0, yend = line_top,linetype = "Alberta Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2018-01-01"),y=label_val,label="Alberta adopts CCIR ($30/t)",hjust=0.05,,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2018-01-01"), xend = ymd("2018-01-01"), y = 0, yend = line_top,linetype = "Alberta Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2020-01-01"),y=label_val,label="Alberta adopts TIER ($30/t)",hjust=0.05,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2020-01-01"), xend = ymd("2020-01-01"), y = 0, yend = line_top,linetype = "Alberta Carbon Tax Changes"),size=.5)+
geom_segment(aes(x = ymd("2021-04-01"), xend = ymd("2021-04-01"), y = 0, yend = line_top,linetype = "Federal Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2021-04-01"),y=label_val,label="Federal Benchmark Increased ($40/t)",hjust=0.05,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2022-04-01"), xend = ymd("2022-04-01"), y = 0, yend = line_top,linetype = "Federal Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2022-04-01"),y=label_val,label="Federal Benchmark Increased ($50/t)",hjust=0.05,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2023-04-01"), xend = ymd("2023-04-01"), y = 0, yend = line_top,linetype = "Federal Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2023-04-01"),y=label_val,label="Federal Benchmark Increased ($65/t)",hjust=0.05,size=2.5,angle=45)+
geom_segment(aes(x = ymd("2024-04-01"), xend = ymd("2024-04-01"), y = 0, yend = line_top,linetype = "Federal Carbon Tax Changes"),size=.5)+
annotate(geom = "text",x = ymd("2024-04-01"),y=label_val,label="Federal Benchmark Increased ($80/t)",hjust=0.05,angle=45,size=2.5,angle=45)+
scale_linetype_manual("",values=c("22","22"),guide="none")
```