From f4bc415987626239709d059cd600a3bf171dd052 Mon Sep 17 00:00:00 2001
From: Marianna Rottoli <marianna.rottoli@mail.polimi.it>
Date: Thu, 30 Apr 2020 10:57:11 +0200
Subject: [PATCH] Added plots for FE road transp and per capita.

---
 scripts/output/comparison/EDGEcomparison.R    |  20 ++--
 .../EDGETransportComparison.Rmd               | 100 +++++++++++++-----
 .../notebook_templates/helper_dashboard.R     |   6 +-
 .../EDGETransportReport.Rmd                   |  24 ++++-
 4 files changed, 105 insertions(+), 45 deletions(-)

diff --git a/scripts/output/comparison/EDGEcomparison.R b/scripts/output/comparison/EDGEcomparison.R
index 442fb05..43c7df0 100644
--- a/scripts/output/comparison/EDGEcomparison.R
+++ b/scripts/output/comparison/EDGEcomparison.R
@@ -35,7 +35,7 @@ gdx_name = "fulldata.gdx"
 emi_all = NULL
 salescomp_all = NULL
 fleet_all = NULL
-EJLDV_all = NULL
+EJroad_all = NULL
 EJmode_all = NULL
 ESmodecap_all = NULL
 CO2km_int_newsales_all = NULL
@@ -112,15 +112,15 @@ fleetFun = function(vintcomp, newcomp, sharesVS1){
 }
 
 
-EJLDVFun <- function(demandEJ){
-  demandEJ = demandEJ[subsector_L1 == "trn_pass_road_LDV_4W",]
+EJroadFun <- function(demandEJ){
+  demandEJ = demandEJ[subsector_L3 %in% c("trn_pass_road", "trn_freight_road"),]
   demandEJ <- demandEJ[, c("sector", "subsector_L3", "subsector_L2", "subsector_L1", "vehicle_type", "technology", "iso", "year", "demand_EJ")]
-  
   demandEJ = merge(demandEJ, REMIND2ISO_MAPPING, by = "iso")
   demandEJ[technology == "Hybrid Liquids", technology := "Liquids"]
   demandEJ[technology == "FCEV", technology := "Hydrogen"]
-  demandEJ[technology == "BEV", technology := "Electricity"]
-  demandEJ = demandEJ[, .(demand_EJ = sum(demand_EJ)), by = c("region", "year", "technology")]
+  demandEJ[technology %in% c("BEV", "Electric"), technology := "Electricity"]
+  demandEJ[subsector_L1 %in% c("trn_pass_road_bus_tmp_subsector_L1", "Bus_tmp_subsector_L1"), subsector_L1 := "Bus_tmp_subsector_L1"]
+  demandEJ = demandEJ[, .(demand_EJ = sum(demand_EJ)), by = c("region", "year", "technology", "subsector_L1")]
   return(demandEJ)
   
 }
@@ -351,7 +351,7 @@ for (outputdir in outputdirs) {
   ## calculate fleet compositons
   fleet = fleetFun(vintcomp, newcomp, sharesVS1)
   ## calculate EJ from LDVs by technology
-  EJLDV = EJLDVFun(demandEJ)
+  EJroad = EJroadFun(demandEJ)
   ## calculate FE demand by mode
   EJmode = EJmodeFun(demandEJ)
   ## calculate ES demand per capita
@@ -366,7 +366,7 @@ for (outputdir in outputdirs) {
   ## add scenario dimension to the results
   fleet[, scenario := as.character(unique(miffile$scenario))]
   salescomp[, scenario := unique(miffile$scenario)]
-  EJLDV[, scenario := as.character(unique(miffile$scenario))]
+  EJroad[, scenario := as.character(unique(miffile$scenario))]
   EJmode[, scenario := as.character(unique(miffile$scenario))]
   ESmodecap[, scenario := as.character(unique(miffile$scenario))]
   CO2km_int_newsales[, scenario := as.character(unique(miffile$scenario))]
@@ -376,7 +376,7 @@ for (outputdir in outputdirs) {
   ## rbind scenarios
   salescomp_all = rbind(salescomp_all, salescomp)
   fleet_all = rbind(fleet_all, fleet)
-  EJLDV_all = rbind(EJLDV_all, EJLDV)
+  EJroad_all = rbind(EJroad_all, EJroad)
   EJmode_all = rbind(EJmode_all, EJmode)
   ESmodecap_all = rbind(ESmodecap_all, ESmodecap)
   CO2km_int_newsales_all = rbind(CO2km_int_newsales_all, CO2km_int_newsales)
@@ -392,7 +392,7 @@ dash_template = "EDGEdashboard.Rmd"
 saveRDS(EJmode_all, paste0(outdir, "/EJmode_all.RDS"))
 saveRDS(salescomp_all, paste0(outdir, "/salescomp_all.RDS"))
 saveRDS(fleet_all, paste0(outdir, "/fleet_all.RDS"))
-saveRDS(EJLDV_all, paste0(outdir, "/EJLDV_all.RDS"))
+saveRDS(EJroad_all, paste0(outdir, "/EJroad_all.RDS"))
 saveRDS(ESmodecap_all, paste0(outdir, "/ESmodecap_all.RDS"))
 saveRDS(CO2km_int_newsales_all, paste0(outdir, "/CO2km_int_newsales_all.RDS"))
 saveRDS(EJfuels_all, paste0(outdir, "/EJfuels_all.RDS"))
diff --git a/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd b/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd
index ae885cc..340f35c 100644
--- a/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd
+++ b/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd
@@ -20,7 +20,7 @@ library(quitte)
 ```{r, echo=FALSE, message=FALSE, warning=FALSE}
 # Set RDS files path
 EJmode_all = readRDS("EJmode_all.RDS")
-EJLDV_all = readRDS("EJLDV_all.RDS")
+EJroad_all = readRDS("EJroad_all.RDS")
 fleet_all = readRDS("fleet_all.RDS")
 salescomp_all = readRDS("salescomp_all.RDS")
 ESmodecap_all = readRDS("ESmodecap_all.RDS")
@@ -182,16 +182,17 @@ salescompf(salescomp_all)
 ```
 
 ```{r, echo=FALSE, message=FALSE, warning=FALSE}
-EJLDVpf = function(dt){
+EJroadpf = function(dt){
   dt[, technology := factor(technology, levels = legend_ord)]
-  plot = ggplot()+
-    geom_area(data = dt, aes(x=year, y=demand_EJ, group = technology, fill = technology), color="black",position= position_stack())+
+  dt = dt[year >= 2020]
+  plotLDV = ggplot()+
+    geom_area(data = dt[subsector_L1 == "trn_pass_road_LDV_4W"], aes(x=year, y=demand_EJ, group = technology, fill = technology), color = "black", size=0.05, position= position_stack())+
     labs(x = "", y = "[EJ]", title = "LDV Final Energy demand")+
     theme_minimal()+
     facet_grid(scenario~region)+
     scale_fill_manual("Technology", values = cols, breaks=legend_ord)+
     expand_limits(y = c(0,1))+
-    scale_x_continuous(breaks = c(2015,2030,2050, 2100))+
+    scale_x_continuous(breaks = c(2020, 2030,2050, 2100))+
     theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
           axis.title = element_text(size = 8),
@@ -201,23 +202,63 @@ EJLDVpf = function(dt){
           # legend.title = element_text(size = 8),
           strip.text = element_text(size=8),
           strip.background = element_rect(color = "grey"))
-  return(plot)
+
+
+   plotBus = ggplot()+
+    geom_area(data = dt[subsector_L1 %in% c("trn_pass_road_bus_tmp_subsector_L1", "Bus_tmp_subsector_L1")], aes(x=year, y=demand_EJ, group = technology, fill = technology), color = "black", size=0.05, position= position_stack())+
+    labs(x = "", y = "[EJ]", title = "Buses Final Energy demand")+
+    theme_minimal()+
+    facet_grid(scenario~region)+
+    scale_fill_manual("Technology", values = cols, breaks=legend_ord)+
+    expand_limits(y = c(0,1))+
+    scale_x_continuous(breaks = c(2020, 2030,2050, 2100))+
+    theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
+          axis.text.y = element_text(size = 8),
+          axis.title = element_text(size = 8),
+          axis.line = element_line(size = 0.5, colour = "grey"),
+          title = element_text(size = 8),
+          # legend.text = element_text(size =8),
+          # legend.title = element_text(size = 8),
+          strip.text = element_text(size=8),
+          strip.background = element_rect(color = "grey"))
+
+
+   plotTrucks = ggplot()+
+    geom_area(data = dt[subsector_L1 %in% c("trn_freight_road_tmp_subsector_L1")], aes(x=year, y=demand_EJ, group = technology, fill = technology), color = "black", size=0.05, position= position_stack())+
+    labs(x = "", y = "[EJ]", title = "Trucks Final Energy demand")+
+    theme_minimal()+
+    facet_grid(scenario~region)+
+    scale_fill_manual("Technology", values = cols, breaks=legend_ord)+
+    expand_limits(y = c(0,1))+
+    scale_x_continuous(breaks = c(2020, 2030,2050, 2100))+
+    theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
+          axis.text.y = element_text(size = 8),
+          axis.title = element_text(size = 8),
+          axis.line = element_line(size = 0.5, colour = "grey"),
+          title = element_text(size = 8),
+          # legend.text = element_text(size =8),
+          # legend.title = element_text(size = 8),
+          strip.text = element_text(size=8),
+          strip.background = element_rect(color = "grey"))
+
+  return(plotlist = list(plotLDV = plotLDV, plotBus = plotBus, plotTrucks = plotTrucks))
 }
 
-EJLDVpf(EJLDV_all)
+EJroadpf(EJroad_all)
 
 ```
 
 ```{r, echo=FALSE, message=FALSE, warning=FALSE}
 EJmodepf = function(dt){
+  dt = dt[year >= 2020]
   plot = ggplot()+
-    geom_area(data = dt, aes(x=year, y=demand_EJ, group = interaction(vehicle_type_plot,aggr_mode), fill = vehicle_type_plot), color = "black", position= position_stack())+
+    geom_area(data = dt, aes(x=year, y=demand_EJ, group = interaction(vehicle_type_plot,aggr_mode), fill = vehicle_type_plot), color = "black", size=0.05, position= position_stack())+
     labs(x = "", y = "[EJ]", title = "Total transport final energy demand")+
     theme_minimal()+
     facet_grid(scenario~region)+
     scale_fill_manual("Vehicle Type",values = cols, breaks=legend_ord)+
     expand_limits(y = c(0,1))+
-    scale_x_continuous(breaks = c(2015,2030,2050, 2100))+
+    scale_x_continuous(breaks = c(2020,2030,2050, 2100))+
     theme(axis.text.x = element_text(angle = 90, size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size=8),
           axis.title = element_text(size = 8),
@@ -239,13 +280,13 @@ EJmodepf(EJmode_all)
 ESmodecappf = function(dt){
   dt[, vehicle_type_plot := factor(vehicle_type_plot, levels = legend_ord)]
   plot_frgt = ggplot()+
-    geom_area(data = dt[mode == "freight"], aes(x=year, y=cap_dem, group = vehicle_type_plot, fill = vehicle_type_plot), color="black",position= position_stack())+
+    geom_area(data = dt[mode == "freight" & year >= 2020], aes(x=year, y=cap_dem, group = vehicle_type_plot, fill = vehicle_type_plot), color="black", size=0.05, position= position_stack())+
     labs(x = "", y = "Energy Services demand [tkm/cap]")+
     theme_minimal()+
     facet_grid(scenario~region)+
     scale_fill_manual("Vehicle Type",values = cols, breaks=legend_ord)+
     expand_limits(y = c(0,1))+
-    scale_x_continuous(breaks = c(2015,2030,2050, 2100))+
+    scale_x_continuous(breaks = c(2020,2030,2050, 2100))+
     theme(axis.text.x = element_text(angle = 90,  size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
           axis.title = element_text(size = 8),
@@ -258,13 +299,13 @@ ESmodecappf = function(dt){
 
   
   plot_pass = ggplot()+
-    geom_area(data = dt[mode == "pass"], aes(x=year, y=cap_dem, group = vehicle_type_plot, fill = vehicle_type_plot), color="black",position= position_stack())+
+    geom_area(data = dt[mode == "pass" & year >= 2020], aes(x=year, y=cap_dem, group = vehicle_type_plot, fill = vehicle_type_plot), color="black", size=0.05, position= position_stack())+
     labs(x = "", y = "Energy Services demand [pkm/cap]")+
     theme_minimal()+
     facet_grid(scenario~region)+
     scale_fill_manual("Vehicle Type",values = cols, breaks=legend_ord)+
     expand_limits(y = c(0,1))+
-    scale_x_continuous(breaks = c(2015,2030,2050, 2100))+
+    scale_x_continuous(breaks = c(2020,2030,2050, 2100))+
     theme(axis.text.x = element_text(angle = 90,  size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
           axis.title = element_text(size = 8),
@@ -289,10 +330,10 @@ ESmodecappf(ESmodecap_all)
 CO2km_int_newsalespf = function(dt){
   dt = dt[!is.na(gCO2_km_ave)]
   plot = ggplot()+
-    geom_line(data = dt[year >=2020], aes(x = year, y = gCO2_km_ave, group = scenario, color = scenario))+
+    geom_line(data = dt[year >= 2020], aes(x = year, y = gCO2_km_ave, group = scenario, color = scenario))+
     labs(title = expression(paste(CO["2"], " intensity of LDVs new additions")), y = expression(paste("[", gCO["2"], "/km]")), x = "")+
     expand_limits(y = c(0,1))+
-    scale_x_continuous(breaks = c(2015,2030,2050, 2100))+
+    scale_x_continuous(breaks = c(2020, 2030, 2050, 2100))+
     theme_minimal()+
     facet_grid(~region)+
     theme(axis.text.x = element_text(angle = 90,  size = 8, vjust=0.5, hjust=1),
@@ -315,14 +356,15 @@ CO2km_int_newsalespf(CO2km_int_newsales_all)
 ```{r, echo=FALSE, message=FALSE, warning=FALSE}
 ## LDV by fuel
 EJfuels_pf = function(dt){
+  dt = dt[year >= 2020]
   plot = ggplot()+
-    geom_area(data = dt, aes(x=year, y=demand_EJ, group = subtech, fill = subtech), color="black", position= position_stack())+
+    geom_area(data = dt, aes(x=year, y=demand_EJ, group = subtech, fill = subtech), color="black", size=0.05, position= position_stack())+
     labs(x = "", y = "[EJ]", title = "Transport FE demand by fuel")+
     theme_minimal()+
     facet_grid(scenario~region)+
     scale_fill_manual("Technology",values = cols, breaks=legend_ord)+
     expand_limits(y = c(0,1))+
-    scale_x_continuous(breaks = c(2015,2030,2050, 2100))+
+    scale_x_continuous(breaks = c(2020, 2030,2050, 2100))+
     theme(axis.text.x = element_text(angle = 90,  size = 8, vjust=0.5, hjust=1),
           axis.text.y = element_text(size = 8),
           axis.title = element_text(size = 8),
@@ -341,20 +383,22 @@ EJfuels_pf(EJfuels_all)
 
 ```{r, echo=FALSE, message=FALSE, warning=FALSE}
 emidem_pf = function(dt){
- 
+ dt[, scenario := as.character(scenario)]
   plot = ggplot()+
-    geom_line(data = dt, aes(x = year, y = value, group = variable, color = variable))+
-    labs(x = "", y = "CO2 emissions [Mt/CO2]")+
+    geom_line(data = dt, aes(x = year, y = value, group = scenario, color = scenario))+
+    labs(x = "", y = "CO2 emissions [Mt/CO2]", title = "Emissions from transport demand")+
     theme_minimal()+
     facet_grid(~region)+
-    theme(axis.text.x = element_text(size = 8),
-          axis.text.y = element_text(size=8),
-          axis.title = element_text(size = 9),
-          title = element_text(size = 9),
-          legend.position = "none",
-          strip.text = element_text(size=9))+
-    scale_color_manual("Scenario", values = cols)
-   
+    theme(axis.text.x = element_text(angle = 90,  size = 8, vjust=0.5, hjust=1),
+          axis.text.y = element_text(size = 8),
+          axis.title = element_text(size = 8),
+          title = element_text(size = 8),
+          legend.text = element_text(size = 8),
+          legend.title = element_text(size = 8),
+          strip.text = element_text(size = 8),
+          strip.background = element_rect(color = "grey"),
+          axis.line = element_line(size = 0.5, colour = "grey"))
+ 
   return(plot)
 }
 
diff --git a/scripts/output/comparison/notebook_templates/helper_dashboard.R b/scripts/output/comparison/notebook_templates/helper_dashboard.R
index ba560f5..63139e2 100644
--- a/scripts/output/comparison/notebook_templates/helper_dashboard.R
+++ b/scripts/output/comparison/notebook_templates/helper_dashboard.R
@@ -84,7 +84,7 @@ plotlyButtonsToHide <- list('sendDataToCloud', 'zoom2d', 'pan2d', 'select2d', 'l
 
 ## Load files
 EJmode_all = readRDS("EJmode_all.RDS")
-EJLDV_all = readRDS("EJLDV_all.RDS")
+EJroad_all = readRDS("EJroad_all.RDS")
 fleet_all = readRDS("fleet_all.RDS")
 salescomp_all = readRDS("salescomp_all.RDS")
 ESmodecap_all = readRDS("ESmodecap_all.RDS")
@@ -287,7 +287,7 @@ CO2km_intensity_newsalesdash = function(dt, scen){
 }
 
 EJLDVdash <- function(dt, scen){
-  
+  dt = dt[subsector_L1 == "trn_pass_road_LDV_4W"]
   dt[, technology := factor(technology, levels = legend_ord)]
   dt = dt[region == region_plot & scenario == scen & year >= 2015 & year <= 2050]
   dt[, details := paste0("Demand: ", round(demand_EJ, digits = 1), " [EJ]","<br>", "Technology: ", technology, "<br>", "Region: ", region," <br>", "Year: ", year) ]
@@ -378,7 +378,7 @@ create_plotlist = function(scens, salescomp_all, fleet_all, ESmodecap_all, EJfue
     ## CO2 intensity new sales LDVs
     CO2km_int_newsales = CO2km_intensity_newsalesdash(CO2km_int_newsales_all, scen)
     ## final energy LDVs by fuel
-    EJLDV = EJLDVdash(EJLDV_all, scen)
+    EJLDV = EJLDVdash(EJroad_all, scen)
     ## emissions transport demand
     emidem = emidem_dash(emidem_all, scen)
 
diff --git a/scripts/output/single/notebook_templates/EDGETransportReport.Rmd b/scripts/output/single/notebook_templates/EDGETransportReport.Rmd
index 84acf82..72472bf 100644
--- a/scripts/output/single/notebook_templates/EDGETransportReport.Rmd
+++ b/scripts/output/single/notebook_templates/EDGETransportReport.Rmd
@@ -197,7 +197,7 @@ p
 plotinconv = function(inco_tech, iso_plot, vehicle_type){
   p=ggplot()+
   geom_bar(data = inco_tech[iso == iso_plot & subsector_L1 == "trn_pass_road_LDV_4W" & vehicle_type == vehicle_type & year<=2100 & year>=2010], aes(x = as.character(year), y = value, group = logit_type, fill = logit_type), position = position_stack(), stat = "identity")+
-  facet_grid(~technology)+
+  facet_wrap(~technology, norw = 3)+
   theme_minimal()+
   # scale_fill_manual(values = cols)+
   expand_limits(y = c(0,0.8))+
@@ -383,10 +383,25 @@ demandEJplotf = function(demandEJ, POP){
                            1e-6]          ## in people/millionpeople=GJ/person
 
 
-  p=ggplot()+
-    geom_area(data = demandEJ[year > 2010], aes(x=year, y=demand_EJ, group = interaction(vehicle_type_plot,aggr_mode), fill = vehicle_type_plot), color = "black", position= position_stack())+
+  ppass=ggplot()+
+    geom_area(data = demandEJ[year > 2010 & aggr_mode %in% c("LDV", "Pass non LDV")], aes(x=year, y=demand_EJ, group = interaction(vehicle_type_plot,aggr_mode), fill = vehicle_type_plot), color = "black", position= position_stack())+
+    facet_wrap(~region, nrow = 4)+
+    labs(x = "", y = "Passenger final Energy demand [EJ]")+
+    theme_minimal()+
+    scale_fill_manual(values = cols)+
+    # scale_fill_manual("Vehicle Type",values = cols, breaks=legend_ord)+
+    theme(axis.text.x = element_text(size = 8),
+          axis.text.y = element_text(size=8),
+          axis.title = element_text(size = 9),
+          title = element_text(size = 9),
+          legend.text = element_text(size = 9),
+          legend.title = element_text(size =9),
+          strip.text = element_text(size=9))
+
+  pfreight=ggplot()+
+    geom_area(data = demandEJ[year > 2010 & aggr_mode %in% c("Freight")], aes(x=year, y=demand_EJ, group = interaction(vehicle_type_plot,aggr_mode), fill = vehicle_type_plot), color = "black", position= position_stack())+
     facet_wrap(~region, nrow = 4)+
-    labs(x = "", y = "Final Energy demand [EJ]")+
+    labs(x = "", y = "Freight final Energy demand [EJ]")+
     theme_minimal()+
     scale_fill_manual(values = cols)+
     # scale_fill_manual("Vehicle Type",values = cols, breaks=legend_ord)+
@@ -398,6 +413,7 @@ demandEJplotf = function(demandEJ, POP){
           legend.title = element_text(size =9),
           strip.text = element_text(size=9))
 
+
   pcap=ggplot()+
     geom_area(data = demandEJcap[year > 2020], aes(x=year, y=cap_dem, group = interaction(vehicle_type_plot,aggr_mode), fill = vehicle_type_plot), color = "black", position= position_stack())+
     facet_wrap(~region, nrow = 4)+
-- 
GitLab