-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCOVID_script.R
2350 lines (2063 loc) · 120 KB
/
COVID_script.R
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' This code holds plotting and analysis scripts for Kikstra et al. (2021), Nat. Energy, "Climate mitigation scenarios with persistent COVID-19 related energy demand changes"
#' Author: Jarmo S. Kikstra
#'
#' Instructions:
#' - to reproduce, please run parts 0 to 2 first, and then select and run the specific figure you would like to create
#'
#' Index:
#' 0. Loading packages.
#' 1. Locating and reading all data used for analysis.
#' 2. Define auxiliary functions.
#' 3. Main text figures.
#' 4. Supplementary figures.
#'
#'
# Part 0: loading packages ====
# uncomment lines below in case the packages are not yet installed.
# pkg.list <- c("vroom",
# "readxl",
# "ggsci",
# "hrbrthemes",
# "ggrepel",
# "RColorBrewer",
# "ggpubr",
# "gridExtra",
# "colorspace",
# "patchwork",
# "tidyverse",
# "plotly",
# "maps",
# "rworldmap",
# "mapproj",
# "rgdal",
# "here",
# "tidyverse")
# install.packages(pkg.list)
# load packages
library(vroom) # load CSV files, fast, as tibble.
library(readxl) # load Excel files
library(ggsci) # scientific colour schemes
library(hrbrthemes) # more themes
library(ggrepel) # for non-overlapping labels
library(RColorBrewer) # for colours
library(ggpubr) # for ggarrange
library(gridExtra) # for ggarrange
library(colorspace) # hsv colorspace manipulations
library(patchwork) # for arrangement with much easier syntax than ggarrange
library(plotly) # for interactive plots
library(maps) # for maps, only supplementary figure
library(rworldmap) # for maps, only supplementary figure
library(mapproj) # for maps, only supplementary figure
library(rgdal) # for maps, only supplementary figure
library(here) # for easily specifying relative paths
library(tidyverse) # for all data manipulation, plotting, etc.
# Part 1: locating and reading data (COVID scenarios, GDP sensitivities, SR15 data, and some auxiliary files) ====
# Part 1.0: Set some global parameters used throughout ====
year.start <- 2015 # start year for timeseries figure
year.end <- 2035 # start year for timeseries figure
# Part 1.1: Setting paths ====
# # set working directory to this file
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))
# base path
here::i_am("README.md")
base.path <- paste0(here(), "/")
out.path <- paste0(base.path, "figures/")
# Part 1.1.1: Setting paths: inputs ====
# main data file
data.file <- "covid-scenarios-data.xlsx"
# sheets
# main scenario data from this study, output of MESSAGEix-GLOBIOM
f.covid <- "scenario_data"
# Collated activity assumptions data
f.activity <- "2025vs2019"
# Extra data with estimated upstream CO2 emissions of demand end-use sectors
f.upstream <- "upstream_data"
# Colour settings by scenario, for plotting
f.colormarkers <- "color_markers"
# Regional definitions for MESSAGEix
f.MESSAGEregion <- "iso_MESSAGEix"
# Aggregate structure and activity changes related to energy demand changes
f.aggregate.energy.demand <- "demand_change"
# SR15 database data files
# SR15 data on a global level, with climate categories
f.sr15 <- paste0(base.path,"sr15.csv")
# SR15 data on a five-regional level, wide format
f.sr15.r5 <- paste0(base.path,"iamc15_scenario_data_all_regions_r2.0.xlsx")
# Part 1.2: Load COVID scenario base data ====
df_full <- read_excel(paste0(base.path,data.file), sheet=f.covid)
df <- pivot_longer(df_full,`2000`:`2100`, names_to = "year") %>% na.omit()
df.cov <- df %>% rename(model=Model, scenario=Scenario, unit=Unit, region=Region, variable=Variable) %>% select(-unit)
# assign colours and categories
color_markers = read_excel(paste0(base.path,data.file), sheet=f.colormarkers)
# Function for desaturating colors by specified proportion
desat <- function(cols, sat=0.5) {
X <- diag(c(1, sat, 1)) %*% rgb2hsv(col2rgb(cols))
hsv(X[1,], X[2,], X[3,])
}
# adapt saturation differences for colorblind-safeness
color_markers_n <- color_markers %>%
mutate(color=ifelse(scenario=="baseline",desat(color,1),color)) %>%
mutate(color=ifelse(scenario=="restore",desat(color,1),color)) %>%
mutate(color=ifelse(scenario=="self reliance",desat(color,0.6),color)) %>%
mutate(color=ifelse(scenario=="smart use",desat(color,0.3),color)) %>%
mutate(color=ifelse(scenario=="green push",desat(color,1),color))
# apply to data
df.cov <- df.cov %>% mutate(category=ifelse(
grepl("1000", scenario, fixed = FALSE),"Below 2.0C",
ifelse(grepl("550", scenario, fixed = FALSE)|grepl("(Same climate policy 1.5)", scenario, fixed = FALSE),"1.5C no or low OS",
"Above 2.0C")
)) %>% mutate(color = ifelse(
grepl("Baseline-no-COVID", scenario, fixed = FALSE),(color_markers %>% filter(scenario=="baseline"))$color,
ifelse(
grepl("Smart Use", scenario, fixed = FALSE),(color_markers %>% filter(scenario=="smart use"))$color,
ifelse(
grepl("Green Push", scenario, fixed = FALSE),(color_markers %>% filter(scenario=="green push"))$color,
ifelse(
grepl("Self-Reliance", scenario, fixed = FALSE),(color_markers %>% filter(scenario=="self reliance"))$color,
ifelse(
grepl("Restore", scenario, fixed = FALSE),(color_markers %>% filter(scenario=="restore"))$color, "black"
))))))
# separate colormarkers for handling of colours outside of dataframe
color_markers_n <- color_markers_n %>%
mutate_if(is.character, str_replace_all, pattern="baseline", replacement="Baseline-no-COVID") %>%
mutate_if(is.character, str_replace_all, pattern="smart use", replacement="Smart Use") %>%
mutate_if(is.character, str_replace_all, pattern="green push", replacement="Green Push") %>%
mutate_if(is.character, str_replace_all, pattern="self reliance", replacement="Self-Reliance") %>%
mutate_if(is.character, str_replace_all, pattern="restore", replacement="Restore")
color_markers_n.550 <- color_markers_n %>% mutate(scenario=paste0(scenario," (550)"))
color_markers_n.samepolicy <- color_markers_n %>% mutate(scenario=paste0(scenario," (Same climate policy 1.5)"))
color_markers_n.1000 <- color_markers_n %>% mutate(scenario=paste0(scenario," (1000)"))
color_markers_n <- color_markers_n %>%
bind_rows(color_markers_n.550) %>%
bind_rows(color_markers_n.samepolicy) %>%
bind_rows(color_markers_n.1000)
mark_cols = color_markers_n$color
# set names, for use in ggplot manual colors
names(mark_cols) = color_markers_n$scenario
# split out GDP sensitivity runs
df.gdp <- df.cov %>% filter(grepl("sens_med.SHK_R", scenario, fixed = FALSE)) # select GDP sensitivity scenarios
df.cov <- df.cov %>% filter(!grepl("sens_med.SHK_R", scenario, fixed = FALSE)) # deselect GDP sensitivity scenarios
# create smaller dataframes with only the World region
df.cov.w <- df.cov %>% filter(region=="World") %>% mutate(year = as.numeric(year)) # just world, to save processing time where possible
gdp <- df.gdp %>% filter(region=="World")
# Part 1.2.1: Load GDP-specific data ====
gdp.var <- "GDP|MER" # alternatively, "GDP|PPP" could be used
gdp <- df.gdp %>% filter(region=="World")
# markers
gdp.marker <- df.cov.w %>% filter(variable==gdp.var) %>% filter(category=="Above 2.0C") %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
mutate(min=min(value), q10=quantile(value,0.1), q25=quantile(value,0.25), med=median(value), q75=quantile(value,0.75), q90=quantile(value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
gdp.marker.end <- gdp.marker %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
gdp.marker <- gdp.marker %>% left_join(gdp.marker.end, by=c("model", "scenario"))
gdp.marker <- gdp.marker %>% filter(scenario=="Green Push") # can be any of the covid scenarios
# separate no-covid scenario
gdp.no.covid <- df.cov.w %>% filter(variable==gdp.var) %>% filter(category=="Above 2.0C") %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
mutate(min=min(value), q10=quantile(value,0.1), q25=quantile(value,0.25), med=median(value), q75=quantile(value,0.75), q90=quantile(value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
gdp.no.covid.end <- gdp.no.covid %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
gdp.no.covid <- gdp.no.covid %>% left_join(gdp.no.covid.end, by=c("model", "scenario"))
gdp.no.covid <- gdp.no.covid %>% filter(scenario=="Baseline-no-COVID") # can be any of the covid scenarios
# set 2020 value
gdp.2020 <- gdp %>% filter(year==2020, variable==gdp.var) %>% mutate(value=
(gdp.marker%>% filter(year==2020, variable==gdp.var))$value)
# add historical years (before reporting fix)
gdp.2015.val <- 57709*1.10774 # from summarized_data excel file, multiplied by scalar to go from 2005 to 2010 usd
gdp.2019.val <- 66912*1.10774 # from summarized_data excel file, multiplied by scalar to go from 2005 to 2010 usd
gdp.2019 <- gdp.2020 %>% mutate(value=gdp.2019.val, year=2019)
gdp.2015 <- gdp.2020 %>% mutate(value=gdp.2015.val, year=2015)
# combine
gdp <- gdp %>% filter(year!=2020) %>% bind_rows(gdp.2020)
gdp.gdp <- gdp %>% filter(variable==gdp.var) %>% mutate(year=as.numeric(year)) %>% group_by(year) %>% arrange(scenario,year) %>%
mutate(min=min(value), q10=quantile(value,0.1), q25=quantile(value,0.25), med=median(value), q75=quantile(value,0.75), q90=quantile(value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
gdp.gdp.end <- gdp.gdp %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
gdp.gdp <- gdp.gdp %>% left_join(gdp.gdp.end, by=c("model", "scenario"))
# make a df for cumulative co2 emissions (consider moving these lines to below)
# covid scens:
df.co2 <- df.cov %>% filter(variable=="Emissions|CO2") %>% filter(region=="World") %>% rename(co2=value)
df.co2.c <- df.co2 %>% filter(!grepl('0',scenario,fixed=FALSE)) %>% select(scenario, year, co2) %>% mutate(year=as.numeric(year))
co22019 <- (df.co2 %>% filter(year==2019,scenario=="Baseline-no-COVID"))$co2
co22020 <- (df.co2 %>% filter(year==2020,scenario=="Restore"))$co2
gdp <- df.gdp %>% filter(region=="World")
# gdp:
gdp.v <- gdp %>% filter(variable=="Emissions|CO2") %>% mutate(year=as.numeric(year)) %>% group_by(year) %>% pivot_wider(scenario,year) %>%
mutate(`2019`=co22019, `2020`=co22020) %>%
pivot_longer(`2000`:`2100`, names_to="year") %>%
arrange(scenario,year) %>% group_by(year) %>%
mutate(min=min(value), q10=quantile(value,0.1), q25=quantile(value,0.25), med=median(value), q75=quantile(value,0.75), q90=quantile(value,0.9), max=max(value))
df.co2 <- df.cov %>% filter(variable=="Emissions|CO2") %>% filter(region=="World") %>% rename(co2=value) %>% select(c(scenario,year,co2)) %>%
bind_rows(gdp.v %>% select(scenario,year,value) %>% rename(co2=value)) %>%
arrange(year) %>% arrange(scenario) %>% mutate(year=as.numeric(year))
y2 <- unique(df.co2$year)[c(2:length(unique(df.co2$year)))]
y1 <- c(2000,y2[y2 != 2100])
# duration of each time period (will be used later also to calculate cumulative results)
duration <- data.frame(year = y2,
dur = y2-y1)
# Part 1.3: load SR15 data ====
# sr15 data (world only - just for categories now...)
df.15 <- vroom(f.sr15) %>% select(-c(colnames(.)[1])) %>% select(-c(exclude, baseline))
# sr15 data (world and r5 - no categories)
df.15.r5 <- read_excel(f.sr15.r5, sheet="data")
years<-c("2010", "2015", "2020", "2025", "2030", "2035", "2040", "2045", "2050")
df.15.r5 <- df.15.r5 %>% select(Model,Scenario,Region,Variable,Unit,years) %>%
pivot_longer(years,names_to="year",values_to="value") %>%
rename(model=Model, scenario=Scenario, variable=Variable, region=Region, unit=Unit) %>%
mutate(year=as.numeric(year))
df.15.r5 <- df.15.r5 %>%
left_join(
df.15 %>% select(model,scenario,category) %>% distinct()
)
df.15 <- df.15.r5
# redo SR15 categories.
df.15 <- df.15 %>% mutate(category = ifelse(category=="Below 1.5C"|category=="1.5C low overshoot", "1.5C no or low OS",
ifelse(category=="1.5C high overshoot"|category=="Lower 2C"|category=="Higher 2C", "Below 2.0C", category))) %>%
mutate(color = ifelse(
grepl("1.5C no or low OS", category, fixed = TRUE),"#1e9583",
ifelse(
grepl("Below 2.0C", scenario, fixed = TRUE),"#63bce4",
"#e78731")))
# Part 1.4: Loading activity data ====
df.act <- read_excel(paste0(base.path, data.file), sheet="2025v2019") %>% rename(`Smart Use`=GL, `Self-Reliance`=SR, `Green Push`=GP) %>%
pivot_longer(`Smart Use`:`Green Push`, values_to="Relative Change 2019-2025", names_to="Scenario")
# Part 1.5: Load upstream co2 emissions data ====
yr.upstream <- 2030 # select focus year for later visualizations
co2.upstream <- read_excel(paste0(base.path, data.file), sheet="upstream_data") %>%
rename(model=Model, scenario=Scenario, unit=Unit, region=Region, variable=Variable) %>%
select(-unit) %>%
pivot_longer(`2010`:`2100`, names_to="year", values_to="value") %>% mutate(year=as.numeric(year)) %>%
mutate(scenario=ifelse(scenario=='baseline_y_macro_clone','Baseline-no-COVID',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='green_learn_marker_mpa_adjusted_macro','Smart Use',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='green_push_marker_mpa_adjusted_macro','Green Push',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='NRC_GDP_marker_mpa_adjusted_macro','Restore',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='self_reliance_marker_mpa_adjusted_macro','Self-Reliance',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='green_push_550fp','Green Push (Same climate policy 1.5)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='NRC_GDP_550fp','Restore (Same climate policy 1.5)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_baseline2_550','Baseline-no-COVID (550)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_green_learn_550','Smart Use (550)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_green_push_550','Green Push (550)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_NRC_GDP_550','Restore (550)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_self_reliance_550','Self-Reliance (550)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_baseline2_1000','Baseline-no-COVID (1000)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_green_learn_1000','Smart Use (1000)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_green_push_1000','Green Push (1000)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_NRC_GDP_1000','Restore (1000)',scenario)) %>% #rename scenarios
mutate(scenario=ifelse(scenario=='EN_self_reliance_1000','Self-Reliance (1000)',scenario)) #rename scenarios
df.cov.demand.co2 <- df.cov.w %>% filter(variable%in%c("Emissions|CO2|Energy|Demand|Residential and Commercial", "Emissions|CO2|Energy|Demand|Transportation", "Emissions|CO2|Energy|Demand|Industry"))
df.co2.demand.only <- df.cov.demand.co2 %>%
mutate(sector=NA) %>%
mutate(sector=ifelse(grepl(pattern='Transport',x=variable,fixed=T),'Transport',sector)) %>%
mutate(sector=ifelse(grepl(pattern='Residential',x=variable,fixed=T),'Buildings',sector)) %>%
mutate(sector=ifelse(grepl(pattern='Industr',x=variable,fixed=T),'Industry',sector)) %>%
mutate(type="End-use") %>%
group_by(year,scenario,sector,region,type) %>% summarise(value=sum(value)) %>%
filter(!grepl('sens',scenario,fixed=T)) %>%
mutate(category=ifelse(
grepl("1000", scenario, fixed = FALSE),"Below 2.0C",
ifelse(grepl("550", scenario, fixed = FALSE)|grepl("(Same climate policy 1.5)", scenario, fixed = FALSE),
"1.5C",
"Above 2.0C")
))
df.co2.sector.incl.upstream <- df.cov.demand.co2 %>%
bind_rows(co2.upstream) %>%
mutate(sector=NA) %>%
mutate(sector=ifelse(grepl(pattern='Transport',x=variable,fixed=T),'Transport',sector)) %>%
mutate(sector=ifelse(grepl(pattern='Residential',x=variable,fixed=T),'Buildings',sector)) %>%
mutate(sector=ifelse(grepl(pattern='Industr',x=variable,fixed=T),'Industry',sector)) %>%
mutate(type="Total") %>%
bind_rows(
df.cov.w %>% filter(variable=="Emissions|CO2") %>%
mutate(sector="All CO2") %>%
mutate(type="Total")
) %>%
group_by(year,scenario,sector,region,type) %>% summarise(value=sum(value)) %>%
filter(!grepl('sens',scenario,fixed=T)) %>%
mutate(category=ifelse(
grepl("1000", scenario, fixed = FALSE),"Below 2.0C",
ifelse(grepl("550", scenario, fixed = FALSE)|grepl("(Same climate policy 1.5)", scenario, fixed = FALSE),
"1.5C",
"Above 2.0C")
))
df.upstream <- bind_rows(
df.co2.demand.only,
df.co2.sector.incl.upstream
) %>%
filter(category%in%c('1.5C','Below 2.0C','Above 2.0C'))
df.upstream$cat.ordered = factor(df.upstream$category, levels=c('1.5C','Below 2.0C','Above 2.0C'))
# wedges
df.upstream.wedge <- df.upstream %>%
filter(type=="Total") %>%
filter(scenario%in%c('Green Push','Restore')) %>%
pivot_wider(names_from=scenario, values_from=value)
df.upstream.wedge.diff <- df.upstream.wedge %>%
mutate(value=`Restore`-`Green Push`) %>%
select(year,region,sector,value) %>%
pivot_wider(names_from = sector, values_from=value)
df.wedge <- df.upstream.wedge.diff %>%
left_join(df.upstream.wedge %>% filter(sector=="All CO2", type=="Total"))
df.wedge.sectorcomparison <- df.wedge %>% filter(year%in%c(2025,2030)) %>% select(year,Buildings,Industry,Transport) %>%
mutate(Buildings=Buildings/Transport, Industry=Industry/Transport, Transport=Transport/Transport)
df.upstream.wedge.15 <- df.upstream %>%
filter(type=="Total") %>%
filter(scenario%in%c('Green Push (Same climate policy 1.5)','Restore (Same climate policy 1.5)')) %>%
pivot_wider(names_from=scenario, values_from=value)
df.upstream.wedge.diff.15 <- df.upstream.wedge.15 %>%
mutate(value=`Restore (Same climate policy 1.5)`-`Green Push (Same climate policy 1.5)`) %>%
select(year,region,sector,value) %>%
pivot_wider(names_from = sector, values_from=value)
df.wedge.15 <- df.upstream.wedge.diff.15 %>%
left_join(df.upstream.wedge.15 %>% filter(sector=="All CO2", type=="Total"))
df.wedge.15.sectorcomparison <- df.wedge.15 %>% filter(year%in%c(2025,2030)) %>% select(year,Buildings,Industry,Transport) %>%
mutate(Buildings=Buildings/Transport, Industry=Industry/Transport, Transport=Transport/Transport)
# Part 1.6: Loading demand change data ====
act.change.df = read_excel(paste0(base.path, data.file), sheet="demand_change") %>%
gather(key = 'scenario', value = 'value',2:5) %>%
mutate(sector = if_else(sector == 'Industrial activity', 'Industry', sector))
# Part 1.7: MESSAGE and SR15 5-regional aggregation data preparation ====
variables <- c("Emissions|CO2",
"Final Energy",
"Final Energy|Residential and Commercial",
"Final Energy|Transportation",
"Final Energy|Industry"
)
plot.category <- "Above 2.0C" # select the category of covid scenarios to show, alternative e.g. "1.5C no or low OS"
plot.category.sr15 <- "1.5C no or low OS" # compare with this category from SR15 pathways
# Part 1.7.1: MESSAGEix 5 regional aggregation ====
oecd <- c("NAM", "WEU", "PAO")
afr <- c("AFR", "MEA")
lam <- c("LAM")
asia <- c("SAS", "PAS", "CPA")
ref <- c("EEU", "FSU")
regs <- c(
"R5OECD",
"R5MAF",
"R5LAM",
"R5ASIA",
"R5REF"
)
# note: unit is dropped here
r5.sum.cov <- df.cov %>% filter(variable%in%variables, region%in%c(oecd,afr,lam,asia,ref)) %>%
mutate(r5=case_when(
region %in% oecd ~ "R5OECD",
region %in% afr ~ "R5MAF",
region %in% lam ~ "R5LAM",
region %in% asia ~ "R5ASIA",
region %in% ref ~ "R5REF"
)) %>%
drop_na(r5) %>%
group_by(model,scenario,variable,year,category,color,r5) %>%
summarise(value=sum(value)) %>%
rename(region=r5)
r5.sum.gdp <- df.gdp %>% filter(variable%in%variables, region%in%c(oecd,afr,lam,asia,ref)) %>%
mutate(r5=case_when(
region %in% oecd ~ "R5OECD",
region %in% afr ~ "R5MAF",
region %in% lam ~ "R5LAM",
region %in% asia ~ "R5ASIA",
region %in% ref ~ "R5REF"
)) %>%
drop_na(r5) %>%
group_by(model,scenario,variable,year,category,r5) %>%
summarise(value=sum(value)) %>%
rename(region=r5)
df.cov.vars <- df.cov %>% filter(variable%in%variables) %>%
bind_rows(r5.sum.cov)
df.gdp.vars <- df.gdp %>% filter(variable%in%variables) %>%
bind_rows(r5.sum.gdp)
df.15.vars <- df.15 %>% filter(variable%in%variables) %>%
mutate(region=ifelse(region=="R5OECD90+EU","R5OECD",region)) %>%
filter(region!="R5ROWO")
# select the required variable from the covid scenarios
df.cov.selected.regional <- df.cov.vars %>% filter(category==plot.category) %>% mutate(year=as.numeric(year)) %>% group_by(year, category, region, variable) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
df.cov.selected.end <- df.cov.selected.regional %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
df.cov.selected.regional <- df.cov.selected.regional %>% left_join(df.cov.selected.end, by=c("model", "scenario"))
df.cov.selected.regional <- df.cov.selected.regional %>% filter(region%in%regs)
# get GDP uncertainty range for specific variable
gdp.regional <- df.gdp.vars %>% mutate(year=as.numeric(year)) %>% filter(category==plot.category) %>% group_by(year, category, region, variable) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
gdp.w.end <- gdp.regional %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
gdp.regional <- gdp.regional %>% left_join(gdp.w.end, by=c("model", "scenario"))
gdp.regional <- gdp.regional %>% filter(region%in%regs)
# Part 1.7.2: SR15 ====
# get selected sr15 ranges for the required variable
df.regional.sr15 <- df.15.vars %>% filter(category==plot.category.sr15) %>% group_by(year, category, region, variable) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
ungroup() %>%
distinct(year, category, q10,med,q90, .keep_all = TRUE) %>% mutate(scenario=plot.category.sr15, model="SR15") %>%
filter(year>=year.start & year<=year.end)
df.regional.end.sr15 <- df.regional.sr15 %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
df.regional.sr15 <- df.regional.sr15 %>% left_join(df.regional.end.sr15)
df.regional.sr15 <- df.regional.sr15 %>% filter(region%in%regs)
# Part 2: Define auxiliary functions ====
# Part 2.1: function for main text figure 2 ====
do_main_timeseries_plot <- function(df.cov.selected, var, varname, varlims, year.end=2035,
df.15=df.15.selected, cat.sr15="1.5C no or low OS", cat.covid="Above 2.0C",
downsize=1, range=5,
df.gdp.sel=df.gdp.selected,
y.unit = "GtCO2/yr",
plotlabels=TRUE, plotranges=TRUE, justpathways=FALSE, plotgdpsensitivity=TRUE){
# get selected sr15 ranges for the required variable
df.v.sr15 <- df.15.selected %>% filter(variable==var) %>% filter(category==cat.sr15) %>% group_by(year, category) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
ungroup() %>%
distinct(year, category, q10,med,q90, .keep_all = TRUE) %>% mutate(scenario=cat.sr15, model="SR15") %>%
filter(year>=year.start & year<=year.end)
df.v.end.sr15 <- df.v.sr15 %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
df.v.sr15 <- df.v.sr15 %>% left_join(df.v.end.sr15)
print(df.v.sr15)
# get GDP uncertainty range for specific variable
gdp.v <- df.gdp.sel %>% filter(variable==var) %>% mutate(year=as.numeric(year)) %>% group_by(year) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
gdp.w.end <- gdp.v %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
gdp.v <- gdp.v %>% left_join(gdp.w.end, by=c("model", "scenario"))
print("min and max emissions in 2020 for full gdp range")
print(gdp.v %>% filter(year==2025) %>% select(scenario, min, max))
# select the required variable from the covid scenarios
df.cov.selected.v <- df.cov.selected %>% filter(variable==var) %>% filter(category%in%cat.covid) %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
df.cov.selected.end <- df.cov.selected.v %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
df.cov.selected.v <- df.cov.selected.v %>% left_join(df.cov.selected.end, by=c("model", "scenario"))
# do main plot
geom.text.size <- 4/downsize
theme.size <- (14/5) * geom.text.size
formatter1000 <- function(){
function(x)x/1000
}
formatter.standard <- function(){
function(x)x
}
if (justpathways==TRUE){
p.v <- ggplot(df.cov.selected %>% filter(variable==var) %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
drop_na(value) %>%
mutate(min=min(na.rm=T, value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(na.rm=T,value)) %>%
filter(year>=year.start & year<=year.end) ,
aes(x=year, group=scenario)) +
# add 0 and net-zero
geom_rect(aes(xmin=year.start, xmax=year.end, ymin=-50000, ymax=0), fill="#ffebcd", alpha=0.08) +
# add gdp uncertainty range
geom_ribbon(data=gdp.v %>% filter(year>=2021), aes(ymin=min, ymax=max, group=model), fill=ifelse(plotgdpsensitivity==TRUE,'black',NULL), alpha=0.1) +
# add covid scenarios
geom_line(data= df.cov.selected.v,aes(y=value, colour=scenario), linetype="solid", size=1/downsize) +
geom_point(data= df.cov.selected.v %>% filter(year!=2019, year!=2021,year!=2022,year!=2023,year!=2024),
aes(y=value, colour=scenario), shape=21, size=3/downsize, fill="white", stroke = 1/downsize) +
coord_cartesian(xlim=c(year.start, year.end+2),
ylim=varlims) +
ggtitle(varname) +
theme_classic() +
scale_color_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
scale_fill_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
theme(legend.position="none",
title=element_text(size=theme.size),
text = element_text(size=theme.size)
) +
xlab(NULL) +
ylab(y.unit) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0),
labels = ifelse(y.unit=="GtCO2/yr",formatter1000(),formatter.standard()))
if (plotlabels){
p.v <- p.v +
coord_cartesian(xlim=c(year.start,year.end+5),ylim=varlims) +
# add labels to scenarios
geom_text_repel(data=df.cov.selected.v %>% filter(year==year.end),
size=geom.text.size/(downsize+0.5),
xlim=c(year.end+1,year.end+5),
aes(x=year.end,
y=value,
group=scenario, label=scenario, colour=scenario), direction="y", hjust = 0, force=5)
return(p.v)
}
}
if (range=="minmax"){
p.v <- ggplot(df.cov.selected %>% filter(variable==var) %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
drop_na(value) %>%
mutate(min=min(na.rm=T, value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(na.rm=T,value)) %>%
filter(year>=year.start & year<=year.end) ,
aes(x=year, group=scenario)) +
# add 0 and net-zero
geom_rect(aes(xmin=year.start, xmax=year.end, ymin=-50000, ymax=0), fill="#ffebcd", alpha=0.08) +
# add sr 15 timeseries category
geom_ribbon(data=df.v.sr15, aes(ymin=min, ymax=max, fill=category), alpha=0.1) +
geom_ribbon(data=df.v.sr15, aes(ymin=q25, ymax=q75, fill=category), alpha=0.3) +
geom_line(data=df.v.sr15, aes(y=min, colour=category), size=0.1) +
geom_line(data=df.v.sr15, aes(y=max, colour=category), size=0.1) +
geom_line(data=df.v.sr15, aes(y=med, colour=category), linetype="dashed", size=1.5/downsize) +
geom_point(data=df.v.sr15, aes(y=med, colour=category), shape=21, size=3/downsize, fill="white", stroke = 1/downsize) +
# add gdp uncertainty range
geom_ribbon(data=gdp.v %>% filter(year>=2021), aes(ymin=min, ymax=max, group=model), fill='black', alpha=0.1) +
# add covid scenarios
geom_line(data= df.cov.selected.v,aes(y=value, colour=scenario), linetype="solid", size=1/downsize) +
geom_point(data= df.cov.selected.v %>% filter(year!=2019, year!=2021,year!=2022,year!=2023,year!=2024),
aes(y=value, colour=scenario), shape=21, size=3/downsize, fill="white", stroke = 1/downsize) +
coord_cartesian(xlim=c(year.start, year.end+2), ylim=varlims) +
ggtitle(varname) +
theme_classic() +
scale_color_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
scale_fill_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
theme(legend.position="none",
title=element_text(size=theme.size),
text = element_text(size=theme.size)
) +
xlab(NULL) +
ylab(y.unit) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0), labels = ifelse(y.unit=="GtCO2/yr",formatter1000(),formatter.standard()))
} else if (range==25){
p.v <- ggplot(df.cov.selected.v,
aes(x=year, group=scenario)) +
# add 0 and net-zero
geom_rect(aes(xmin=year.start, xmax=year.end, ymin=-50000, ymax=0), fill="#ffebcd", alpha=0.08) +
# add sr 15 timeseries category
geom_ribbon(data=df.v.sr15, aes(ymin=q25, ymax=q75, fill=category), alpha=0.3) +
geom_line(data=df.v.sr15, aes(y=med, colour=category), linetype="dashed", size=1.5/downsize) +
geom_point(data=df.v.sr15, aes(y=med, colour=category), shape=21, size=3/downsize, fill="white", stroke = 1/downsize) +
# add gdp uncertainty range
geom_ribbon(data=gdp.v %>% filter(year>=2021), aes(ymin=min, ymax=max, group=model), fill='black', alpha=0.1) +
# add covid scenarios
geom_line(data= df.cov.selected.v,aes(y=value, colour=scenario), linetype="solid", size=1/downsize) +
geom_point(data= df.cov.selected.v %>% filter(year!=2019, year!=2021,year!=2022,year!=2023,year!=2024),
aes(y=value, colour=scenario), shape=21, size=3/downsize, fill="white", stroke = 1/downsize) +
coord_cartesian(xlim=c(year.start, year.end+2), ylim=varlims) +
ggtitle(varname) +
theme_classic() +
scale_color_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
scale_fill_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
theme(legend.position="none",
title=element_text(size=theme.size),
text = element_text(size=theme.size)
) +
xlab(NULL) +
ylab(y.unit) +
scale_x_continuous(expand = c(0,0), limits = c(year.start,year.end+5)) +
scale_y_continuous(expand = c(0,0), labels = ifelse(y.unit=="GtCO2/yr",formatter1000(),formatter.standard()))
}
if (plotlabels){
p.v <- p.v +
coord_cartesian(xlim=c(year.start,year.end+5),ylim=varlims) + #c(year.start, year.end+2+(8*(downsize))), ylim=varlims) +
# add labels to scenarios
geom_text_repel(data=df.cov.selected.v %>% filter(year==year.end),
size=geom.text.size/(downsize+0.5),
xlim=c(year.end+1,year.end+5),#c(year.end+2-10+10*downsize, year.end+5+(3*(downsize-1))-10+15*downsize ),
aes(x=year.end,
y=value,
# fontface = "bold",
group=scenario, label=scenario, colour=scenario), direction="y", hjust = 0, force=5)
}
# calculate combined_ranges
if (plotranges){
NPi_str <- "CD-LINKS_NPi"
df.npi.sr15 <- df.15.selected %>% filter(variable==var) %>%
filter(scenario==NPi_str) %>%
filter(year==year.end)
df.npi.sr15$category <- "NPi"
df.npi.sr15 <- df.npi.sr15 %>%
group_by(year, category) %>%
mutate(min=min(na.rm=T, value), q10=quantile(na.rm=T,x=value,0.1), med=median(value), q90=quantile(na.rm=T,x=value,0.9), max=max(na.rm=T, value)) %>%
ungroup() %>%
distinct(year, category, q10,med,q90, .keep_all = TRUE) %>%
select(-c(model,value)) %>%
filter(year>=year.start & year<=year.end)
NDC_str <- "CD-LINKS_INDCi"
df.ndc.sr15 <- df.15.selected %>% filter(variable==var) %>%
filter(scenario==NDC_str)
df.ndc.sr15$category <- "INDCi"
df.ndc.sr15 <- df.ndc.sr15 %>%
group_by(year, category) %>%
mutate(min=min(na.rm=T,value), q10=quantile(na.rm=T,x=value,0.1), med=median(value), q90=quantile(na.rm=T,x=value,0.9), max=max(na.rm=T, value)) %>%
ungroup() %>%
distinct(year, category, q10,med,q90, .keep_all = TRUE) %>%
select(-c(model,value)) %>%
filter(year>=year.start & year<=year.end)
t15_str <- "1.5C no or low OS"
df.t15.sr15 <- df.15.selected %>% filter(variable==var) %>%
filter(category==t15_str)
df.t15.sr15$scenario <- t15_str
df.t15.sr15 <- df.t15.sr15 %>%
group_by(year, category) %>%
mutate(min=min(na.rm=T,value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(na.rm=T,value)) %>%
ungroup() %>%
distinct(year, category, q10,q25,med,q75,q90, .keep_all = TRUE) %>%
select(-c(model,value)) %>%
filter(year>=year.start & year<=year.end)
cov_range <- df.cov.selected %>% filter(variable==var & scenario!="Baseline-no-COVID") %>%
filter(category%in%cat.covid) %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), med=median(value), q90=quantile(na.rm=T,x=value,0.9), max=max(value))%>%
filter(year==year.end) %>% mutate(scenario='Covid recoveries') %>% select(-model)
combined_ranges <- bind_rows(df.npi.sr15, df.ndc.sr15) %>% bind_rows(df.t15.sr15) %>% bind_rows(cov_range)
print(combined_ranges)
p.r <- ggplot(combined_ranges %>% filter(year==year.end), aes(x=year.end))+
geom_linerange(aes(x=year,xmin=year,
xmax=year,
y=min,ymin=min,
ymax=max,
group=scenario,
colour=scenario),
position = position_dodge(width = .1),
size=2) +
geom_text(aes(x=year,y=max+350,
label=paste0(as.character(round(max/1000))),
group=scenario,
colour=scenario),
position = position_dodge(width = .1),
size=2.5) +
geom_text(aes(x=year,y=min-350,
label=paste0(as.character(round(min/1000))),
group=scenario,
colour=scenario),
position = position_dodge(width = .1),
size=2.5) +
guides(colour=guide_legend("Scenario family"),
label=FALSE) +
theme_void()+
theme(axis.line.x=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
) +
scale_color_manual(name = "Scenario family",
labels = c("1.5C (no or low OS)", "NDCs (CD-LINKS)", "National Policies (CD-LINKS)", "Covid recoveries"),
values = c(mark_cols,
"1.5C no or low OS"= "#1e9583",
"Below 2.0C"="#63bce4",
"Above 2.0C"="#e78731",
"INDCi"="#8491B4FF",
"NPi"="#4DBBD5FF",
"Covid recoveries"="red",
"CD-LINKS_NPi"="#4DBBD5FF",
"CD-LINKS_INDCi"="#8491B4FF"
))+
xlab(NULL) +
ylab(y.unit) +
scale_y_continuous(limits = varlims,
expand = c(0,0))
return(p.r)
} else {
return(p.v)
}
}
# Part 2.1.1: simpler function that is alike main text figure 2 ====
do_main_timeseries_plot_simple <- function(df.cov.selected, var, varname, varlims, year.end=2035,
df.15=df.15.selected, cat.sr15="1.5C no or low OS", cat.covid="Above 2.0C",
downsize=1, range=5,
df.gdp.sel=df.gdp.selected,
y.unit = "GtCO2/yr",
plotlabels=TRUE, plotranges=TRUE, justpathways=FALSE, plotgdpsensitivity=TRUE){
# get GDP uncertainty range for specific variable
gdp.v <- df.gdp.sel %>% filter(variable==var) %>% mutate(year=as.numeric(year)) %>% group_by(year) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
gdp.w.end <- gdp.v %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
gdp.v <- gdp.v %>% left_join(gdp.w.end, by=c("model", "scenario"))
# select the required variable from the covid scenarios
df.cov.selected.v <- df.cov.selected %>% filter(variable==var) %>% filter(category%in%cat.covid) %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
drop_na(value) %>%
mutate(min=min(value), q10=quantile(na.rm=T,x=value,0.1), q25=quantile(na.rm=T,x=value,0.25), med=median(value), q75=quantile(na.rm=T,x=value,0.75), q90=quantile(na.rm=T,x=value,0.9), max=max(value)) %>%
filter(year>=year.start & year<=year.end)
df.cov.selected.end <- df.cov.selected.v %>% filter(year==year.end) %>%
rename(min.end=min, q10.end=q10, q25.end=q25, med.end=med, q75.end=q75, q90.end=q90, max.end=max) %>%
ungroup() %>%
select(model, scenario, min.end, q10.end, q25.end, med.end, q75.end, q90.end, max.end)
df.cov.selected.v <- df.cov.selected.v %>% left_join(df.cov.selected.end, by=c("model", "scenario"))
# do main plot
geom.text.size <- 4/downsize
theme.size <- (14/5) * geom.text.size
formatter1000 <- function(){
function(x)x/1000
}
formatter.standard <- function(){
function(x)x
}
if (justpathways==TRUE){
p.v <- ggplot(df.cov.selected %>% filter(variable==var) %>% mutate(year=as.numeric(year)) %>% group_by(year, category) %>%
drop_na(value) %>%
mutate(min=min(na.rm=T, value),
q10=quantile(na.rm=T,x=value,0.1),
q25=quantile(na.rm=T,x=value,0.25),
med=median(value),
q75=quantile(na.rm=T,x=value,0.75),
q90=quantile(na.rm=T,x=value,0.9),
max=max(na.rm=T,value)) %>%
filter(year>=year.start & year<=year.end) ,
aes(x=year, group=scenario)) +
# add gdp uncertainty range
geom_ribbon(data=gdp.v %>% filter(year>=2021), aes(ymin=min, ymax=max, group=model),
fill=ifelse(plotgdpsensitivity==TRUE,'black','white'), alpha=0.1) +
# add covid scenarios
geom_line(data= df.cov.selected.v,aes(y=value, colour=scenario), linetype="solid", size=1/downsize) +
geom_point(data= df.cov.selected.v %>% filter(year!=2019, year!=2021,year!=2022,year!=2023,year!=2024),
aes(y=value, colour=scenario), shape=21, size=3/downsize, fill="white", stroke = 1/downsize) +
coord_cartesian(xlim=c(year.start, year.end+2),
ylim=varlims) +
ggtitle(varname) +
theme_classic() +
scale_color_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
scale_fill_manual(values=c(mark_cols,"1.5C no or low OS"= "#1e9583", "Below 2.0C"="#63bce4","Above 2.0C"="#e78731"))+
theme(legend.position="none",
title=element_text(size=theme.size),
text = element_text(size=theme.size)
) +
xlab(NULL) +
ylab(y.unit) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0),
labels = ifelse(y.unit=="GtCO2/yr",formatter1000(),formatter.standard()))
if (plotlabels){
p.v <- p.v +
coord_cartesian(xlim=c(year.start,year.end+5),ylim=varlims) + #c(year.start, year.end+2+(8*(downsize))), ylim=varlims) +
# add labels to scenarios
geom_text_repel(data=df.cov.selected.v %>% filter(year==year.end),
size=geom.text.size/(downsize+0.5),
xlim=c(year.end+1,year.end+5),#c(year.end+2-10+10*downsize, year.end+5+(3*(downsize-1))-10+15*downsize ),
aes(x=year.end,
y=value,
# fontface = "bold",
group=scenario, label=scenario, colour=scenario), direction="y", hjust = 0, force=5)
return(p.v)
}
}
}
# Part 2.2: Functions for data processing to analyse mitigation scenarios, for main text figure 3 ====
# Part 2.2.1: Define decarbonisation functions ====
decarb_sector <- function(df.co2, c.scen=comp.scen){
df.co2.rec <- df.co2 %>% filter(year%in%c(2021,2025)) %>% pivot_wider(names_from = year, values_from = co2) %>%
mutate(decarb_pace_rec=(`2025`-`2021`)/`2021`*100/4) %>%
select(-c(`2025`,`2021`))
df.co2.rec <- df.co2.rec %>%
mutate(ref_rec = ifelse(grepl("550", scenario, fixed = FALSE),
(df.co2.rec %>% filter(scenario==paste0(c.scen, " (550)")))$decarb_pace_rec,
ifelse(grepl("1000", scenario, fixed = FALSE),
(df.co2.rec %>% filter(scenario==paste0(c.scen, " (1000)")))$decarb_pace_rec,
NA)))
df.co2.post.rec <- df.co2 %>% filter(year%in%c(2025,2040)) %>% pivot_wider(names_from = year, values_from = co2) %>%
mutate(decarb_pace_post_rec=(`2040`-`2025`)/`2025`*100/16) %>%
select(-c(`2040`,`2025`))
df.co2.post.rec <- df.co2.post.rec %>%
mutate(ref_post_rec = ifelse(grepl("550", scenario, fixed = FALSE),
(df.co2.post.rec %>% filter(scenario==paste0(c.scen, " (550)")))$decarb_pace_post_rec,
ifelse(grepl("1000", scenario, fixed = FALSE),
(df.co2.post.rec %>% filter(scenario==paste0(c.scen, " (1000)")))$decarb_pace_post_rec,
NA)))
df.co2.deco2 <- left_join(df.co2.rec,df.co2.post.rec) %>%
mutate(postpone.ambition.ratio=decarb_pace_post_rec/decarb_pace_rec) %>%
mutate(ref_ratio_rec=decarb_pace_rec/ref_rec) %>%
mutate(ref_ratio_post_rec=decarb_pace_post_rec/ref_post_rec) %>%
filter(grepl('550',scenario,fixed=FALSE)|grepl('1000',scenario,fixed=FALSE)) %>%
mutate(clim=ifelse(grepl('550',scenario,fixed=FALSE),'1.5C','below 2C'))
decarb.sector <- df.co2.deco2 %>% select(scenario,variable,decarb_pace_rec,decarb_pace_post_rec) %>% filter(!grepl('FP',scenario,fixed=FALSE))
return(decarb.sector)
}
calculate_decarb <- function(dfl, region.sel = "World", comp.scen = 'Restore', only.total=FALSE){
if (only.total){
df.co2 <- dfl %>% filter(variable=="Emissions|CO2") %>%
filter(region==region.sel) %>% rename(co2=value)
decarb.total <<- decarb_sector(df.co2) %>% mutate(variable="Emissions|CO2")
return(
decarb.total %>%
mutate(clim=ifelse(grepl('550',scenario,fixed=FALSE), '1.5', '2.0'))
)
}
# CO2
df.co2.t <- dfl %>% filter(variable=="Emissions|CO2|Energy|Demand|Transportation") %>%
filter(region==region.sel) %>% rename(co2=value)
df.co2.b <- dfl %>% filter(variable=="Emissions|CO2|Energy|Demand|Residential and Commercial") %>%
filter(region==region.sel) %>% rename(co2=value)
df.co2.i <- dfl %>% filter(variable=="Emissions|CO2|Energy|Demand|Industry") %>%
filter(region==region.sel) %>% rename(co2=value)
decarb.t <<- decarb_sector(df.co2.t, c.scen=comp.scen) %>% mutate(variable="Emissions|CO2|Energy|Demand|Transportation")
decarb.b <<- decarb_sector(df.co2.b, c.scen=comp.scen) %>% mutate(variable="Emissions|CO2|Energy|Demand|Residential and Commercial")
decarb.i <<- decarb_sector(df.co2.i, c.scen=comp.scen) %>% mutate(variable="Emissions|CO2|Energy|Demand|Industry")
# combine decarbonisation paces in one dataframe
decarb <- decarb.t %>% bind_rows(decarb.i) %>% bind_rows(decarb.b) %>%
mutate(clim=ifelse(grepl('550',scenario,fixed=FALSE), '1.5', '2.0'))
return(decarb)
}
get_relative_decarb_paces <- function(decarb, comp.scen = 'Restore'){
out.df.for.radar <- decarb %>% select(scenario,variable,decarb_pace_post_rec) %>%
# mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2"&grepl('550',scenario,fixed=TRUE),
# decarb_pace_post_rec/(decarb.total %>% filter(grepl(paste0(comp.scen, ' (550)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
# mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2"&grepl('1000',scenario,fixed=TRUE),
# decarb_pace_post_rec/(decarb.total %>% filter(grepl(paste0(comp.scen, ' (1000)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2|Energy|Demand|Transportation"&grepl('550',scenario,fixed=TRUE),
decarb_pace_post_rec/(decarb.t %>% filter(grepl(paste0(comp.scen, ' (550)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2|Energy|Demand|Transportation"&grepl('1000',scenario,fixed=TRUE),
decarb_pace_post_rec/(decarb.t %>% filter(grepl(paste0(comp.scen, ' (1000)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2|Energy|Demand|Residential and Commercial"&grepl('550',scenario,fixed=TRUE),
decarb_pace_post_rec/(decarb.b %>% filter(grepl(paste0(comp.scen, ' (550)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2|Energy|Demand|Residential and Commercial"&grepl('1000',scenario,fixed=TRUE),
decarb_pace_post_rec/(decarb.b %>% filter(grepl(paste0(comp.scen, ' (1000)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2|Energy|Demand|Industry"&grepl('550',scenario,fixed=TRUE),
decarb_pace_post_rec/(decarb.i %>% filter(grepl(paste0(comp.scen, ' (550)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
mutate(decarb_pace_post_rec = ifelse(variable=="Emissions|CO2|Energy|Demand|Industry"&grepl('1000',scenario,fixed=TRUE),
decarb_pace_post_rec/(decarb.i %>% filter(grepl(paste0(comp.scen, ' (1000)'),scenario,fixed=TRUE)))$decarb_pace_post_rec,decarb_pace_post_rec)) %>%
rename(value=decarb_pace_post_rec) %>%
mutate(variable=ifelse(variable=="Emissions|CO2|Energy|Demand|Transportation","CO2 Transport",
ifelse(variable=="Emissions|CO2|Energy|Demand|Residential and Commercial","CO2 Buildings",
ifelse(variable=="Emissions|CO2|Energy|Demand|Industry","CO2 Industry",
variable))))
return(out.df.for.radar)
}
# Part 2.2.2 function for producing all radar data ====
produce_radar_data <- function(dfcv=df.cov.all, compare.scens = c("Restore", "Baseline-no-COVID", "Green Push"), regions.to.plot=regions.to.plot, duration=duration){
radar.all <- NULL
# Part 2.2.2.1: variable selection and categorisation for region aggregation ====
list.of.variables.for.circle.additive <- c(
"Emissions|CO2", # general decarbonization
"Emissions|CO2|Energy|Demand|Transportation", # decarbonizing transport
"Emissions|CO2|Energy|Demand|Residential and Commercial", # decarbonizing buildings
"Emissions|CO2|Energy|Demand|Industry", # decarbonizing industry
"Capacity|Electricity|Coal", # coal phase-out
"Emissions|Kyoto Gases (AR5-GWP100)", # for calculating aggregated carbon costs
"Final Energy|Transportation|Electricity", # for electrification transport
"Final Energy|Transportation", # for electrification transport
"Investment|Energy Supply" # energy investment
)
list.of.variables.weights <- c(
"Secondary Energy|Electricity", # for aggregating solar-wind share
"Emissions|Kyoto Gases (AR5-GWP100)" # for calculating aggregated carbon costs
)
list.of.variables.for.circle.weighted <- c(
"Secondary Energy|Electricity|Solar-Wind share", # electricity generation
"Price|Carbon" # for calculating aggregated carbon costs
)
# Part 2.2.2.2: Do region aggregation for regional plots ====
df <- dfcv %>% filter(variable%in%c(list.of.variables.for.circle.additive, list.of.variables.for.circle.weighted, list.of.variables.weights))
# aggregate to R5 level
oecd <- c("NAM", "WEU", "PAO")
afr <- c("AFR", "MEA")
lam <- c("LAM")
asia <- c("SAS", "PAS", "CPA")
ref <- c("EEU", "FSU")