From cf8a85f8dfbfe891de9266cfd3893e51082a1738 Mon Sep 17 00:00:00 2001 From: Marianna Rottoli <marianna.rottoli@mail.polimi.it> Date: Wed, 15 Apr 2020 10:42:32 +0200 Subject: [PATCH] Added dashboard. Modified reporting adding extra plots and fixes. --- scripts/output/comparison/EDGEcomparison.R | 246 +++++- .../EDGETransportComparison.Rmd | 159 +++- .../notebook_templates/EDGEdashboard.Rmd | 732 ++++++++++++++++++ .../notebook_templates/helper_dashboard.R | 390 ++++++++++ 4 files changed, 1499 insertions(+), 28 deletions(-) create mode 100644 scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd create mode 100644 scripts/output/comparison/notebook_templates/helper_dashboard.R diff --git a/scripts/output/comparison/EDGEcomparison.R b/scripts/output/comparison/EDGEcomparison.R index 2038639..442fb05 100644 --- a/scripts/output/comparison/EDGEcomparison.R +++ b/scripts/output/comparison/EDGEcomparison.R @@ -10,7 +10,9 @@ require(lucode) require(quitte) require(data.table) require(rmndt) - +require(moinput) +require(edgeTrpLib) +setConfig(forcecache = TRUE) if(!exists("source_include")) { ## Define arguments that can be read from command line @@ -35,9 +37,10 @@ salescomp_all = NULL fleet_all = NULL EJLDV_all = NULL EJmode_all = NULL - - - +ESmodecap_all = NULL +CO2km_int_newsales_all = NULL +EJfuels_all = NULL +emidem_all = NULL scenNames <- getScenNames(outputdirs) EDGEdata_path <- path(outputdirs, paste("EDGE-T/")) @@ -156,19 +159,193 @@ EJmodeFun = function(demandEJ){ } +ESmodecapFun = function(demandkm, POP){ + ## REMIND-EDGE results + demandkm <- demandkm[,c("sector","subsector_L3","subsector_L2", + "subsector_L1","vehicle_type","technology", "iso","year","demand_F")] + + ## attribute aggregated mode and vehicle names for plotting purposes, and aggregate + demandkm[, aggr_mode := ifelse(subsector_L1 %in% c("Three-Wheeler", "trn_pass_road_LDV_4W"), "LDV", NA)] + demandkm[, aggr_mode := ifelse(sector %in% c("trn_freight", "trn_shipping_intl"), "Freight", aggr_mode)] + demandkm[, aggr_mode := ifelse(sector %in% c("trn_aviation_intl"), "Pass. non LDV", aggr_mode)] + demandkm[, aggr_mode := ifelse(subsector_L2 %in% c("trn_pass_road_bus", "HSR_tmp_subsector_L2", "Passenger Rail_tmp_subsector_L2", "Cycle_tmp_subsector_L2", "Walk_tmp_subsector_L2", "Domestic Aviation_tmp_subsector_L2", "Bus") | subsector_L1 %in% c("trn_pass_road_LDV_2W"), "Pass. non LDV", aggr_mode)] + + demandkm[, veh := ifelse(grepl("Truck", vehicle_type) & vehicle_type != "Light Truck and SUV" | vehicle_type == "3W Rural", "Truck", NA)] + demandkm[, veh := ifelse(grepl("Large|SUV|Midsize|Multipurpose Vehicle|Van|Light Truck and SUV", vehicle_type), "Large Cars", veh)] + demandkm[, veh := ifelse(grepl("Subcompact|Compact|Mini|Three-Wheeler_tmp_vehicletype", vehicle_type), "Small Cars", veh)] + demandkm[, veh := ifelse(grepl("Motorcycle|Moped|Scooter", vehicle_type), "Motorbikes", veh)] + demandkm[, veh := ifelse(grepl("bus|Bus", vehicle_type), "Bus", veh)] + demandkm[, veh := ifelse(subsector_L3 == "Domestic Aviation", "Domestic Aviation", veh)] + demandkm[, veh := ifelse(subsector_L3 == "International Aviation", "International Aviation", veh)] + demandkm[, veh := ifelse(subsector_L3 == "Domestic Ship", "Domestic Shipping", veh)] + demandkm[, veh := ifelse(subsector_L3 == "International Ship", "International Shipping", veh)] + demandkm[, veh := ifelse(grepl("Freight Rail", vehicle_type), "Freight Rail", veh)] + demandkm[, veh := ifelse(grepl("Passenger Rail|HSR", vehicle_type), "Passenger Rail", veh)] + demandkm[, veh := ifelse(grepl("Ship", vehicle_type), "Shipping", veh)] + demandkm[, veh := ifelse(grepl("Cycle|Walk", subsector_L3), "Non motorized", veh)] + demandkm = demandkm[,.(demand_F = sum(demand_F)), by = c("iso", "year", "aggr_mode", "veh")] + setnames(demandkm, old = "veh", new = "vehicle_type") + + + demandkm[, vehicle_type_plot := factor(vehicle_type, levels = c("LDV","Freight Rail", "Truck", "Domestic Ship", "International Ship", + "Motorbikes", "Small Cars", "Large Cars", "Van", + "Domestic Aviation", "International Aviation","Bus", "Passenger Rail", + "Freight", "Non motorized", "Shipping"))] + + ## attribute aggregate mode (passenger, freight) + demandkm[, mode := ifelse(vehicle_type %in% c("Freight", "Freight Rail", "Truck", "Shipping") ,"freight", "pass")] + + ## aggregate to regions + POP = merge(POP, REMIND2ISO_MAPPING, all.x = TRUE, by = c("iso")) + POP = POP[, .(pop = sum(value)), by = c("region", "year")] + demandkm = merge(demandkm, REMIND2ISO_MAPPING, by = "iso") + demandkm = demandkm[, .(demand_F = sum(demand_F)), by = c("region", "year", "vehicle_type_plot", "aggr_mode", "mode")] + + demandkm = merge(demandkm, POP, all.x = TRUE, by =c("year", "region")) + + ## calculate per capita values + demandkm = demandkm[order(aggr_mode)] + demandkm[, cap_dem := demand_F/ ## in million km + pop] ## in million km/million people=pkm/person + + demandkm = demandkm[year >= 2015 & year <= 2100] + + return(demandkm) + +} + +FEliq_sourceFun = function(FEliq_source, gdp){ + ## Attribute oil and biodiesel (TODO Coal2Liquids is accounted for as Oil! + FEliq_source[, technology := ifelse(variable %in% c("FE|Transport|Liquids|Oil", "FE|Transport|Liquids|Coal"), "Oil", NA)] + FEliq_source[, technology := ifelse(variable %in% c("FE|Transport|Liquids|Biomass"), "Biodiesel", technology)] + FEliq_source[, technology := ifelse(variable %in% c("FE|Transport|Liquids|Hydrogen"), "Synfuel", technology)] + FEliq_source = FEliq_source[,.(value = sum(value)), by = c("model", "scenario", "region", "year", "unit", "technology")] + + FEliq_sourceR = FEliq_source[][, shareliq := value/sum(value),by=c("region", "year")] + ## to ISO level + FEliq_sourceISO <- disaggregate_dt(FEliq_source, REMIND2ISO_MAPPING, + valuecol="value", + datacols=c("model","scenario", "unit","technology"), + weights=gdp) + ## calculate share + FEliq_sourceISO[, shareliq := value/sum(value),by=c("iso", "year")] + + return(list(FEliq_sourceISO = FEliq_sourceISO, FEliq_sourceR = FEliq_sourceR)) +} + +CO2km_int_newsales_Fun = function(shares_LDV, mj_km_data, sharesVS1, FEliq_source, gdp){ + ## energy intensity https://en.wikipedia.org/wiki/Energy_density + # emi_petrol = 45 ## MJ/gFUEL + # emi_biodiesel = 42 ## MJ/gFUEL + # emi_cng = 54 ## MJ/gFUEL + # + # ## CO2 content + # CO2_petrol = 3.1 ## gCO2/gFUEL + # CO2_biodiesel = 2.7 ## TODO this number is made up! gCO2/gFUEL + # CO2_cng = 2.7 ## gCO2/gFUEL + + ## TODO of CO2 content of biodiesel is made up! gCO2/gFUEL Same for Synfuels! and for PHEVs! + emi_fuel = data.table(technology = c("Oil", "Biodiesel", "NG", "Synfuel", "Hybrid Liquids", "Hybrid Electric"), ei_gF_MJ = c(20, 20, 20, 20, 20, 10), emi_cGO2_gF = c(3.1, 3.1, 2.7, 2.7, 3.1, 3.1)) + + emi_liquids = merge(FEliq_source, emi_fuel, all.x = TRUE, by = "technology") + emi_liquids = emi_liquids[, .(ei_gF_MJ = sum(shareliq*ei_gF_MJ), emi_cGO2_gF = sum(shareliq*emi_cGO2_gF)), by = c("iso", "year")][, technology := "Liquids"] + emi_NG = cbind(emi_fuel[technology == "NG"], unique(FEliq_source[,c("year", "iso")])) + + emi_fuel = rbind(emi_NG, emi_liquids) + emi_fuel[, gCO2_MJ := ei_gF_MJ*emi_cGO2_gF] + ## merge emissions factor with energy intensity for LDVs + emi_fuel = merge(mj_km_data[subsector_L1 == "trn_pass_road_LDV_4W" & year %in% unique(FEliq_source$year)], emi_fuel, all.x = TRUE, by = c("iso", "year", "technology")) + emi_fuel[is.na(gCO2_MJ) & !technology %in% c("Liquids", "NG"), gCO2_MJ := 0] + emi_fuel[, gCO2_km := MJ_km * gCO2_MJ] + + ## merge with sales composition + intemi = merge(emi_fuel, shares_LDV, all.y = TRUE, by = c("iso", "year", "technology", "vehicle_type", "subsector_L1"), all.x = TRUE) + intemi = intemi[!is.na(share) & !is.na(gCO2_km)] + ## find average emission intensity + intemi[, gCO2_km_ave := gCO2_km*share] + intemi = intemi[,.(gCO2_km_ave = sum(gCO2_km_ave)), by = c("year", "iso", "vehicle_type")] + ## find average emissions across fleet (all vehicle types) + intemi = merge(intemi, sharesVS1, all.x = TRUE, by = c("iso", "year", "vehicle_type")) + intemi = intemi[,.(gCO2_km_ave = sum(gCO2_km_ave*share)), by = c("iso", "year", "subsector_L1")] + ## find regional values + intemi = merge(intemi, REMIND2ISO_MAPPING, by="iso") + intemi = merge(intemi, gdp, all.x=TRUE, by = c("iso", "year")) + intemi[, share := weight/sum(weight), by = c("year", "region")] + intemi = intemi[,.(gCO2_km_ave = sum(gCO2_km_ave*share)), by = c("year", "region")] + intemi = intemi[year >= 2015 & year <= 2100] + + return(intemi) +} + + +EJfuelsFun = function(demandEJ, FEliq_source){ + ## find the composition of liquid fuels + FEliq_source = FEliq_source[,.(value = sum(value)), by = c("region", "year", "technology")] + ## renmae technology not to generate confusion with all technologies (non liquids) + setnames(FEliq_source, old = "technology", new = "subtech") + FEliq_source[, technology := "Liquids"] + ## find shares + FEliq_source[, shareliq := value/sum(value),by=c("region", "year")] + ## merge with regional mapping + demandEJ = merge(demandEJ, REMIND2ISO_MAPPING, by = "iso") + ## attribute "liquids" to hybrid liquids + demandEJ[, technology := ifelse(technology %in% c("Liquids", "Hybrid Liquids"), "Liquids", technology)] + demandEJ[, technology := ifelse(technology %in% c("BEV", "LA-BEV", "Electric"), "Electricity", technology)] + demandEJ[, technology := ifelse(technology %in% c("FCEV"), "Hydrogen", technology)] + ## aggregate + demandEJ = demandEJ[, .(demand_EJ = sum(demand_EJ)), by = c("region", "year","technology")] + ## merge with liquids composition + demandEJ = merge(demandEJ, FEliq_source, all = TRUE, by = c("region", "year", "technology"), allow.cartesian=TRUE) + ## fuels that are not Liquids need a 1 as a share, otherwie would have an NA + demandEJ[, shareliq := ifelse(is.na(shareliq), 1, shareliq)] + demandEJ[, subtech := ifelse(is.na(subtech), technology, subtech)] + ## calculate demand by fuel including oil types + demandEJ = demandEJ[,.(demand_EJ = demand_EJ*shareliq), by = c("region", "year", "subtech")] + ## filter out years + demandEJ = demandEJ[year >= 2015 & year <= 2100] + + return(demandEJ) +} + +emidemFun = function(emidem){ + emidem = emidem[region!="World" & year >= 2015 & year <= 2100] + emidem[, variable := as.character(variable)] + return(emidem) +} + + for (outputdir in outputdirs) { ## load mif file name_mif = list.files(path = outputdir, pattern = "REMIND_generic", full.names = F) name_mif = name_mif[!grepl("withoutPlu", name_mif)] miffile <- as.data.table(read.quitte(paste0(outputdir, "/", name_mif))) - + miffile[, region:=as.character(region)] + miffile[, year := period] + miffile[, period:=NULL] + miffile = miffile[region != "World"] + ## load RDS files - sharesVS1 = readRDS(paste0(outputdir, "/EDGE-T/" ,"shares.RDS"))[["VS1_shares"]] - newcomp = readRDS(paste0(outputdir, "/EDGE-T/" ,"newcomp.RDS")) - vintcomp = readRDS(paste0(outputdir, "/EDGE-T/" ,"vintcomp.RDS")) - shares_LDV = readRDS(paste0(outputdir, "/EDGE-T/" ,"annual_sales.RDS")) - demandEJ = readRDS(paste0(outputdir, "/EDGE-T/" , "demandF_plot_EJ.RDS")) + sharesVS1 = readRDS(paste0(outputdir, "/EDGE-T/", "shares.RDS"))[["VS1_shares"]] + newcomp = readRDS(paste0(outputdir, "/EDGE-T/", "newcomp.RDS")) + vintcomp = readRDS(paste0(outputdir, "/EDGE-T/", "vintcomp.RDS")) + shares_LDV = readRDS(paste0(outputdir, "/EDGE-T/", "annual_sales.RDS")) + demandEJ = readRDS(paste0(outputdir, "/EDGE-T/", "demandF_plot_EJ.RDS")) + demandkm = readRDS(paste0(outputdir, "/EDGE-T/", "demandF_plot_pkm.RDS")) + mj_km_data = readRDS(paste0(outputdir, "/EDGE-T/", "mj_km_data.RDS")) + + ## load population and GDP + POP_country=calcOutput("Population", aggregate = F)[,, "pop_SSP2"] + POP <- magpie2dt(POP_country, regioncol = "iso", + yearcol = "year", datacols = "POP") + gdp <- getRMNDGDP(scenario = "gdp_SSP2", usecache = T) + ## select useful entries from mif file + FEliq_source = miffile[variable %in% c("FE|Transport|Liquids|Biomass", "FE|Transport|Liquids|Hydrogen", "FE|Transport|Liquids|Coal", "FE|Transport|Liquids|Oil"),] + emidem = miffile[variable %in% c("Emi|CO2|Transport|Demand"),] + ## modify mif file entries to be used in the functions + FEliq_source = FEliq_sourceFun(FEliq_source, gdp) + + ## calculate sales salescomp = SalesFun(shares_LDV, newcomp, sharesVS1) ## calculate fleet compositons @@ -177,31 +354,60 @@ for (outputdir in outputdirs) { EJLDV = EJLDVFun(demandEJ) ## calculate FE demand by mode EJmode = EJmodeFun(demandEJ) + ## calculate ES demand per capita + ESmodecap = ESmodecapFun(demandkm, POP) + ## calculate average emissions intensity from the LDVs fleet + CO2km_int_newsales = CO2km_int_newsales_Fun(shares_LDV, mj_km_data, sharesVS1, FEliq_source$FEliq_sourceISO, gdp) + ## calculate FE for all transport sectors by fuel, dividng Oil into Biofuels and Synfuels + EJfuels = EJfuelsFun(demandEJ, FEliq_source$FEliq_sourceR) + ## calculate demand emissions + emidem = emidemFun(emidem) ## 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))] EJmode[, scenario := as.character(unique(miffile$scenario))] - + ESmodecap[, scenario := as.character(unique(miffile$scenario))] + CO2km_int_newsales[, scenario := as.character(unique(miffile$scenario))] + EJfuels[, scenario := as.character(unique(miffile$scenario))] + emidem[, scenario := as.character(unique(miffile$scenario))] + ## rbind scenarios salescomp_all = rbind(salescomp_all, salescomp) fleet_all = rbind(fleet_all, fleet) EJLDV_all = rbind(EJLDV_all, EJLDV) EJmode_all = rbind(EJmode_all, EJmode) - + ESmodecap_all = rbind(ESmodecap_all, ESmodecap) + CO2km_int_newsales_all = rbind(CO2km_int_newsales_all, CO2km_int_newsales) + EJfuels_all = rbind(EJfuels_all, EJfuels) + emidem_all = rbind(emidem_all, emidem) } -dir.create("output/comparerunEDGE", showWarnings = FALSE) - -outdir = "output/comparerunEDGE/" +outdir = paste0("output/comparerunEDGE", gsub(" | ^([[:alpha:]]*).*","", Sys.time())) +dir.create(outdir) md_template = "EDGETransportComparison.Rmd" +dash_template = "EDGEdashboard.Rmd" -saveRDS(EJmode_all, "output/comparerunEDGE/EJmode_all.RDS") -saveRDS(salescomp_all, "output/comparerunEDGE/salescomp_all.RDS") -saveRDS(fleet_all, "output/comparerunEDGE/fleet_all.RDS") -saveRDS(EJLDV_all, "output/comparerunEDGE/EJLDV_all.RDS") - +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(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")) +saveRDS(emidem_all, paste0(outdir, "/emidem_all.RDS")) file.copy(file.path("./scripts/output/comparison/notebook_templates", md_template), outdir) rmarkdown::render(path(outdir, md_template), output_format="pdf_document") +## if it's a 4 scenarios comparison across ConvCase, SynSurge, ElecEra, and HydrHype. run the dashboard +if (length(outputdirs) == 4 & + isTRUE(any(grepl("SynSurge", outputdirs))) & + isTRUE(any(grepl("ConvCase", outputdirs))) & + isTRUE(any(grepl("ElecEra", outputdirs))) & + isTRUE(any(grepl("HydrHype", outputdirs))) ){ + file.copy(file.path("./scripts/output/comparison/notebook_templates/helper_dashboard.R"), outdir) + file.copy(file.path("./scripts/output/comparison/notebook_templates", dash_template), outdir) + rmarkdown::render(path(outdir, dash_template)) +} + diff --git a/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd b/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd index 8f62197..ae885cc 100644 --- a/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd +++ b/scripts/output/comparison/notebook_templates/EDGETransportComparison.Rmd @@ -1,8 +1,10 @@ --- title: "Compare scenarios Transport" output: + pdf_document: default html_document: df_print: paged +classoption: landscape --- ```{r, echo=FALSE, message=FALSE, warning=FALSE} @@ -16,13 +18,15 @@ library(quitte) ``` ```{r, echo=FALSE, message=FALSE, warning=FALSE} -# Set mif path +# Set RDS files path EJmode_all = readRDS("EJmode_all.RDS") EJLDV_all = readRDS("EJLDV_all.RDS") fleet_all = readRDS("fleet_all.RDS") salescomp_all = readRDS("salescomp_all.RDS") - -iso_plot = c("DEU", "USA", "IND", "CHN") +ESmodecap_all = readRDS("ESmodecap_all.RDS") +CO2km_int_newsales_all = readRDS("CO2km_int_newsales_all.RDS") +EJfuels_all = readRDS("EJfuels_all.RDS") +emidem_all = readRDS("emidem_all.RDS") setConfig(forcecache=T) @@ -35,6 +39,7 @@ cols <- c("NG" = "#d11141", "FCEV" = "#00aedb", "Hydrogen" = "#00aedb", "Biodiesel" = "#66a182", + "Synfuel" = "orchid", "Oil" = "#2e4057", "fuel price pkm" = "#edae49", "Operating costs registration and insurance" = "#8d96a3", @@ -48,7 +53,8 @@ cols <- c("NG" = "#d11141", "Trucks" = "#ff6a6a", "International Shipping" = "#cd2626", "Domestic Shipping" = "#ff4040", - "Truck" = "#ff7f50"; + "Shipping" = "#ff4040", + "Truck" = "#ff7f50", "Trucks (<3.5t)" = "#ff7f50", "Trucks (3.5t-16)" = "#8b0000", "Trucks (>16)" = "#fa8072", @@ -72,10 +78,16 @@ cols <- c("NG" = "#d11141", "FE|Buildings" = "#d2b48c", "FE|Industry" = "#919191", "Electricity_push" = "#00b159", + "ElecEra" = "#00b159", + "ElecEraWise" = "#68c6a4", + "HydrHype" = "#00aedb", + "HydrHypeWise" = "#o3878f", "Hydrogen_push" = "#00aedb", "Smart_lifestyles_Electricity_push" = "#68c6a4", # "Smart_lyfestiles_Electricity_push" = "#03878f", ##maybe "#o3878f" "Conservative_liquids" = "#113245", + "ConvCase" = "#113245", + "ConvCaseWise" = "#d11141", "Emi|CO2|Transport|Demand" = "#113245", "Emi|CO2|Industry|Gross" = "#919191", "Emi|CO2|Buildings|Direct" = "#d2b48c", @@ -85,12 +97,12 @@ cols <- c("NG" = "#d11141", "Cons. + Synfuels" = "orchid", "Ctax_Conservative" = "#d11141") -legend_ord_modes <- c("Freight Rail", "Truck", "Trucks (<3.5t)", "Trucks (3.5t-16)", "Trucks (>16)", "International Shipping","Domestic Shipping", "Trucks", +legend_ord_modes <- c("Freight Rail", "Truck", "Shipping", "International Shipping", "Domestic Shipping", "Trucks", "Motorbikes", "Small Cars", "Large Cars", "Van", "International Aviation", "Domestic Aviation","Bus", "Passenger Rail", "Freight", "LDV", "Pass non LDV", "Freight (Inland)", "Pass non LDV (Domestic)", "Non motorized") -legend_ord_fuels <- c("BEV", "Electricity", "Hybrid Electric", "FCEV", "Hydrogen", "Hybrid Liquids", "Liquids", "Oil", "Biodiesel", "NG") +legend_ord_fuels <- c("BEV", "Electricity", "Hybrid Electric", "FCEV", "Hydrogen", "Hybrid Liquids", "Liquids", "Oil", "Biodiesel", "Synfuel", "NG") legend_ord_costs <- c("fuel price pkm", "Operating costs registration and insurance", "Operating costs maintenance", "Capital cost") @@ -178,6 +190,8 @@ EJLDVpf = function(dt){ 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))+ 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), @@ -195,13 +209,15 @@ EJLDVpf(EJLDV_all) ``` ```{r, echo=FALSE, message=FALSE, warning=FALSE} -EJmodeFUn = function(dt){ +EJmodepf = function(dt){ 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())+ 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))+ 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), @@ -214,6 +230,133 @@ EJmodeFUn = function(dt){ return(plot) } -EJmodeFUn(EJmode_all) +EJmodepf(EJmode_all) + +``` + + +```{r, echo=FALSE, message=FALSE, warning=FALSE} +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())+ + 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))+ + 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")) + + + 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())+ + 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))+ + 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(list(plot_pass = plot_pass, plot_frgt = plot_frgt)) +} + + +ESmodecappf(ESmodecap_all) + +``` + + +```{r, echo=FALSE, message=FALSE, warning=FALSE} +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))+ + 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))+ + theme_minimal()+ + facet_grid(~region)+ + 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"))+ + guides(linetype = FALSE) + return(plot) +} + +CO2km_int_newsalespf(CO2km_int_newsales_all) +``` + + +```{r, echo=FALSE, message=FALSE, warning=FALSE} +## LDV by fuel +EJfuels_pf = function(dt){ + plot = ggplot()+ + geom_area(data = dt, aes(x=year, y=demand_EJ, group = subtech, fill = subtech), color="black", 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))+ + 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) +} + +EJfuels_pf(EJfuels_all) +``` + + +```{r, echo=FALSE, message=FALSE, warning=FALSE} +emidem_pf = function(dt){ + + plot = ggplot()+ + geom_line(data = dt, aes(x = year, y = value, group = variable, color = variable))+ + labs(x = "", y = "CO2 emissions [Mt/CO2]")+ + 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) + + return(plot) +} +emidem_pf(emidem_all) ``` diff --git a/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd b/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd new file mode 100644 index 0000000..7bc7fef --- /dev/null +++ b/scripts/output/comparison/notebook_templates/EDGEdashboard.Rmd @@ -0,0 +1,732 @@ +--- +title: "Transport decarbonization dashboard" +author: "" +output: + flexdashboard::flex_dashboard: + vertical_layout: fill + orientation: rows +runtime: flexdashboard +--- + + + +```{r loading_required_libraries, include=FALSE} + #Loading required libraries + library(flexdashboard) + library(ggplot2) + library(plotly) + library(magclass) + library(mip) + library(data.table) +``` + + + +```{r creating_charts, include=FALSE} +source("helper_dashboard.R") + +legend=plotlist$legend + +``` + +```{r global_options, include=FALSE} + # setting global R chunk options (https://yihui.name/knitr/options/#chunk_options) + knitr::opts_chunk$set(dev='svg', fig.asp = 1 ) # fig.asp = default aspect ratio +``` + +```{r creating_info_tooltips, include=FALSE} + #data frame with help tooltips + + helpTooltip_df <- data.frame( + title=c("Per capita Passenger Transport Energy Services Demand", "Per capita Freight Transport Energy Services Demand", "Sales composition", "Final energy LDVs by fuel","Transport Final Energy Demand", "Fleet composition", "CO2 intensity", "Emissions transport demand"), + placement=c("right", "left", "right", "left", "left", "left", "right", "right")) + + +helpTooltip = function(tooltipdf){ + + title <- as.character(tooltipdf$title) + placement <- as.character(tooltipdf$placement) + + contents <- gsub('\n', '',gsub('"', '"',paste0(' +<div class="tooltipContainer"> + <div class="tooltipDescription">', + legend[[title]]$description,' + </div> + <div class="legendContainer"> + <table class="legendTable"> + <tbody> + <tr>', + do.call(paste,lapply(names(legend[[title]]$contents),function(x){ + paste0('<tr><td>', + ifelse(is.null(legend[[title]]$contents[[x]]$linetype), + paste0('<div class="square"><div class="circle" style="background-color:',legend[[title]]$contents[[x]]$fill,';"></div></div>'), + paste0('<div class="line" style="border-color:',legend[[title]]$contents[[x]]$fill,'; border-style: ',legend[[title]]$contents[[x]]$linetype,';"></div>') + ),' + </td><td class="legendText">',x,'</td></tr>' + ) + })),' + </tbody> + </table> + </div> +</div>'))) + + result = "" + result = paste0(result,"<script type='text/javascript'>\n"); + result = paste0(result,"$(window).load(function () {\n"); + result = paste0(result," $('.chart-title').filter(function() { return $(this).clone().children().remove().end().text() == '", title, "' }).each(function(){\n"); + if(!(placement == "none")) + result = paste0(result," $(this).append('<i class=\"glyphicon glyphicon-info-sign infoIcon\" data-toggle=\"tooltip\" data-html=\"true\" data-placement=\"right\" title=\"",contents,"\"></i>');\n"); + result = paste0(result," $(this).find('[data-toggle=\"tooltip\"]').tooltip({ placement:'",placement,"'});\n"); + result = paste0(result," })\n"); + result = paste0(result,"})\n"); + result = paste0(result,"</script>"); + + return(result) +} + +``` + +<!--############################# --> +<!--#### START OF MENUS CODE #### --> +<!--############################# --> + +<!-- ### Creating main emissions menu ### --> + +<!--Creating First Page - LDV Page--> +Overview {data-icon="glyphicon glyphicon-scale"} +===================================== +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#overview){class="glyphicon glyphicon-oil sidebar-button selected"} +[](#overview-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button"} +[](#overview-bev-scenario){class="glyphicon glyphicon-flash sidebar-button"} +[](#overview-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button"} + +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Conventional Case", icon = "glyphicon-oil") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$ConvCase$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Conventional patterns. + +<!-- Dividing the page in two columns--> +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Per capita Passenger Transport Energy Services Demand +```{r} +plotlist$ConvCase$plot$ESmodecap_pass +``` + +### Per capita Freight Transport Energy Services Demand +```{r} +plotlist$ConvCase$plot$ESmodecap_frgt +``` + + +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Transport Final Energy Demand + +```{r} +plotlist$ConvCase$plot$EJfuels +``` + +### Emissions transport demand + +```{r} +plotlist$ConvCase$plot$emidem +``` + +Overview FCEV scenario {.hidden data-icon="glyphicon glyphicon-tint"} +===================================== + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#overview){class="glyphicon glyphicon-oil sidebar-button"} +[](#overview-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button selected"} +[](#overview-bev-scenario){class="glyphicon glyphicon-flash sidebar-button"} +[](#overview-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button"} + + +<!-- Dividing the page in two rows--> +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Hydrogen Hype", icon = "glyphicon-tint") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$HydrHype$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Increased preference for hydrogen vehicles. + +<!-- Dividing the page in two columns--> +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Per capita Passenger Transport Energy Services Demand +```{r} +plotlist$HydrHype$plot$ESmodecap_pass +``` + +### Per capita Freight Transport Energy Services Demand +```{r} +plotlist$HydrHype$plot$ESmodecap_frgt +``` + + +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Transport Final Energy Demand + +```{r} +plotlist$HydrHype$plot$EJfuels +``` + +### Emissions transport demand + +```{r} +plotlist$HydrHype$plot$emidem +``` + +Overview BEV scenario { .hidden data-icon="glyphicon glyphicon-flash"} +===================================== + + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#overview){class="glyphicon glyphicon-oil sidebar-button"} +[](#overview-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button"} +[](#overview-bev-scenario){class="glyphicon glyphicon-flash sidebar-button selected"} +[](#overview-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button"} + +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Electric Era", icon = "glyphicon-flash") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$ElecEra$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Increased preference for battery electric vehicles. + + +<!-- Dividing the page in two columns--> +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Per capita Passenger Transport Energy Services Demand +```{r} +plotlist$ElecEra$plot$ESmodecap_pass +``` + +### Per capita Freight Transport Energy Services Demand +```{r} +plotlist$ElecEra$plot$ESmodecap_frgt +``` + + +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Transport Final Energy Demand + +```{r} +plotlist$ElecEra$plot$EJfuels +``` + +### Emissions transport demand + +```{r} +plotlist$ElecEra$plot$emidem +``` + +Overview Synfuel scenario {.hidden data-icon="glyphicon glyphicon-random"} +===================================== + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#overview){class="glyphicon glyphicon-oil sidebar-button"} +[](#overview-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button"} +[](#overview-bev-scenario){class="glyphicon glyphicon-flash sidebar-button"} +[](#overview-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button selected"} + +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Synfuel Surge", icon = "glyphicon-random") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$SynSurge$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Increased preference for synthetic fuels. + + +<!-- Dividing the page in two columns--> +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Per capita Passenger Transport Energy Services Demand +```{r} +plotlist$SynSurge$plot$ESmodecap_pass +``` + +### Per capita Freight Transport Energy Services Demand +```{r} +plotlist$SynSurge$plot$ESmodecap_frgt +``` + + +Row {data-heigth=500} +----------------------------------------------------------------------- + +### Transport Final Energy Demand + +```{r} +plotlist$SynSurge$plot$EJfuels +``` + +### Emissions transport demand + +```{r} +plotlist$SynSurge$plot$emidem +``` + + +LDVs {data-icon="fa-car"} +===================================== + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#ldvs){class="glyphicon glyphicon-oil sidebar-button selected"} +[](#ldvs-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button"} +[](#ldvs-bev-scenario){class="glyphicon glyphicon-flash sidebar-button"} +[](#ldvs-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button"} + +<!-- Dividing the page in two rows--> +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Conventional Case", icon = "glyphicon-oil") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$ConvCase$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Conventional patterns. + + + +<!-- Dividing the page in two rows--> +Row {data-height= 450} +----------------------------------------------------------------------- + +### Sales composition {data-width=250} +```{r} +plotlist$ConvCase$plot$salescomp +``` + + +### CO2 intensity of new sales {data-width=250} + +```{r} +plotlist$ConvCase$plot$CO2km_int_newsales +``` + + +Row {data-height= 450} +----------------------------------------------------------------------- + +### Final energy LDVs by fuel +```{r} +plotlist$ConvCase$plot$EJLDV +``` + + +### Fleet composition +```{r} +plotlist$ConvCase$plot$vintcomp +``` + + + +LDVs FCEV scenario {.hidden data-icon="glyphicon glyphicon-tint"} +===================================== + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#ldvs){class="glyphicon glyphicon-oil sidebar-button"} +[](#ldvs-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button selected"} +[](#ldvs-bev-scenario){class="glyphicon glyphicon-flash sidebar-button"} +[](#ldvs-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button"} + + +<!-- Dividing the page in two rows--> +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Hydrogen Hype", icon = "glyphicon-tint") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$HydrHype$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Increased preference for hydrogen vehicles. + +<!-- Dividing the page in two rows--> +Row {data-height= 450} +----------------------------------------------------------------------- + +### Sales composition {data-width=250} +```{r} +plotlist$HydrHype$plot$salescomp +``` + + +### CO2 intensity of new sales {data-width=250} + +```{r} +plotlist$HydrHype$plot$CO2km_int_newsales +``` + + +Row {data-height= 450} +----------------------------------------------------------------------- + +### Final energy LDVs by fuel +```{r} +plotlist$HydrHype$plot$EJLDV +``` + + +### Fleet composition +```{r} +plotlist$HydrHype$plot$vintcomp +``` + + +LDVs BEV scenario { .hidden data-icon="glyphicon glyphicon-flash"} +===================================== + + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#ldvs){class="glyphicon glyphicon-oil sidebar-button"} +[](#ldvs-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button"} +[](#ldvs-bev-scenario){class="glyphicon glyphicon-flash sidebar-button selected"} +[](#ldvs-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button"} + + +<!-- Dividing the page in two rows--> +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Electric Era", icon = "glyphicon-flash") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$ElecEra$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Increased preference for battery electric vehicles. + + +<!-- Dividing the page in two rows--> +Row {data-height= 450} +----------------------------------------------------------------------- + +### Sales composition {data-width=250} +```{r} +plotlist$ElecEra$plot$salescomp +``` + + +### CO2 intensity of new sales {data-width=250} + +```{r} +plotlist$ElecEra$plot$CO2km_int_newsales +``` + + +Row {data-height= 450} +----------------------------------------------------------------------- + +### Final energy LDVs by fuel +```{r} +plotlist$ElecEra$plot$EJLDV +``` + + +### Fleet composition +```{r} +plotlist$ElecEra$plot$vintcomp +``` + + +LDVs Synfuel scenario {.hidden data-icon="glyphicon glyphicon-random"} +===================================== + +Side Navigation {.sidebar data-width=60} +------------------------------------- + +[](#ldvs){class="glyphicon glyphicon-oil sidebar-button"} +[](#ldvs-fcev-scenario){class="glyphicon glyphicon-tint sidebar-button"} +[](#ldvs-bev-scenario){class="glyphicon glyphicon-flash sidebar-button"} +[](#ldvs-synfuel-scenario){class="glyphicon glyphicon-random sidebar-button selected"} + +<!-- Dividing the page in two rows--> +Row {data-height=300} +----------------------------------------------------------------------- + +### Transport scenario {data-width=100} +```{r} +valueBox("Synfuel Surge", icon = "glyphicon-random") +``` + + +### Emissions {data-width=100} +```{r} +valueBox(plotlist$SynSurge$emiscen, icon = "fa-cloud") +``` + + +### Scenario description {data-width=200} + +Increased preference for synthetic fuels. + + + +<!-- Dividing the page in two rows--> +Row {data-height= 450} +----------------------------------------------------------------------- + +### Sales composition {data-width=250} +```{r} +plotlist$SynSurge$plot$salescomp +``` + + +### CO2 intensity of new sales {data-width=250} + +```{r} +plotlist$SynSurge$plot$CO2km_int_newsales +``` + + +Row {data-height= 450} +----------------------------------------------------------------------- + +### Final energy LDVs by fuel +```{r} +plotlist$SynSurge$plot$EJLDV +``` + + +### Fleet composition +```{r} +plotlist$SynSurge$plot$vintcomp +``` + + +<!-- creating information tooltip --> +```{r, results='asis'} +for(i in 1:nrow(helpTooltip_df)){ + cat(helpTooltip(helpTooltip_df[i,])) +} +``` + + +<!-- ADVANCED: using CSS to define the appearance of the side bar --> + +<!-- CSS --> +<style type="text/css"> + + .sidebar-button { /* sidebar size */ + display: inline-block; + font-size: 40px; + height: 80px; + padding-top: 20px + } + + .sidebar-button, .sidebar-button:hover { + text-decoration: none; + } + + .sidebar-button.selected, .sidebar-button.selected:hover { + opacity: 1; + } + + .sidebar-button { + opacity: 0.7; + } + + .sidebar-button:hover { + opacity: 0.4; + } + + /*legend*/ + .tooltipContainer { + padding: 5px; + } + + .tooltipDescription { + width: fit-content; + margin: 5px auto; + } + + .legendContainer { + } + + .legendTable { + margin:auto; + text-align: left; + border-collapse: separate; + border-spacing: 2px; + } + + .legendText { + padding: 4px 20px; + } + + .legendTable tr:nth-child(odd) { + background-color: #1d1e1f; /* Background-color of odd rows */ + } + + .legendTable tr:nth-child(even) { + background-color: #32353a; /* Background-color of even rows */ + } + + .square { + height: 16px; + width: 16px; + margin: 4px 12px; + } + + .circle { + height: 100%; + width: 100%; + border-radius: 50%; + display: inline-block; + } + + .line{ + width: 80%; + min-width: 22px; + border: 1px solid #fff; + margin: auto; + } + + .units { + float: right; + margin: -2px 5px 0 5px; + color: #b7b7b7; + } + + .infoIcon { + float: right; + margin: 0 5px; + color: #b7b7b7; + } + + .modalIcon { + float: right; + margin: 0 5px; + color: #b7b7b7; + } + + .expandIcon { + float: right; + margin: 0 5px; + color: #b7b7b7; + } + + .modalIcon:hover, .infoIcon:hover, .expandIcon:hover { + color: #7c7c7c; + } + + .unselectable { + -webkit-touch-callout: none; + -webkit-user-select: none; + -khtml-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + } + +</style> + + + + + diff --git a/scripts/output/comparison/notebook_templates/helper_dashboard.R b/scripts/output/comparison/notebook_templates/helper_dashboard.R new file mode 100644 index 0000000..6fa22c2 --- /dev/null +++ b/scripts/output/comparison/notebook_templates/helper_dashboard.R @@ -0,0 +1,390 @@ +## script that prepares the plots +region_plot = "EUR" + +require(ggplot2) +require(data.table) +require(plotly) +require(moinput) +require(magclass) +require(rmndt) +require(quitte) +require(devtools) + +setConfig(forcecache=T) + +## Aestethics Options +aestethics <- list("alpha"=0.6, + "line" = list("size"= 0.1), + "y-axis" = list("color"="#878787","size"= 1/3.78) +) + +## Legends and colors + +cols <- c("NG" = "#d11141", + "Liquids" = "#8c8c8c", + "Hybrid Liquids" = "#ffc425", + "Hybrid Electric" = "#f37735", + "BEV" = "#00b159", + "Electricity" = "#00b159", + "FCEV" = "#00aedb", + "Hydrogen" = "#00aedb", + "Biodiesel" = "#66a182", + "Synfuel" = "orchid", + "Oil" = "#2e4057", + "International Aviation" = "#9acd32", + "Domestic Aviation" = "#7cfc00", + "Bus" = "#32cd32", + "Passenger Rail" = "#2e8b57", + "Freight Rail" = "#ee4000", + "Trucks" = "#ff6a6a", + "International Shipping" = "#cd2626", + "Domestic Shipping" = "#ff4040", + "Shipping" = "#ff4040", + "Truck" = "#ff7f50", + "Motorbikes" = "#1874cd", + "Small Cars" = "#87cefa", + "Large Cars" = "#6495ed", + "Van" = " #40e0d0", + "LDV" = "#00bfff", + "Non motorized" = "#da70d6", + "Freight"="#ff0000", + "Freight (Inland)" = "#cd5555", + "Pass non LDV" = "#6b8e23", + "Pass" = "#66cdaa", + "Pass non LDV (Domestic)" = "#54ff9f", + "refined liquids enduse" = "#8c8c8c", + "FE|Transport|Hydrogen" = "#00aedb", + "FE|Transport|NG" = "#d11141", + "FE|Transport|Liquids" = "#8c8c8c", + "FE|Transport|Electricity" = "#00b159", + "FE|Transport" = "#1e90ff", + "FE|Buildings" = "#d2b48c", + "FE|Industry" = "#919191", + "ElecEra" = "#00b159", + "ElecEraWise" = "#68c6a4", + "HydrHype" = "#00aedb", + "HydrHypeWise" = "#o3878f", + "Hydrogen_push" = "#00aedb", + "Conservative_liquids" = "#113245", + "ConvCase" = "#113245", + "ConvCaseWise" = "#d11141") + +legend_ord_modes <- c("Freight Rail", "Truck", "Shipping", "International Shipping", "Domestic Shipping", "Trucks", + "Motorbikes", "Small Cars", "Large Cars", "Van", + "International Aviation", "Domestic Aviation","Bus", "Passenger Rail", + "Freight", "LDV", "Pass non LDV", "Freight (Inland)", "Pass non LDV (Domestic)", "Non motorized") + +legend_ord_fuels <- c("BEV", "Electricity", "Hybrid Electric", "FCEV", "Hydrogen", "Hybrid Liquids", "Liquids", "Oil", "Biodiesel", "Synfuel", "NG") + +legend_ord = c(legend_ord_modes, legend_ord_fuels) + +## Load files +EJmode_all = readRDS("EJmode_all.RDS") +EJLDV_all = readRDS("EJLDV_all.RDS") +fleet_all = readRDS("fleet_all.RDS") +salescomp_all = readRDS("salescomp_all.RDS") +ESmodecap_all = readRDS("ESmodecap_all.RDS") +CO2km_int_newsales_all = readRDS("CO2km_int_newsales_all.RDS") +EJfuels_all = readRDS("EJfuels_all.RDS") +emidem_all = readRDS("emidem_all.RDS") + +## scenarios +scens = unique(EJmode_all$scenario) + +## plot functions +vintcomparisondash = function(dt, scen){ + + dt = dt[year %in% c(2015, 2050, 2100)] + dt[, year := as.character(year)] + dt = dt[region == region_plot & scenario == scen] + + plot = ggplot()+ + geom_bar(data = dt, + aes(x=year, y=value, group=interaction(variable, technology), + fill = technology, width=.75), alpha = 0.5, position="stack", stat = "identity", width = 0.5)+ + geom_bar(data = dt, + aes(x=year, y=value, group=interaction(variable, technology), + fill = technology, alpha = factor(alphaval), width=.75), position="stack", stat = "identity", width = 0.5, color = "black", size=0.05)+ + guides(fill = guide_legend(reverse=TRUE))+ + theme_minimal()+ + theme(axis.text.x = element_text(angle = 90, size=8, vjust=0.5, hjust=1), + title = element_text(size=8), + axis.line = element_line(size = 0.5, colour = "grey"), + strip.text = element_text(size=8), + strip.background = element_rect(color = "grey"), + legend.position = "none")+ + scale_alpha_discrete(breaks = c(1,0), name = "Status", labels = c("Vintages","New additions")) + + guides(linetype=FALSE, + fill=guide_legend(reverse=FALSE, title="Transport mode"))+ + scale_fill_manual(values = cols)+ + labs(x = "", y = "") + + + plot = ggplotly(plot) + + vars = as.character(unique(dt$technology)) + + output = list(plot = plot, + vars = vars) +} + +salescomdash = function(dt, scen){ + + dt = dt[region == region_plot & scenario == scen] + + plot = ggplot()+ + geom_bar(data = dt, aes(x=as.numeric(as.character(year)),y = shareFS1, group = technology, fill = technology), position = position_stack(), stat = "identity")+ + theme_minimal()+ + scale_fill_manual("Technology", values = cols)+ + expand_limits(y = c(0,1))+ + scale_x_continuous(breaks = c(2015, 2030, 2050, 2100))+ + theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=1, size = 8), + axis.text.y = element_text(size=8), + axis.line = element_line(size = 0.5, colour = "grey"), + axis.title = element_text(size = 8), + title = element_text(size = 8), + strip.text = element_text(size=8), + strip.background = element_rect(color = "grey"), + legend.position = "none")+ + labs(x = "", y = "") + + plot = ggplotly(plot) + + ## vars used for creating the legend in the dashboard + vars = as.character(unique(dt$technology)) + + output = list(plot = plot, + vars = vars) + + return(output) +} + +ESmodecapdash = function(dt, scen){ + + dt = dt[region == region_plot & scenario == scen] + + 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", size=0.05, position= position_stack())+ + labs(x = "", y = "")+ + theme_minimal()+ + 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))+ + 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.position = "none", + strip.text = element_text(size = 8), + strip.background = element_rect(color = "grey"), + axis.line = element_line(size = 0.5, colour = "grey")) + + 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", size=0.05, position= position_stack())+ + labs(x = "", y = "")+ + theme_minimal()+ + 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))+ + 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.position = "none", + strip.text = element_text(size = 8), + strip.background = element_rect(color = "grey"), + axis.line = element_line(size = 0.5, colour = "grey")) + + plot_pass = ggplotly(plot_pass) + plot_frgt = ggplotly(plot_frgt) + vars_pass = as.character(unique(dt[mode == "pass"]$vehicle_type_plot)) + vars_frgt = as.character(unique(dt[mode == "freight"]$vehicle_type_plot)) + + output = list(plot = list(plot_pass = plot_pass, plot_frgt = plot_frgt), + vars = list(vars_pass = vars_pass, vars_frgt = vars_frgt)) + + return(output) + +} + +EJfuels_dash = function(dt, scen){ + dt = dt[region == region_plot & scenario == scen & year >= 2015] + plot = ggplot()+ + geom_area(data = dt, aes(x=year, y=demand_EJ, group = subtech, fill = subtech), color="black", size=0.05, position= position_stack())+ + theme_minimal()+ + scale_fill_manual("Technology",values = cols, breaks=legend_ord)+ + expand_limits(y = c(0,1))+ + labs(x = "", y = "")+ + scale_x_continuous(breaks = c(2015,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), + title = element_text(size = 8), + legend.position = "none", + strip.text = element_text(size = 8), + strip.background = element_rect(color = "grey"), + axis.line = element_line(size = 0.5, colour = "grey")) + + plot = ggplotly(plot) + + vars = as.character(unique(dt$subtech)) + + output = list(plot = plot, + vars = vars) + + return(output) +} + +CO2km_intensity_newsalesdash = function(dt, scen){ + historical_values = data.table(year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018), emi = c(159, 157, 145, 140, 137, 132, 128, 124, 120, 119, 119, 120)) + targets = data.table(name = c("2021 target", "2025 target", "2030 target"), value = c(95, 95*(1-0.15), 95*(1-0.37))) + + dt = dt[!is.na(gCO2_km_ave) & region == region_plot & scenario == scen] + + plot = ggplot()+ + geom_line(data = dt[year >=2020], aes(x = year, y = gCO2_km_ave))+ + geom_point(data = historical_values, aes(x = year, y= emi), color = "grey20")+ + geom_hline(data = targets, aes(yintercept = value, linetype = name), color = "grey20", size=0.1)+ + geom_text(data = targets, aes(y = value+5, x = c(2025, 2030, 2035), label = name), size = 3)+ + expand_limits(y = c(0,1))+ + labs(x = "", y = "")+ + scale_x_continuous(breaks = c(2015, 2030, 2050, 2100))+ + theme_minimal()+ + 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.position = "none", + strip.text = element_text(size = 8), + strip.background = element_rect(color = "grey"), + axis.line = element_line(size = 0.5, colour = "grey"))+ + guides(linetype = FALSE) + + plot = ggplotly(plot) + + return(plot) +} + +EJLDVdash <- function(dt, scen){ + + dt[, technology := factor(technology, levels = legend_ord)] + dt = dt[region == region_plot & scenario == scen & year >= 2015] + + plot = ggplot()+ + geom_area(data = dt, aes(x=year, y=demand_EJ, group = technology, fill = technology), color="black", size=0.05, position= position_stack())+ + labs(x = "", y = "")+ + theme_minimal()+ + scale_fill_manual("Technology", values = cols, breaks=legend_ord)+ + expand_limits(y = c(0,1))+ + scale_x_continuous(breaks = c(2015,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.position = "none", + strip.text = element_text(size=8), + strip.background = element_rect(color = "grey")) + + plot = ggplotly(plot) + + vars = as.character(unique(dt$technology)) + + output = list(plot = plot, + vars = vars) + + return(output) + +} + +emidem_dash = function(dt, scen){ + dt = dt[region == region_plot & scenario == scen] + + plot = ggplot()+ + geom_line(data = dt, aes(x = year, y = value))+ + labs(x = "", y = "")+ + theme_minimal()+ + expand_limits(y = c(0,1))+ + scale_x_continuous(breaks = c(2015, 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.position = "none", + strip.text = element_text(size=8), + strip.background = element_rect(color = "grey")) + + plot = ggplotly(plot) + + return(plot) + +} + +legend = list() + +create_plotlist = function(scens, salescomp_all, fleet_all, ESmodecap_all, EJfuels_all, CO2km_int_newsales_all, EJLDV_all){ + output = NULL + for (scen in scens) { + + ## attribute scenario name + if (grepl("ConvCase", scen)) { + scenname = "ConvCase" + } else if (grepl("ElecEra", scen)) { + scenname = "ElecEra" + } else if (grepl("HydrHype", scen)) { + scenname = "HydrHype" + } else if (grepl("SynSurge", scen)) { + scenname = "SynSurge" + } + + ## CO2 tax pathway + emiscen = gsub("_.*", "", scen) + + ## sales + salescomp = salescomdash(salescomp_all, scen) + ## vintages + vintcomp = vintcomparisondash(fleet_all, scen) + ## energy services demand + ESmodecap = ESmodecapdash(ESmodecap_all, scen) + ## final energy demand + EJfuels = EJfuels_dash(EJfuels_all, scen) ## Final Energy demand all modes, passenger and freight + ## 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) + ## emissions transport demand + emidem = emidem_dash(emidem_all, scen) + + ## collect plots + output[[scenname]]$plot$vintcomp = vintcomp$plot + output[[scenname]]$plot$salescomp = salescomp$plot + output[[scenname]]$plot$ESmodecap_pass = ESmodecap$plot$plot_pass + output[[scenname]]$plot$ESmodecap_frgt = ESmodecap$plot$plot_frgt + output[[scenname]]$plot$EJfuels = EJfuels$plot + output[[scenname]]$plot$CO2km_int_newsales = CO2km_int_newsales + output[[scenname]]$plot$EJLDV = EJLDV$plot + output[[scenname]]$plot$emidem = emidem + output[[scenname]]$emiscen = emiscen + } + + + legend$'Sales composition'$contents <- lapply(salescomp$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) }) + names(legend$'Sales composition'$contents) <- salescomp$vars + legend$'Per capita Passenger Transport Energy Services Demand'$contents <- lapply(ESmodecap$vars$vars_pass, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) }) + names(legend$'Per capita Passenger Transport Energy Services Demand'$contents) <- ESmodecap$vars$vars_pass + legend$'Per capita Freight Transport Energy Services Demand'$contents <- lapply(ESmodecap$vars$vars_frgt, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) }) + names(legend$'Per capita Freight Transport Energy Services Demand'$contents) <- ESmodecap$vars$vars_frgt + legend$'Final energy LDVs by fuel'$contents <- lapply(EJLDV$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) }) + names(legend$'Final energy LDVs by fuel'$contents) <- EJLDV$vars + legend$'Transport Final Energy Demand'$contents <- lapply(EJfuels$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) }) + names(legend$'Transport Final Energy Demand'$contents) <- EJfuels$vars + legend$'Fleet composition'$contents <- lapply(vintcomp$vars, function(var) { return(list("fill"=toString(cols[var]),"linetype"=NULL)) }) + names(legend$'Fleet composition'$contents) <- vintcomp$vars + + output$legend = legend + return(output) + +} + +plotlist = create_plotlist(scens, salescomp_all, fleet_all, ESmodecap_all, EJfuels_all, CO2km_int_newsales_all, EJLDV_all) -- GitLab