diff --git a/R/dashboard.R b/R/dashboard.R index a989ae2d..bde4bb85 100644 --- a/R/dashboard.R +++ b/R/dashboard.R @@ -2,14 +2,14 @@ #' @description Create REMIND dashboard results for single runs #' #' @author Renato Rodrigues -#' +#' #' @param gdx GDX (fulldata.gdx) file path #' @param statsFile run statistics (runstatistics.rda) file path #' @param regionMapping regionMapping file name or file path (ex. "regionmappingH12.csv") #' @param hist historical mif file path #' @param reportfile REMIND mif report file path #' @param output_file file name to save the html dashboard -#' +#' #' @examples #' \dontrun{ #' # loading required libraries @@ -22,39 +22,46 @@ #' @importFrom rmarkdown render #' #' @export -#' - -dashboard <- function(gdx=NULL, statsFile=NULL, regionMapping=NULL, hist=NULL, reportfile=NULL, output_file=NULL) { +#' +dashboard <- function(gdx = NULL, statsFile = NULL, regionMapping = NULL, + hist = NULL, reportfile = NULL, output_file = NULL) { # error checking - if(!(!(is.null(gdx)) && file.exists(gdx))){ + if (!(!(is.null(gdx)) && file.exists(gdx))) { warning("REMIND dashboard require a valid gdx file path.") return() } - if(!(!(is.null(statsFile)) && file.exists(statsFile))){ + if (!(!(is.null(statsFile)) && file.exists(statsFile))) { warning("REMIND dashboard require a valid run statistics (runstatistics.rda) file path.") return() } - if(is.null(regionMapping)){ + if (is.null(regionMapping)) { warning("REMIND dashboard require a regionMapping file.") return() } - + stats <- NULL load(file = statsFile) - if(stats$config$gms$optimization == "testOneRegi"){ + if (stats$config$gms$optimization == "testOneRegi") { warning("REMIND dashboard does not support yet testOneRegi runs.") return() } - - #dashboard markdown file path - markdownPath <- system.file("markdown","dashboard.Rmd",package = "remind") - - #set output file if null - if(is.null(output_file)) - output_file <- file.path(getwd(),"REMIND_dashboard.html") - - # generate dashboard for REMIND - rmarkdown::render(markdownPath, output_file = output_file, params = list(gdx=gdx, statsFile=statsFile, regionMapping=regionMapping, hist=hist, reportfile=reportfile)) + # dashboard markdown file path + markdownPath <- system.file("markdown", "dashboard.Rmd", package = "remind2") + + # set output file if null + if (is.null(output_file)) { + output_file <- file.path(getwd(), "REMIND_dashboard.html") + } + + # generate dashboard for REMIND + rmarkdown::render(markdownPath, + output_file = output_file, + params = list( + gdx = gdx, statsFile = statsFile, + regionMapping = regionMapping, + hist = hist, reportfile = reportfile + ) + ) } diff --git a/R/reportCharts.R b/R/reportCharts.R index 24cdece0..c1a01842 100644 --- a/R/reportCharts.R +++ b/R/reportCharts.R @@ -27,45 +27,43 @@ #' @importFrom scales pretty_breaks #' #' @export - -reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NULL, chartType="ggplot") { - +reportCharts <- function(gdx = NULL, regionMapping = NULL, hist = NULL, reportfile = NULL, chartType = "ggplot") { # error checking - if(!(!(is.null(gdx)) && file.exists(gdx)) && !(!(is.null(reportfile)) && file.exists(reportfile))){ + if (!(!(is.null(gdx)) && file.exists(gdx)) && !(!(is.null(reportfile)) && file.exists(reportfile))) { warning("reportCharts function require either a valid gdx or a mif report file path.") return() } - if(is.null(regionMapping)){ + if (is.null(regionMapping)) { warning("REMIND dashboard require a regionMapping file.") return() } # if no reporting-mif-file is provided create reporting - if(is.null(reportfile)){ - reportfile <- convGDX2MIF(gdx) + if (is.null(reportfile)) { + data <- convGDX2MIF(gdx) + } else { + data <- read.report(reportfile, as.list = FALSE) } - data <- read.report(reportfile,as.list=FALSE) data <- deletePlus(data) data <- collapseNames(data) - data <- data[,getYears(data)<="y2100",] + data <- data[, getYears(data) <= "y2100", ] ### Aestethics Options - aestethics <- list("alpha"=0.6, - "line" = list("size"= 2/3.78), - "y-axis" = list("color"="#878787","size"= 1/3.78) - ) + aestethics <- list( + "alpha" = 0.6, + "line" = list("size" = 2 / 3.78), + "y-axis" = list("color" = "#878787", "size" = 1 / 3.78) + ) # variable labels renaming for facet grids variable_label <- c( "Emi|CO2" = "Carbon Dioxide (Mt CO2)", "Emi|CH4" = "Methane (Mt CH4)", "Emi|N2O" = "Nitrous Oxide (kt N2O)", - "Emi|PFC" = "Perfluorocarbons (kt CF4-equiv)", "Emi|HFC" = "Hydrofluorocarbons (kt HFC134a-equiv)", "Emi|SF6" = "Sulphur hexafluoride (kt SF6)", - "Emi|NH3" = "NH3 (Mt)", "Emi|NOX" = "NOX (Mt)", "Emi|OC" = "OC (Mt)", @@ -74,22 +72,18 @@ reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NUL "Emi|BC" = "BC (Mt)", "Emi|C2F6" = "C2F6 (kt)", "Emi|C6F14" = "C6F14 (kt C6F14)", - "Price|Final Energy|Buildings" = "Buildings", "Price|Final Energy|Industry" = "Industry", "Price|Final Energy|Transport" = "Transport", - "Price|Final Energy|Electricity|Transport" = "Electricity", "Price|Final Energy|Liquids|Transport" = "Liquids", "Price|Final Energy|Hydrogen|Transport" = "Hydrogen", - "Price|Final Energy|Electricity|Industry" = "Electricity", "Price|Final Energy|Heating Oil|Industry" = "Heating Oil", "Price|Final Energy|Solids|Industry" = "Solids", "Price|Final Energy|Gases|Industry" = "Gases", "Price|Final Energy|Heat|Industry" = "Heat", "Price|Final Energy|Hydrogen|Industry" = "Hydrogen", - "Price|Final Energy|Electricity|Buildings" = "Electricity", "Price|Final Energy|Heat|Buildings" = "Heat", "Price|Final Energy|Gases|Buildings" = "Gases", @@ -99,33 +93,33 @@ reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NUL ) missingColors <- c( - "DEU"=brewer.pal(9,"Oranges")[9], - "EUW"=brewer.pal(9,"YlOrRd")[6], "EWN"=brewer.pal(9,"YlOrRd")[6], "FRA"=brewer.pal(9,"YlOrRd")[7], - "EUS"=brewer.pal(9,"YlOrRd")[2], "ESW"=brewer.pal(9,"YlOrRd")[2], "ESC"=brewer.pal(9,"YlOrRd")[3], - "EUC"=brewer.pal(9,"Greys")[5], "ECS"=brewer.pal(9,"Greys")[3], "ECE"=brewer.pal(9,"Greys")[5], - "EUN"=brewer.pal(9,"Blues")[6], "ENC"=brewer.pal(9,"Blues")[5], "UKI"=brewer.pal(9,"Blues")[6], - "NEU"=brewer.pal(9,"YlGn")[5], "NEN"=brewer.pal(9,"YlGn")[5], "NES"=brewer.pal(9,"YlGn")[3], - "CHE"="#78C679", "ENN"="#78C679", "ESE"="#D9F0A3","EUI"="#78C679", "ROE"="#D9F0A3", #older EU - "SSA"="#00BAFF", "REF"="#D900BC", "CAZ"="#007362", "CHA"="#F24200", #maps - "F-Gases"="#ff9977", "N2O|Waste"="#bcbc6d", "N2O|Industry"="#666666", #Kyoto Gases - "Energy|Supply|Non-Elec"="#661a00", "Energy|Supply|Electricity|Gross"="#993a44", "FFaI|Industry|Process"="#aa5a00", "Energy|Demand|Industry|Gross"="#7777ff", "Buildings|Direct"="#4444bb", "Transport|Demand"="#222288", "Carbon Capture and Storage|Biomass|Neg"="#00aa00", "Land-Use Change"="#116611", # CO2 emissions per sector + "DEU" = brewer.pal(9, "Oranges")[9], + "EUW" = brewer.pal(9, "YlOrRd")[6], "EWN" = brewer.pal(9, "YlOrRd")[6], "FRA" = brewer.pal(9, "YlOrRd")[7], + "EUS" = brewer.pal(9, "YlOrRd")[2], "ESW" = brewer.pal(9, "YlOrRd")[2], "ESC" = brewer.pal(9, "YlOrRd")[3], + "EUC" = brewer.pal(9, "Greys")[5], "ECS" = brewer.pal(9, "Greys")[3], "ECE" = brewer.pal(9, "Greys")[5], + "EUN" = brewer.pal(9, "Blues")[6], "ENC" = brewer.pal(9, "Blues")[5], "UKI" = brewer.pal(9, "Blues")[6], + "NEU" = brewer.pal(9, "YlGn")[5], "NEN" = brewer.pal(9, "YlGn")[5], "NES" = brewer.pal(9, "YlGn")[3], + "CHE" = "#78C679", "ENN" = "#78C679", "ESE" = "#D9F0A3", "EUI" = "#78C679", "ROE" = "#D9F0A3", # older EU + "SSA" = "#00BAFF", "REF" = "#D900BC", "CAZ" = "#007362", "CHA" = "#F24200", # maps + "F-Gases" = "#ff9977", "N2O|Waste" = "#bcbc6d", "N2O|Industry" = "#666666", # Kyoto Gases + "Energy|Supply|Non-Elec" = "#661a00", "Energy|Supply|Electricity|Gross" = "#993a44", "FFaI|Industry|Process" = "#aa5a00", "Energy|Demand|Industry|Gross" = "#7777ff", "Buildings|Direct" = "#4444bb", "Transport|Demand" = "#222288", "Carbon Capture and Storage|Biomass|Neg" = "#00aa00", "Land-Use Change" = "#116611", # CO2 emissions per sector "Emi|CO2" = "#333333", "Emi|CH4" = "#3333aa", "Emi|N2O" = "#aa3333", # Main Greenhouse Gases Emissions "Emi|PFC" = "#000000", "Emi|HFC" = "#000000", "Emi|SF6" = "#000000", # F-Gases "Emi|NH3" = "#000000", "Emi|NOX" = "#000000", "Emi|OC" = "#000000", "Emi|SO2" = "#000000", "Emi|VOC" = "#000000", "Emi|BC" = "#000000", "Emi|C2F6" = "#000000", "Emi|C6F14" = "#000000", # Other Gases "Biomass|World Market" = "#005900", "Coal|Primary Level" = "#0c0c0c", "Natural Gas|Primary Level" = "#999959", "Crude Oil|Primary Level" = "#cc7500", # PE - "Uranium"="EF7676", #Uranium extraction - "Lignocellulosic"="#005900", - "1st Generation"="#008c00", - "Traditional Biomass" = "#000000", #SE Solids - "Industry" = "#000000", "Heating Oil" = "#E41A1C", #FE prices - "Liquids|Fossil" = "#cc7500", "Liquids|Oil" = "#cc7500", "Liquids|Coal" = "#0c0c0c", "Liquids|Biomass" = "#005900", "Solids|Coal" = "#0c0c0c", "Solids|Biomass" = "#005900" #FE + "Uranium" = "EF7676", # Uranium extraction + "Lignocellulosic" = "#005900", + "1st Generation" = "#008c00", + "Traditional Biomass" = "#000000", # SE Solids + "Industry" = "#000000", "Heating Oil" = "#E41A1C", # FE prices + "Liquids|Fossil" = "#cc7500", "Liquids|Oil" = "#cc7500", "Liquids|Coal" = "#0c0c0c", "Liquids|Biomass" = "#005900", "Solids|Coal" = "#0c0c0c", "Solids|Biomass" = "#005900" # FE ) - missingColorsdf <- data.frame(row.names=names(missingColors), color=missingColors) + missingColorsdf <- data.frame(row.names = names(missingColors), color = missingColors) - plotlyButtonsToHide <- list('sendDataToCloud', 'zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d', 'hoverClosestCartesian', 'hoverCompareCartesian', 'zoom3d', 'pan3d', 'orbitRotation', 'tableRotation', 'resetCameraDefault3d', 'resetCameraLastSave3d', 'hoverClosest3d', 'zoomInGeo', 'zoomOutGeo', 'resetGeo', 'hoverClosestGeo', 'hoverClosestGl2d', 'hoverClosestPie', 'resetSankeyGroup', 'toggleHover', 'resetViews', 'toggleSpikelines', 'resetViewMapbox') + plotlyButtonsToHide <- list("sendDataToCloud", "zoom2d", "pan2d", "select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "autoScale2d", "resetScale2d", "hoverClosestCartesian", "hoverCompareCartesian", "zoom3d", "pan3d", "orbitRotation", "tableRotation", "resetCameraDefault3d", "resetCameraLastSave3d", "hoverClosest3d", "zoomInGeo", "zoomOutGeo", "resetGeo", "hoverClosestGeo", "hoverClosestGl2d", "hoverClosestPie", "resetSankeyGroup", "toggleHover", "resetViews", "toggleSpikelines", "resetViewMapbox") ### auxiliar function to format specific text - unitSubscript <- function(unit){ + unitSubscript <- function(unit) { unit <- gsub("CO2", "CO\\2\\<\\/sub\\>", unit) unit <- gsub("CO2 equivalent", "CO\\2 equivalent\\<\\/sub\\>", unit) unit <- gsub("CH4", "CH\\4\\<\\/sub\\>", unit) @@ -146,28 +140,33 @@ reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NUL ### Define which region plots to create gdxRegions <- unique(readGDX(gdx, name = "regi2iso")[[1]]) regionSubsetList <- toolRegionSubsets(gdx) - mainRegions <- c(gdxRegions[!gdxRegions %in% as.vector(unlist(sapply(regionSubsetList,function(x)x)))],names(regionSubsetList)) + mainRegions <- c(gdxRegions[!gdxRegions %in% as.vector(unlist(sapply(regionSubsetList, function(x) x)))], names(regionSubsetList)) - if (is.null(regionSubsetList)){ - regionsList <- c(list("world"="GLO"), - list("regions"=mainRegions)) # main regions + if (is.null(regionSubsetList)) { + regionsList <- c( + list("world" = "GLO"), + list("regions" = mainRegions) + ) # main regions } else { - regionsList <- c(list("world"="GLO"), - list("regions"=mainRegions), # main regions - regionSubsetList, # aggregated regions - list("DEU"="DEU")) #extra country + regionsList <- c( + list("world" = "GLO"), + list("regions" = mainRegions), # main regions + regionSubsetList, # aggregated regions + list("DEU" = "DEU") + ) # extra country } out$data$regions <- regionsList ## loading population info - pop <- readGDX(gdx, name = "pm_pop")[,getYears(data),] # pop in billions - pop <- mbind(pop,dimSums(pop,dim=1)) # adding GLO - if (!is.null(regionSubsetList)) + pop <- readGDX(gdx, name = "pm_pop")[, getYears(data), ] # pop in billions + pop <- mbind(pop, dimSums(pop, dim = 1)) # adding GLO + if (!is.null(regionSubsetList)) { pop <- mbind(pop, calc_regionSubset_sums(pop, regionSubsetList)) + } pop <- as.quitte(pop) - pop <- pop[c("region","period","value")] - colnames(pop) <- c("region","period","population") - pop$population <- pop$population*1e9 + pop <- pop[c("region", "period", "value")] + colnames(pop) <- c("region", "period", "population") + pop$population <- pop$population * 1e9 ### Creating maps @@ -191,18 +190,18 @@ reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NUL # save(europeMap, file="EuropeMap.RData") # loading previosuly created map files - worldMap <- system.file("extdata","WorldMap.RData",package = "remind") - load(worldMap) #updates worldMap object with world map file data - #europeMap <- system.file("extdata","EuropeMap.RData",package = "remind") - #load(europeMap) #updates europeMap object with europe map file data + worldMap <- system.file("extdata", "WorldMap.RData", package = "remind2") + load(worldMap) # updates worldMap object with world map file data + # europeMap <- system.file("extdata","EuropeMap.RData",package = "remind") + # load(europeMap) #updates europeMap object with europe map file data # loading region mapping regionMapping <- toolGetMapping(basename(regionMapping), type = "regional") # adding kosovo - regionMapping <- rbind(regionMapping,data.frame(CountryCode = "KOS", X = "Kosovo", RegionCode = regionMapping[which(regionMapping$CountryCode=="SRB"),]$RegionCode)) + regionMapping <- rbind(regionMapping, data.frame(CountryCode = "KOS", X = "Kosovo", RegionCode = regionMapping[which(regionMapping$CountryCode == "SRB"), ]$RegionCode)) - colors <- plotstyle(unique(regionMapping$RegionCode),unknown=missingColorsdf) # region colors + colors <- plotstyle(unique(regionMapping$RegionCode), unknown = missingColorsdf) # region colors series <- regionMapping %>% group_by(name = .data$RegionCode) %>% @@ -229,340 +228,422 @@ reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NUL # CO2 prices vars <- c("Price|Carbon (US$2005/t CO2)") - color <- c("Price|Carbon"="#000000") #overwritting default mip color value for carbon price + color <- c("Price|Carbon" = "#000000") # overwritting default mip color value for carbon price g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," US$2005/t CO2
CO2 ",ifelse(reg=="GLO","World", as.character(df$region))," carbon price","
year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/t CO2
CO2 ", ifelse(reg == "GLO", "World", as.character(df$region)), " carbon price", "
year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + - expand_limits(y=c(0,100)) + + expand_limits(y = c(0, 100)) + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$emissions$'Carbon Price' <- g + if ("ggplot" %in% chartType) { + out$ggplot$emissions$"Carbon Price" <- g } - if ("plotly" %in% chartType){ - out$plotly$emissions$'Carbon Price' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$emissions$"Carbon Price" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$emissions$'Carbon Price') <- names(regionsList) + names(out$plotly$emissions$"Carbon Price") <- names(regionsList) } - out$legend$'Carbon Price'$description <- "

CO2 emissions price

" - out$legend$'Carbon Price'$contents <- list("CO2 price" =list("fill"=color["Price|Carbon"],"linetype"="solid")) - out$legend$'Carbon Price'$units <- "US$2005/t CO2 per year" + out$legend$"Carbon Price"$description <- "

CO2 emissions price

" + out$legend$"Carbon Price"$contents <- list("CO2 price" = list("fill" = color["Price|Carbon"], "linetype" = "solid")) + out$legend$"Carbon Price"$units <- "US$2005/t CO2 per year" # Kyoto Gases - vars <- c("F-Gases" = "Emi|F-Gases (Mt CO2-equiv/yr)", - "CO2 - Gross Fossil Fuels and Industry" = "Emi|CO2|Gross Fossil Fuels and Industry (Mt CO2/yr)", - "CH4 - Energy Supply and Demand" = "Emi|CH4|Energy Supply and Demand (Mt CH4/yr)", - "N2O - Energy Supply and Demand" = "Emi|N2O|Energy Supply and Demand (kt N2O/yr)", - "N2O - Industry" = "Emi|N2O|Industry (kt N2O/yr)", - "CH4 - Waste" = "Emi|CH4|Waste (Mt CH4/yr)", - "N2O - Waste" = "Emi|N2O|Waste (kt N2O/yr)", - "CH4 - Other" = "Emi|CH4|Other (Mt CH4/yr)", - "CH4 - Land-Use Change" = "Emi|CH4|Land Use (Mt CH4/yr)", - "N2O - Land-Use Change" = "Emi|N2O|Land Use (kt N2O/yr)", - "CO2 - CCS Biomass" = "Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)", - "CO2 - Land-Use Change" = "Emi|CO2|Land-Use Change (Mt CO2/yr)" + # TODO: bring back outdated variables currently commented out + vars <- c( + "F-Gases" = "Emi|GHG|F-Gases (Mt CO2eq/yr)", + # "CO2 - Gross Fossil Fuels and Industry" = "Emi|CO2|Gross Fossil Fuels and Industry (Mt CO2/yr)", + # "CH4 - Energy Supply and Demand" = "Emi|CH4|Energy Supply and Demand (Mt CH4/yr)", + # "N2O - Energy Supply and Demand" = "Emi|N2O|Energy Supply and Demand (kt N2O/yr)", + "N2O - Industry" = "Emi|N2O|Industry (kt N2O/yr)", + "CH4 - Waste" = "Emi|CH4|Waste (Mt CH4/yr)", + "N2O - Waste" = "Emi|N2O|Waste (kt N2O/yr)", + # "CH4 - Other" = "Emi|CH4|Other (Mt CH4/yr)", + "CH4 - Land-Use Change" = "Emi|CH4|Land-Use Change (Mt CH4/yr)", + "N2O - Land-Use Change" = "Emi|N2O|Land-Use Change (kt N2O/yr)", + # "CO2 - CCS Biomass" = "Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)", + "CO2 - Land-Use Change" = "Emi|CO2|Land-Use Change (Mt CO2/yr)" ) - tmpData <- data[,,vars] # converting emissions to CO2 equivalent - GWP <- c("CO2"=1,"CH4"=28,"N2O"=265) - tmpData[,,c("Emi|CO2|Land-Use Change (Mt CO2/yr)","Emi|CO2|Gross Fossil Fuels and Industry (Mt CO2/yr)","Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)")] <- tmpData[,,c("Emi|CO2|Land-Use Change (Mt CO2/yr)","Emi|CO2|Gross Fossil Fuels and Industry (Mt CO2/yr)","Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)")] * GWP["CO2"] - tmpData[,,c("Emi|CH4|Energy Supply and Demand (Mt CH4/yr)","Emi|CH4|Land Use (Mt CH4/yr)","Emi|CH4|Other (Mt CH4/yr)","Emi|CH4|Waste (Mt CH4/yr)")] <- tmpData[,,c("Emi|CH4|Energy Supply and Demand (Mt CH4/yr)","Emi|CH4|Land Use (Mt CH4/yr)","Emi|CH4|Other (Mt CH4/yr)","Emi|CH4|Waste (Mt CH4/yr)")] * GWP["CH4"] - tmpData[,,c("Emi|N2O|Land Use (kt N2O/yr)","Emi|N2O|Energy Supply and Demand (kt N2O/yr)","Emi|N2O|Waste (kt N2O/yr)","Emi|N2O|Industry (kt N2O/yr)")] <- tmpData[,,c("Emi|N2O|Land Use (kt N2O/yr)","Emi|N2O|Energy Supply and Demand (kt N2O/yr)","Emi|N2O|Waste (kt N2O/yr)","Emi|N2O|Industry (kt N2O/yr)")] * GWP["N2O"]/1000 - tmpData[,,c("Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)")] <- -tmpData[,,c("Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)")] - - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + tmpData <- data[, , vars] # converting emissions to CO2 equivalent + GWP <- c("CO2" = 1, "CH4" = 28, "N2O" = 265) + + tmpData[, , c( + "Emi|CO2|Land-Use Change (Mt CO2/yr)" + #"Emi|CO2|Gross Fossil Fuels and Industry (Mt CO2/yr)", + #"Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)" + )] <- + tmpData[, , c( + "Emi|CO2|Land-Use Change (Mt CO2/yr)" + # "Emi|CO2|Gross Fossil Fuels and Industry (Mt CO2/yr)", + # "Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)" + )] * GWP["CO2"] + + tmpData[, , c( + # "Emi|CH4|Energy Supply and Demand (Mt CH4/yr)", + "Emi|CH4|Land-Use Change (Mt CH4/yr)", + # "Emi|CH4|Other (Mt CH4/yr)", + "Emi|CH4|Waste (Mt CH4/yr)" + )] <- + tmpData[, , c( + # "Emi|CH4|Energy Supply and Demand (Mt CH4/yr)", + "Emi|CH4|Land-Use Change (Mt CH4/yr)", + # "Emi|CH4|Other (Mt CH4/yr)", + "Emi|CH4|Waste (Mt CH4/yr)" + )] * GWP["CH4"] + + tmpData[, , c( + "Emi|N2O|Land-Use Change (kt N2O/yr)", + # "Emi|N2O|Energy Supply and Demand (kt N2O/yr)", + "Emi|N2O|Waste (kt N2O/yr)", + "Emi|N2O|Industry (kt N2O/yr)" + )] <- + tmpData[, , c( + "Emi|N2O|Land-Use Change (kt N2O/yr)", + # "Emi|N2O|Energy Supply and Demand (kt N2O/yr)", + "Emi|N2O|Waste (kt N2O/yr)", + "Emi|N2O|Industry (kt N2O/yr)" + )] * GWP["N2O"] / 1000 + + # tmpData[, , c("Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)")] <- -tmpData[, , c("Emi|CO2|Carbon Capture and Storage|Biomass (Mt CO2/yr)")] + + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(tmpData[reg,,vars]) - df$details <- unitSubscript(paste0(round(df$value,2)," Mt CO2 equivalent
CO2 equivalent ",ifelse(reg=="GLO","World", as.character(df$region))," ", gsub("\\|"," emissions by ",gsub("Emi\\|","",df$variable)),"
year: ",df$period)) - g <- ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(tmpData[reg, , vars]) + df$details <- unitSubscript(paste0(round(df$value, 2), " Mt CO2 equivalent
CO2 equivalent ", ifelse(reg == "GLO", "World", as.character(df$region)), " ", gsub("\\|", " emissions by ", gsub("Emi\\|", "", df$variable)), "
year: ", df$period)) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_fill_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$emissions$'Kyoto Gases Emissions' <- g + if ("ggplot" %in% chartType) { + out$ggplot$emissions$"Kyoto Gases Emissions" <- g } - if ("plotly" %in% chartType){ - out$plotly$emissions$'Kyoto Gases Emissions' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) - ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + if ("plotly" %in% chartType) { + out$plotly$emissions$"Kyoto Gases Emissions" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + # TODO: fix bug and bring back? + # for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + # if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { + # ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + # } + # } return(ggplotly) }) - names(out$plotly$emissions$'Kyoto Gases Emissions') <- names(regionsList) + names(out$plotly$emissions$"Kyoto Gases Emissions") <- names(regionsList) } - out$legend$'Kyoto Gases Emissions'$description <- "

Kyoto Gases Emissions

" - out$legend$'Kyoto Gases Emissions'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Kyoto Gases Emissions'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Kyoto Gases Emissions'$units <- "US$2005/t CO2 per year" + out$legend$"Kyoto Gases Emissions"$description <- "

Kyoto Gases Emissions

" + out$legend$"Kyoto Gases Emissions"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Kyoto Gases Emissions"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Kyoto Gases Emissions"$units <- "US$2005/t CO2 per year" # CO2 emissions per sector - vars <- c("Energy - Non-electricity" = "Emi|CO2|Energy|Supply|Non-Elec (Mt CO2/yr)", - "Energy - Electricity" = "Emi|CO2|Energy|Supply|Electricity|Gross (Mt CO2/yr)", - "Industrial Processes" = "Emi|CO2|FFaI|Industry|Process (Mt CO2/yr)", - "Demand - Industry" = "Emi|CO2|Energy|Demand|Industry|Gross (Mt CO2/yr)", - # "Emi|CO2|Industrial Processes (Mt CO2/yr)", - "Demand - Buildings" = "Emi|CO2|Buildings|Direct (Mt CO2/yr)", - "Demand - Transport" = "Emi|CO2|Transport|Demand (Mt CO2/yr)", - "Land use change" = "Emi|CO2|Carbon Capture and Storage|Biomass|Neg (Mt CO2/yr)", - "Carbon Capture and Storage - Biomass" = "Emi|CO2|Land-Use Change (Mt CO2/yr)") - - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + vars <- c( + "Energy - Non-electricity" = "Emi|CO2|Energy|Supply|Non-electric (Mt CO2/yr)", + "Energy - Electricity" = "Emi|CO2|Energy|Supply|Electricity|Gross (Mt CO2/yr)", + "Industrial Processes" = "Emi|CO2|FFaI|Industry|Process (Mt CO2/yr)", + "Demand - Industry" = "Emi|CO2|Energy|Demand|Industry|Gross (Mt CO2/yr)", + # "Emi|CO2|Industrial Processes (Mt CO2/yr)", + "Demand - Buildings" = "Emi|CO2|Buildings|Direct (Mt CO2/yr)", + "Demand - Transport" = "Emi|CO2|Transport|Demand (Mt CO2/yr)", + "Land use change" = "Emi|CO2|Carbon Capture and Storage|Biomass|Neg (Mt CO2/yr)", + "Carbon Capture and Storage - Biomass" = "Emi|CO2|Land-Use Change (Mt CO2/yr)" + ) + + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- unitSubscript(paste0(round(df$value,2)," Mt CO2
CO2 ",ifelse(reg=="GLO","World", as.character(df$region))," emissions by ",names(vars[df$variable]),"
year: ",df$period)) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= unitSubscript(paste0(round(.data$value,2)," Mt CO2
","Total carbon emissions in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period))) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- unitSubscript(paste0(round(df$value, 2), " Mt CO2
CO2 ", ifelse(reg == "GLO", "World", as.character(df$region)), " emissions by ", names(vars[df$variable]), "
year: ", df$period)) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = unitSubscript(paste0(round(.data$value, 2), " Mt CO2
", "Total carbon emissions in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period))) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$emissions$'CO2 Emissions per Sector' <- g + if ("ggplot" %in% chartType) { + out$ggplot$emissions$"CO2 Emissions per Sector" <- g } - if ("plotly" %in% chartType){ - out$plotly$emissions$'CO2 Emissions per Sector' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$emissions$"CO2 Emissions per Sector" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$emissions$'CO2 Emissions per Sector') <- names(regionsList) + names(out$plotly$emissions$"CO2 Emissions per Sector") <- names(regionsList) } - out$legend$'CO2 Emissions per Sector'$description <- "

Annual CO2 emissions per sector

" - out$legend$'CO2 Emissions per Sector'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'CO2 Emissions per Sector'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'CO2 Emissions per Sector'$contents <- c(list("Total CO2 emissions" =list("fill"="#000","linetype"="dashed")),out$legend$'CO2 Emissions per Sector'$contents) - out$legend$'CO2 Emissions per Sector'$units <- "Mt CO2 per year" + out$legend$"CO2 Emissions per Sector"$description <- "

Annual CO2 emissions per sector

" + out$legend$"CO2 Emissions per Sector"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"CO2 Emissions per Sector"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"CO2 Emissions per Sector"$contents <- c(list("Total CO2 emissions" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"CO2 Emissions per Sector"$contents) + out$legend$"CO2 Emissions per Sector"$units <- "Mt CO2 per year" # Main Greenhouse Gases Emissions - vars <- c("Emi|CO2 (Mt CO2/yr)", - "Emi|CH4 (Mt CH4/yr)", - "Emi|N2O (kt N2O/yr)") + vars <- c( + "Emi|CO2 (Mt CO2/yr)", + "Emi|CH4 (Mt CH4/yr)", + "Emi|N2O (kt N2O/yr)" + ) g <- lapply(regionsList, function(reg) { - if (length(reg) == 1) # color as variable - color <- plotstyle(as.character(gsub(" \\(.*","",vars)),unknown=missingColorsdf) - else # color as region - color <- plotstyle(unique(reg),unknown=missingColorsdf) - df <- as.quitte(data[reg,,vars]) - df$details <- unitSubscript(paste0(round(df$value,2), " ", gsub("/yr","",df$unit),"
",gsub(".* ","",gsub("/yr","",df$unit))," ",ifelse(reg=="GLO","World", as.character(df$region))," emissions", "
","year: ",df$period)) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=as.formula(paste("~",ifelse(length(reg) == 1,"variable","region"))),text=~details, group = ~region)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~variable, ncol=1, scales="free_y",labeller=labeller(variable = unitSubscript(variable_label))) + + if (length(reg) == 1) { # color as variable + color <- plotstyle(as.character(gsub(" \\(.*", "", vars)), unknown = missingColorsdf) + } else { # color as region + color <- plotstyle(unique(reg), unknown = missingColorsdf) + } + df <- as.quitte(data[reg, , vars]) + df$details <- unitSubscript(paste0(round(df$value, 2), " ", gsub("/yr", "", df$unit), "
", gsub(".* ", "", gsub("/yr", "", df$unit)), " ", ifelse(reg == "GLO", "World", as.character(df$region)), " emissions", "
", "year: ", df$period)) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = as.formula(paste("~", ifelse(length(reg) == 1, "variable", "region"))), text = ~details, group = ~region)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~variable, ncol = 1, scales = "free_y", labeller = labeller(variable = unitSubscript(variable_label))) + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$emissions$'Main Greenhouse Gases Emissions' <- g + if ("ggplot" %in% chartType) { + out$ggplot$emissions$"Main Greenhouse Gases Emissions" <- g } - if ("plotly" %in% chartType){ - out$plotly$emissions$'Main Greenhouse Gases Emissions' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$emissions$"Main Greenhouse Gases Emissions" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$emissions$'Main Greenhouse Gases Emissions') <- names(regionsList) + names(out$plotly$emissions$"Main Greenhouse Gases Emissions") <- names(regionsList) } - #out$legend$'Main Greenhouse Gases Emissions'$description <- "

Annual emissions for main greenhouse gases

" - #out$legend$'Main Greenhouse Gases Emissions'$contents <- "placeholder - not needed" + # out$legend$'Main Greenhouse Gases Emissions'$description <- "

Annual emissions for main greenhouse gases

" + # out$legend$'Main Greenhouse Gases Emissions'$contents <- "placeholder - not needed" # F-Gases - vars <- c("Emi|PFC (kt CF4-equiv/yr)", - "Emi|HFC (kt HFC134a-equiv/yr)", - "Emi|SF6 (kt SF6/yr)") + vars <- c( + "Emi|PFC (kt CF4-equiv/yr)", + "Emi|HFC (kt HFC134a-equiv/yr)", + "Emi|SF6 (kt SF6/yr)" + ) g <- lapply(regionsList, function(reg) { - if (length(reg) == 1) # color as variable - color <- plotstyle(as.character(gsub(" \\(.*","",vars)),unknown=missingColorsdf) - else # color as region - color <- plotstyle(unique(reg),unknown=missingColorsdf) - df <- as.quitte(data[reg,,vars]) - df$details <- unitSubscript(paste0(round(df$value,2), " ", gsub("/yr","",df$unit),"
",gsub(".* ","",gsub("/yr","",df$unit))," ",ifelse(reg=="GLO","World", as.character(df$region))," emissions", "
","year: ",df$period)) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=as.formula(paste("~",ifelse(length(reg) == 1,"variable","region"))),text=~details, group = ~region)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~variable, ncol=1, scales="free_y",labeller=labeller(variable = unitSubscript(variable_label))) + + if (length(reg) == 1) { # color as variable + color <- plotstyle(as.character(gsub(" \\(.*", "", vars)), unknown = missingColorsdf) + } else { # color as region + color <- plotstyle(unique(reg), unknown = missingColorsdf) + } + df <- as.quitte(data[reg, , vars]) + df$details <- unitSubscript(paste0(round(df$value, 2), " ", gsub("/yr", "", df$unit), "
", gsub(".* ", "", gsub("/yr", "", df$unit)), " ", ifelse(reg == "GLO", "World", as.character(df$region)), " emissions", "
", "year: ", df$period)) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = as.formula(paste("~", ifelse(length(reg) == 1, "variable", "region"))), text = ~details, group = ~region)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~variable, ncol = 1, scales = "free_y", labeller = labeller(variable = unitSubscript(variable_label))) + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$emissions$'F-Gases' <- g + if ("ggplot" %in% chartType) { + out$ggplot$emissions$"F-Gases" <- g } - if ("plotly" %in% chartType){ - out$plotly$emissions$'F-Gases' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$emissions$"F-Gases" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$emissions$'F-Gases') <- names(regionsList) + names(out$plotly$emissions$"F-Gases") <- names(regionsList) } - #out$legend$'F-Gases'$description <- "

placeholder - not needed

" - #out$legend$'F-Gases'$contents <- "placeholder - not needed" + # out$legend$'F-Gases'$description <- "

placeholder - not needed

" + # out$legend$'F-Gases'$contents <- "placeholder - not needed" # Other Gases - vars <- c("Emi|NH3 (Mt NH3/yr)", - "Emi|NOX (Mt NOX/yr)", - "Emi|OC (Mt OC/yr)", - "Emi|SO2 (Mt SO2/yr)", - "Emi|VOC (Mt VOC/yr)", - "Emi|BC (Mt BC/yr)", - "Emi|C2F6 (kt C2F6/yr)", - "Emi|C6F14 (kt C6F14/yr)") + vars <- c( + "Emi|NH3 (Mt NH3/yr)", + "Emi|NOX (Mt NOX/yr)", + "Emi|OC (Mt OC/yr)", + "Emi|SO2 (Mt SO2/yr)", + "Emi|VOC (Mt VOC/yr)", + "Emi|BC (Mt BC/yr)", + "Emi|C2F6 (kt C2F6/yr)", + "Emi|C6F14 (kt C6F14/yr)" + ) g <- lapply(regionsList, function(reg) { - if (length(reg) == 1) # color as variable - color <- plotstyle(as.character(gsub(" \\(.*","",vars)),unknown=missingColorsdf) - else # color as region - color <- plotstyle(unique(reg),unknown=missingColorsdf) - df <- as.quitte(data[reg,,vars]) - df$details <- unitSubscript(paste0(round(df$value,2), " ", gsub("/yr","",df$unit),"
",gsub(".* ","",gsub("/yr","",df$unit))," ",ifelse(reg=="GLO","World", as.character(df$region))," emissions", "
","year: ",df$period)) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=as.formula(paste("~",ifelse(length(reg) == 1,"variable","region"))),text=~details, group = ~region)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year + if (length(reg) == 1) { # color as variable + color <- plotstyle(as.character(gsub(" \\(.*", "", vars)), unknown = missingColorsdf) + } else { # color as region + color <- plotstyle(unique(reg), unknown = missingColorsdf) + } + df <- as.quitte(data[reg, , vars]) + df$details <- unitSubscript(paste0(round(df$value, 2), " ", gsub("/yr", "", df$unit), "
", gsub(".* ", "", gsub("/yr", "", df$unit)), " ", ifelse(reg == "GLO", "World", as.character(df$region)), " emissions", "
", "year: ", df$period)) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = as.formula(paste("~", ifelse(length(reg) == 1, "variable", "region"))), text = ~details, group = ~region)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year scale_y_continuous(breaks = pretty_breaks(2), limits = c(0, NA)) + - facet_wrap(~variable, ncol=1, scales="free_y",labeller=labeller(variable = unitSubscript(variable_label))) + + facet_wrap(~variable, ncol = 1, scales = "free_y", labeller = labeller(variable = unitSubscript(variable_label))) + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$emissions$'Other Gases' <- g + if ("ggplot" %in% chartType) { + out$ggplot$emissions$"Other Gases" <- g } - if ("plotly" %in% chartType){ - out$plotly$emissions$'Other Gases' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$emissions$"Other Gases" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$emissions$'Other Gases') <- names(regionsList) + names(out$plotly$emissions$"Other Gases") <- names(regionsList) } - #out$legend$'Other Gases'$description <- "

placeholder - not needed

" - #out$legend$'Other Gases'$contents <- "placeholder - not needed" + # out$legend$'Other Gases'$description <- "

placeholder - not needed

" + # out$legend$'Other Gases'$contents <- "placeholder - not needed" ##### PRIMARY ENERGY - #var.tot <-"PE (EJ/yr)" - vars <- c("Coal with CCS" = "PE|Coal|w/ CC (EJ/yr)", - "Coal without CCS" = "PE|Coal|w/o CC (EJ/yr)", - "Oil" = "PE|Oil (EJ/yr)", - "Gas with CCS" = "PE|Gas|w/ CC (EJ/yr)", - "Gas without CCS" = "PE|Gas|w/o CC (EJ/yr)", - "Biomass with CCS" = "PE|Biomass|w/ CC (EJ/yr)", - "Biomass without CCS" = "PE|Biomass|w/o CC (EJ/yr)", - "Nuclear" = "PE|Nuclear (EJ/yr)", - "Hydro" = "PE|Hydro (EJ/yr)", - "Geothermal" = "PE|Geothermal (EJ/yr)", - "Solar" = "PE|Solar (EJ/yr)", - "Wind" = "PE|Wind (EJ/yr)") + # var.tot <-"PE (EJ/yr)" + vars <- c( + "Coal with CCS" = "PE|Coal|w/ CC (EJ/yr)", + "Coal without CCS" = "PE|Coal|w/o CC (EJ/yr)", + "Oil" = "PE|Oil (EJ/yr)", + "Gas with CCS" = "PE|Gas|w/ CC (EJ/yr)", + "Gas without CCS" = "PE|Gas|w/o CC (EJ/yr)", + "Biomass with CCS" = "PE|Biomass|w/ CC (EJ/yr)", + "Biomass without CCS" = "PE|Biomass|w/o CC (EJ/yr)", + "Nuclear" = "PE|Nuclear (EJ/yr)", + "Hydro" = "PE|Hydro (EJ/yr)", + "Geothermal" = "PE|Geothermal (EJ/yr)", + "Solar" = "PE|Solar (EJ/yr)", + "Wind" = "PE|Wind (EJ/yr)" + ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",gsub("\\|"," ",gsub("\\+\\|","",gsub("PE\\|","",as.character(df$variable)))), " primary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", gsub("\\|", " ", gsub("\\+\\|", "", gsub("PE\\|", "", as.character(df$variable)))), " primary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) g <- list() # absolute chart - g$abs <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total primary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) + g$abs <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total primary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) # relative chart - df <- df %>% group_by(.data$region,.data$period) %>% mutate(percent = .data$value/sum(.data$value)*100) #creating percentage column - df$percDetails <- paste0(round(df$percent,2)," %
",gsub("\\|"," ",gsub("\\+\\|","",gsub("PE\\|","",as.character(df$variable)))), " primary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g$perc <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~percDetails,group = ~variable)) + - geom_bar(position = "fill",stat = "identity",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) + - scale_y_continuous(labels = scales::percent) + - scale_x_continuous(limits = c(2005, NA)) ) - #per capita chart - df <- suppressWarnings(left_join(df, pop, by=c("region", "period"))) - df <- df %>% group_by(.data$region,.data$period) %>% mutate(percapita = .data$value/.data$population) #creating per capita column - df$percapitaDetails <- paste0(round(df$percapita,2)," EJ per capita
",gsub("\\|"," ",gsub("\\+\\|","",gsub("PE\\|","",as.character(df$variable)))), " primary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g$percapita <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~percapita,fill=~variable,text=~percapitaDetails,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(percapita = sum(.data$percapita,na.rm=TRUE)) %>% - mutate(position= .data$percapita+max(.data$percapita)/1000) %>% - mutate(details= paste0(round(.data$percapita,2)," EJ per capita
","Total primary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1){ + df <- df %>% + group_by(.data$region, .data$period) %>% + mutate(percent = .data$value / sum(.data$value) * 100) # creating percentage column + df$percDetails <- paste0(round(df$percent, 2), " %
", gsub("\\|", " ", gsub("\\+\\|", "", gsub("PE\\|", "", as.character(df$variable)))), " primary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g$perc <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~percDetails, group = ~variable)) + + geom_bar(position = "fill", stat = "identity", alpha = aestethics$alpha) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color) + + scale_y_continuous(labels = scales::percent) + + scale_x_continuous(limits = c(2005, NA))) + # per capita chart + df <- suppressWarnings(left_join(df, pop, by = c("region", "period"))) + df <- df %>% + group_by(.data$region, .data$period) %>% + mutate(percapita = .data$value / .data$population) # creating per capita column + df$percapitaDetails <- paste0(round(df$percapita, 2), " EJ per capita
", gsub("\\|", " ", gsub("\\+\\|", "", gsub("PE\\|", "", as.character(df$variable)))), " primary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g$percapita <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~percapita, fill = ~variable, text = ~percapitaDetails, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(percapita = sum(.data$percapita, na.rm = TRUE)) %>% + mutate(position = .data$percapita + max(.data$percapita) / 1000) %>% + mutate(details = paste0(round(.data$percapita, 2), " EJ per capita
", "Total primary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g$abs <- g$abs + theme(strip.text.x = element_blank()) g$perc <- g$perc + theme(strip.text.x = element_blank()) g$percapita <- g$percapita + theme(strip.text.x = element_blank()) @@ -570,1168 +651,1367 @@ reportCharts <- function(gdx=NULL, regionMapping=NULL, hist=NULL, reportfile=NUL return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Total Primary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Total Primary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Total Primary Energy' <- lapply(names(regionsList), function(reg) { + if ("plotly" %in% chartType) { + out$plotly$PE$"Total Primary Energy" <- lapply(names(regionsList), function(reg) { ggplotly <- lapply(names(g[[reg]]), function(type) { - gg <- ggplotly(g[[reg]][[type]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + gg <- ggplotly(g[[reg]][[type]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(gg) }) names(ggplotly) <- names(g[[reg]]) return(ggplotly) }) - names(out$plotly$PE$'Total Primary Energy') <- names(regionsList) + names(out$plotly$PE$"Total Primary Energy") <- names(regionsList) } - out$legend$'Total Primary Energy'$description <- "

Primary energy per carrier

" - out$legend$'Total Primary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Total Primary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Total Primary Energy'$contents <- c(list("Total primary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Total Primary Energy'$contents) - out$legend$'Total Primary Energy'$units <- c("EJ per year","Percentage share","EJ per capita") + out$legend$"Total Primary Energy"$description <- "

Primary energy per carrier

" + out$legend$"Total Primary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Total Primary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Total Primary Energy"$contents <- c(list("Total primary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Total Primary Energy"$contents) + out$legend$"Total Primary Energy"$units <- c("EJ per year", "Percentage share", "EJ per capita") # PE prices - vars <- c("Biomass - World Market" = "Price|Biomass|World Market (US$2005/GJ)", - "Coal - Primary Level" = "Price|Coal|Primary Level (US$2005/GJ)", - "Natural Gas - Primary Level" = "Price|Natural Gas|Primary Level (US$2005/GJ)", - "Crude Oil - Primary Level"="Price|Crude Oil|Primary Level (US$2005/GJ)") + vars <- c( + "Biomass - World Market" = "Price|Biomass|World Market (US$2005/GJ)", + "Coal - Primary Level" = "Price|Coal|Primary Level (US$2005/GJ)", + "Natural Gas - Primary Level" = "Price|Natural Gas|Primary Level (US$2005/GJ)", + "Crude Oil - Primary Level" = "Price|Crude Oil|Primary Level (US$2005/GJ)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2), " US$2005/GJ
",gsub("\\|","",regmatches(df$variable, gregexpr("\\|.*?\\|", df$variable))), " ", ifelse(reg=="GLO","World", as.character(df$region))," price","
year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color,alpha=aestethics$alpha) + # vertical line at initial year - facet_wrap(~region, ncol=3, scales="free") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/GJ
", gsub("\\|", "", regmatches(df$variable, gregexpr("\\|.*?\\|", df$variable))), " ", ifelse(reg == "GLO", "World", as.character(df$region)), " price", "
year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color, alpha = aestethics$alpha) + # vertical line at initial year + facet_wrap(~region, ncol = 3, scales = "free") + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Primary Energy Prices' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Primary Energy Prices" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Primary Energy Prices' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$PE$"Primary Energy Prices" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$PE$'Primary Energy Prices') <- names(regionsList) + names(out$plotly$PE$"Primary Energy Prices") <- names(regionsList) } - out$legend$'Primary Energy Prices'$description <- "

Primary energy prices

" - out$legend$'Primary Energy Prices'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"="solid")) }) - names(out$legend$'Primary Energy Prices'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Primary Energy Prices'$units <- "US$2005/GJ per year" + out$legend$"Primary Energy Prices"$description <- "

Primary energy prices

" + out$legend$"Primary Energy Prices"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = "solid")) + }) + names(out$legend$"Primary Energy Prices"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Primary Energy Prices"$units <- "US$2005/GJ per year" # Coal production vars <- c("PE|Production|Net|Coal (EJ/yr)") color <- plotstyle("Coal") - names(color) <- gsub(" \\(.*","",vars) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2),"
", ifelse(reg=="GLO","World", as.character(df$region)), " Coal production","
year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), "
", ifelse(reg == "GLO", "World", as.character(df$region)), " Coal production", "
year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_fill_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Coal production' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Coal production" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Coal production' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$PE$"Coal production" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.02 + } + } return(ggplotly) }) - names(out$plotly$PE$'Coal production') <- names(regionsList) + names(out$plotly$PE$"Coal production") <- names(regionsList) } - out$legend$'Coal production'$description <- "

Net Coal production.

" - out$legend$'Coal production'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"=NULL)) }) - names(out$legend$'Coal production'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Coal production'$units <- "EJ/yr" + out$legend$"Coal production"$description <- "

Net Coal production.

" + out$legend$"Coal production"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = NULL)) + }) + names(out$legend$"Coal production"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Coal production"$units <- "EJ/yr" # Gas production vars <- c("PE|Production|Net|Gas (EJ/yr)") color <- "#999959" - names(color) <- gsub(" \\(.*","",vars) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2),"
", ifelse(reg=="GLO","World", as.character(df$region)), " Gas production","
year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), "
", ifelse(reg == "GLO", "World", as.character(df$region)), " Gas production", "
year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_fill_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Gas production' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Gas production" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Gas production' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$PE$"Gas production" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.02 + } + } return(ggplotly) }) - names(out$plotly$PE$'Gas production') <- names(regionsList) + names(out$plotly$PE$"Gas production") <- names(regionsList) } - out$legend$'Gas production'$description <- "

Net Gas production.

" - out$legend$'Gas production'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"=NULL)) }) - names(out$legend$'Gas production'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Gas production'$units <- "EJ/yr" + out$legend$"Gas production"$description <- "

Net Gas production.

" + out$legend$"Gas production"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = NULL)) + }) + names(out$legend$"Gas production"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Gas production"$units <- "EJ/yr" # Oil production vars <- c("PE|Production|Net|Oil (EJ/yr)") color <- "#cc7500" - names(color) <- gsub(" \\(.*","",vars) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2),"
", ifelse(reg=="GLO","World", as.character(df$region)), " Oil production","
year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), "
", ifelse(reg == "GLO", "World", as.character(df$region)), " Oil production", "
year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_fill_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Oil production' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Oil production" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Oil production' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$PE$"Oil production" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.02 + } + } return(ggplotly) }) - names(out$plotly$PE$'Oil production') <- names(regionsList) + names(out$plotly$PE$"Oil production") <- names(regionsList) } - out$legend$'Oil production'$description <- "

Net Oil production.

" - out$legend$'Oil production'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"=NULL)) }) - names(out$legend$'Oil production'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Oil production'$units <- "EJ/yr" + out$legend$"Oil production"$description <- "

Net Oil production.

" + out$legend$"Oil production"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = NULL)) + }) + names(out$legend$"Oil production"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Oil production"$units <- "EJ/yr" # Uranium production vars <- c("PE|Production|Net|Uranium [Energy] (EJ/yr)") color <- plotstyle("Uranium") - names(color) <- gsub(" \\(.*","",vars) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2),"
", ifelse(reg=="GLO","World", as.character(df$region)), " Uranium production","
year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), "
", ifelse(reg == "GLO", "World", as.character(df$region)), " Uranium production", "
year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_fill_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Uranium production' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Uranium production" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Uranium production' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$PE$"Uranium production" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.02 + } + } return(ggplotly) }) - names(out$plotly$PE$'Uranium production') <- names(regionsList) + names(out$plotly$PE$"Uranium production") <- names(regionsList) } - out$legend$'Uranium production'$description <- "

Net Uranium production.

" - out$legend$'Uranium production'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"=NULL)) }) - names(out$legend$'Uranium production'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Uranium production'$units <- "EJ/yr" + out$legend$"Uranium production"$description <- "

Net Uranium production.

" + out$legend$"Uranium production"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = NULL)) + }) + names(out$legend$"Uranium production"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Uranium production"$units <- "EJ/yr" # Biomass production - vars <- c("PE|Production|Biomass|Lignocellulosic (EJ/yr)", - "PE|Production|Biomass|1st Generation (EJ/yr)") + vars <- c( + "PE|Production|Biomass|Lignocellulosic (EJ/yr)", + "PE|Production|Biomass|1st Generation (EJ/yr)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ/yr
", ifelse(reg=="GLO","World", as.character(df$region)),gsub(".*\\|","",gsub(" \\(.*","",df$variable)), " Biomass production
", "year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ/yr
", ifelse(reg == "GLO", "World", as.character(df$region)), gsub(".*\\|", "", gsub(" \\(.*", "", df$variable)), " Biomass production
", "year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_fill_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$PE$'Biomass production' <- g + if ("ggplot" %in% chartType) { + out$ggplot$PE$"Biomass production" <- g } - if ("plotly" %in% chartType){ - out$plotly$PE$'Biomass production' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) - ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.02 - return(ggplotly) + if ("plotly" %in% chartType) { + out$plotly$PE$"Biomass production" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { + ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.02 + } + } + return(ggplotly) }) - names(out$plotly$PE$'Biomass production') <- names(regionsList) + names(out$plotly$PE$"Biomass production") <- names(regionsList) } - out$legend$'Biomass production'$description <- "

Energy crops Biomass production.

" - out$legend$'Biomass production'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"=NULL)) }) - names(out$legend$'Biomass production'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Biomass production'$units <- "EJ/yr" + out$legend$"Biomass production"$description <- "

Energy crops Biomass production.

" + out$legend$"Biomass production"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = NULL)) + }) + names(out$legend$"Biomass production"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Biomass production"$units <- "EJ/yr" ##### SECONDARY ENERGY # SE price - vars <- c("Price|Secondary Energy|Biomass (US$2005/GJ)", - "Price|Secondary Energy|Electricity (US$2005/GJ)", - "Price|Secondary Energy|Gases (US$2005/GJ)", - "Price|Secondary Energy|Heat (US$2005/GJ)", - "Price|Secondary Energy|Hydrogen (US$2005/GJ)", - "Price|Secondary Energy|Liquids (US$2005/GJ)", - "Price|Secondary Energy|Solids (US$2005/GJ)") + vars <- c( + "Price|Secondary Energy|Biomass (US$2005/GJ)", + "Price|Secondary Energy|Electricity (US$2005/GJ)", + "Price|Secondary Energy|Gases (US$2005/GJ)", + "Price|Secondary Energy|Heat (US$2005/GJ)", + "Price|Secondary Energy|Hydrogen (US$2005/GJ)", + "Price|Secondary Energy|Liquids (US$2005/GJ)", + "Price|Secondary Energy|Solids (US$2005/GJ)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2), " US$2005/GJ
",ifelse(reg=="GLO","World", as.character(df$region))," ",gsub("Price\\|Secondary Energy\\|","",df$variable), " price
", "year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, ncol=3, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/GJ
", ifelse(reg == "GLO", "World", as.character(df$region)), " ", gsub("Price\\|Secondary Energy\\|", "", df$variable), " price
", "year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, ncol = 3, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Secondary Energy Prices' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Secondary Energy Prices" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Secondary Energy Prices' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$SE$"Secondary Energy Prices" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$SE$'Secondary Energy Prices') <- names(regionsList) + names(out$plotly$SE$"Secondary Energy Prices") <- names(regionsList) } - out$legend$'Secondary Energy Prices'$description <- "

Secondary energy prices

" - out$legend$'Secondary Energy Prices'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"="solid")) }) - names(out$legend$'Secondary Energy Prices'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Secondary Energy Prices'$units <- "US$2005/GJ per year" - - #SE quantity - vars <- c("SE|Electricity (EJ/yr)", - "SE|Gases (EJ/yr)", - "SE|Heat (EJ/yr)", - "SE|Hydrogen (EJ/yr)", - "SE|Liquids (EJ/yr)", - "SE|Solids (EJ/yr)") + out$legend$"Secondary Energy Prices"$description <- "

Secondary energy prices

" + out$legend$"Secondary Energy Prices"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = "solid")) + }) + names(out$legend$"Secondary Energy Prices"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Secondary Energy Prices"$units <- "US$2005/GJ per year" + + # SE quantity + vars <- c( + "SE|Electricity (EJ/yr)", + "SE|Gases (EJ/yr)", + "SE|Heat (EJ/yr)", + "SE|Hydrogen (EJ/yr)", + "SE|Liquids (EJ/yr)", + "SE|Solids (EJ/yr)" + ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",gsub("SE\\|","",as.character(df$variable)), " secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", gsub("SE\\|", "", as.character(df$variable)), " secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Total Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Total Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Total Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) + if ("plotly" %in% chartType) { + out$plotly$SE$"Total Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) return(ggplotly) }) - names(out$plotly$SE$'Total Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Total Secondary Energy") <- names(regionsList) } - out$legend$'Total Secondary Energy'$description <- "

Secondary energy per carrier

" - out$legend$'Total Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Total Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Total Secondary Energy'$contents <- c(list("Total secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Total Secondary Energy'$contents) - out$legend$'Total Secondary Energy'$units <- "EJ per year" + out$legend$"Total Secondary Energy"$description <- "

Secondary energy per carrier

" + out$legend$"Total Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Total Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Total Secondary Energy"$contents <- c(list("Total secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Total Secondary Energy"$contents) + out$legend$"Total Secondary Energy"$units <- "EJ per year" ### SE Electricity - #var.tot <-"SE|Electricity (EJ/yr)" - vars <- c("Coal with CC" = "SE|Electricity|Coal|w/ CC (EJ/yr)", - "Coal without CC" = "SE|Electricity|Coal|w/o CC (EJ/yr)", - "Oil" = "SE|Electricity|Oil (EJ/yr)", - "Gas with CC" = "SE|Electricity|Gas|w/ CC (EJ/yr)", - "Gas without CC" = "SE|Electricity|Gas|w/o CC (EJ/yr)", - "Biomass with CC" = "SE|Electricity|Biomass|w/ CC (EJ/yr)", - "Biomass without CC" = "SE|Electricity|Biomass|w/o CC (EJ/yr)", - "Nuclear" = "SE|Electricity|Nuclear (EJ/yr)", - "Hydrogen" = "SE|Electricity|Hydrogen (EJ/yr)", - "Solar" = "SE|Electricity|Solar (EJ/yr)", - "Wind" = "SE|Electricity|Wind (EJ/yr)", - "Geothermal" = "SE|Electricity|Geothermal (EJ/yr)", - "Hydro" = "SE|Electricity|Hydro (EJ/yr)" + # var.tot <-"SE|Electricity (EJ/yr)" + vars <- c( + "Coal with CC" = "SE|Electricity|Coal|w/ CC (EJ/yr)", + "Coal without CC" = "SE|Electricity|Coal|w/o CC (EJ/yr)", + "Oil" = "SE|Electricity|Oil (EJ/yr)", + "Gas with CC" = "SE|Electricity|Gas|w/ CC (EJ/yr)", + "Gas without CC" = "SE|Electricity|Gas|w/o CC (EJ/yr)", + "Biomass with CC" = "SE|Electricity|Biomass|w/ CC (EJ/yr)", + "Biomass without CC" = "SE|Electricity|Biomass|w/o CC (EJ/yr)", + "Nuclear" = "SE|Electricity|Nuclear (EJ/yr)", + "Hydrogen" = "SE|Electricity|Hydrogen (EJ/yr)", + "Solar" = "SE|Electricity|Solar (EJ/yr)", + "Wind" = "SE|Electricity|Wind (EJ/yr)", + "Geothermal" = "SE|Electricity|Geothermal (EJ/yr)", + "Hydro" = "SE|Electricity|Hydro (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$value <- 1/0.0036 * df$value # converting from EJ to TWh + df <- as.quitte(data[reg, , vars]) + df$value <- 1 / 0.0036 * df$value # converting from EJ to TWh df$unit <- "TWh/yr" - df$details <- paste0(round(df$value,2)," TWh
",names(vars[df$variable]), " electricity secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," TWh
","Total electricity secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) - #+ annotate("text", x=2015,y=Inf, label = unique(df$region), hjust = 0, vjust = 2, size=3.5) - ) - if (length(reg) == 1) + df$details <- paste0(round(df$value, 2), " TWh
", names(vars[df$variable]), " electricity secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings( + ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " TWh
", "Total electricity secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color) + #+ annotate("text", x=2015,y=Inf, label = unique(df$region), hjust = 0, vjust = 2, size=3.5) + ) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Electricity Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Electricity Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Electricity Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$SE$"Electricity Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$SE$'Electricity Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Electricity Secondary Energy") <- names(regionsList) } - out$legend$'Electricity Secondary Energy'$description <- "

Electricity secondary energy per carrier

" - out$legend$'Electricity Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Electricity Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Electricity Secondary Energy'$contents <- c(list("Total electricity secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Electricity Secondary Energy'$contents) - out$legend$'Electricity Secondary Energy'$units <- "Terawatt hour per year" + out$legend$"Electricity Secondary Energy"$description <- "

Electricity secondary energy per carrier

" + out$legend$"Electricity Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Electricity Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Electricity Secondary Energy"$contents <- c(list("Total electricity secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Electricity Secondary Energy"$contents) + out$legend$"Electricity Secondary Energy"$units <- "Terawatt hour per year" ### SE Biomass ### SE Gases - #var.tot <-"SE|Gases (EJ/yr)" - vars <- c("Biomass with CC" = "SE|Gases|Biomass|w/ CC (EJ/yr)", - "Biomass without CC" = "SE|Gases|Biomass|w/o CC (EJ/yr)", - "Coal with CC" = "SE|Gases|Coal|w/ CC (EJ/yr)", - "Coal without CC" = "SE|Gases|Coal|w/o CC (EJ/yr)", - "Natural Gas" = "SE|Gases|Natural Gas (EJ/yr)" + # var.tot <-"SE|Gases (EJ/yr)" + vars <- c( + "Biomass with CC" = "SE|Gases|Biomass|w/ CC (EJ/yr)", + "Biomass without CC" = "SE|Gases|Biomass|w/o CC (EJ/yr)", + "Coal with CC" = "SE|Gases|Coal|w/ CC (EJ/yr)", + "Coal without CC" = "SE|Gases|Coal|w/o CC (EJ/yr)", + "Natural Gas" = "SE|Gases|Natural Gas (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " gases secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total gases secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " gases secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total gases secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Gases Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Gases Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Gases Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$SE$"Gases Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$SE$'Gases Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Gases Secondary Energy") <- names(regionsList) } - out$legend$'Gases Secondary Energy'$description <- "

Gases secondary energy per carrier

" - out$legend$'Gases Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Gases Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Gases Secondary Energy'$contents <- c(list("Total gases secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Gases Secondary Energy'$contents) - out$legend$'Gases Secondary Energy'$units <- "EJ per year" + out$legend$"Gases Secondary Energy"$description <- "

Gases secondary energy per carrier

" + out$legend$"Gases Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Gases Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Gases Secondary Energy"$contents <- c(list("Total gases secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Gases Secondary Energy"$contents) + out$legend$"Gases Secondary Energy"$units <- "EJ per year" ### SE Heat - #var.tot <-"SE|Heat (EJ/yr)" - vars <- c("Biomass" = "SE|Heat|Biomass (EJ/yr)", - "Coal" = "SE|Heat|Coal (EJ/yr)", - "Gas" = "SE|Heat|Gas (EJ/yr)", - "Heat Pump" = "SE|Heat|Electricity|Heat Pump (EJ/yr)" + # var.tot <-"SE|Heat (EJ/yr)" + vars <- c( + "Biomass" = "SE|Heat|Biomass (EJ/yr)", + "Coal" = "SE|Heat|Coal (EJ/yr)", + "Gas" = "SE|Heat|Gas (EJ/yr)", + "Heat Pump" = "SE|Heat|Electricity|Heat Pump (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " heat secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total heat secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " heat secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total heat secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Heat Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Heat Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Heat Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$SE$"Heat Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$SE$'Heat Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Heat Secondary Energy") <- names(regionsList) } - out$legend$'Heat Secondary Energy'$description <- "

Heat secondary energy per carrier

" - out$legend$'Heat Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Heat Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Heat Secondary Energy'$contents <- c(list("Total heat secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Heat Secondary Energy'$contents) - out$legend$'Heat Secondary Energy'$units <- "EJ per year" + out$legend$"Heat Secondary Energy"$description <- "

Heat secondary energy per carrier

" + out$legend$"Heat Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Heat Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Heat Secondary Energy"$contents <- c(list("Total heat secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Heat Secondary Energy"$contents) + out$legend$"Heat Secondary Energy"$units <- "EJ per year" ### SE Liquids - #var.tot <-"SE|Liquids (EJ/yr)" - vars <- c("Biomass" = "SE|Liquids|Biomass (EJ/yr)", - "Coal with CC" = "SE|Liquids|Coal|w/ CC (EJ/yr)", - "Coal without CC" = "SE|Liquids|Coal|w/o CC (EJ/yr)", - "Gas with CC" = "SE|Liquids|Gas|w/ CC (EJ/yr)", - "Gas without CC" = "SE|Liquids|Gas|w/o CC (EJ/yr)", - "Oil" = "SE|Liquids|Oil (EJ/yr)" + # var.tot <-"SE|Liquids (EJ/yr)" + vars <- c( + "Biomass" = "SE|Liquids|Biomass (EJ/yr)", + "Coal with CC" = "SE|Liquids|Coal|w/ CC (EJ/yr)", + "Coal without CC" = "SE|Liquids|Coal|w/o CC (EJ/yr)", + "Gas with CC" = "SE|Liquids|Gas|w/ CC (EJ/yr)", + "Gas without CC" = "SE|Liquids|Gas|w/o CC (EJ/yr)", + "Oil" = "SE|Liquids|Oil (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " liquids secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total liquids secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " liquids secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total liquids secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Liquids Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Liquids Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Liquids Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$SE$"Liquids Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$SE$'Liquids Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Liquids Secondary Energy") <- names(regionsList) } - out$legend$'Liquids Secondary Energy'$description <- "

Liquids secondary energy per carrier

" - out$legend$'Liquids Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Liquids Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Liquids Secondary Energy'$contents <- c(list("Total liquids secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Liquids Secondary Energy'$contents) - out$legend$'Liquids Secondary Energy'$units <- "EJ per year" + out$legend$"Liquids Secondary Energy"$description <- "

Liquids secondary energy per carrier

" + out$legend$"Liquids Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Liquids Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Liquids Secondary Energy"$contents <- c(list("Total liquids secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Liquids Secondary Energy"$contents) + out$legend$"Liquids Secondary Energy"$units <- "EJ per year" ### SE Solids - #var.tot <-"SE|Solids (EJ/yr)" - vars <- c("Biomass" = "SE|Solids|Biomass (EJ/yr)", - "Traditional Biomass" = "SE|Solids|Traditional Biomass (EJ/yr)", - "Coal" = "SE|Solids|Coal (EJ/yr)" + # var.tot <-"SE|Solids (EJ/yr)" + vars <- c( + "Biomass" = "SE|Solids|Biomass (EJ/yr)", + "Traditional Biomass" = "SE|Solids|Traditional Biomass (EJ/yr)", + "Coal" = "SE|Solids|Coal (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " solids secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total solids secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " solids secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total solids secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Solids Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Solids Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Solids Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$SE$"Solids Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$SE$'Solids Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Solids Secondary Energy") <- names(regionsList) } - out$legend$'Solids Secondary Energy'$description <- "

Solids secondary energy per carrier

" - out$legend$'Solids Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Solids Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Solids Secondary Energy'$contents <- c(list("Total solids secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Solids Secondary Energy'$contents) - out$legend$'Solids Secondary Energy'$units <- "EJ per year" + out$legend$"Solids Secondary Energy"$description <- "

Solids secondary energy per carrier

" + out$legend$"Solids Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Solids Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Solids Secondary Energy"$contents <- c(list("Total solids secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Solids Secondary Energy"$contents) + out$legend$"Solids Secondary Energy"$units <- "EJ per year" ### SE Hydrogen - #var.tot <-"SE|Hydrogen (EJ/yr)" - vars <- c("Biomass with CC" = "SE|Hydrogen|Biomass|w/ CC (EJ/yr)", - "Biomass without CC" = "SE|Hydrogen|Biomass|w/o CC (EJ/yr)", - "Coal with CC" = "SE|Hydrogen|Coal|w/ CC (EJ/yr)", - "Coal without CC" = "SE|Hydrogen|Coal|w/o CC (EJ/yr)", - "Gas with CC" = "SE|Hydrogen|Gas|w/ CC (EJ/yr)", - "Gas without CC" = "SE|Hydrogen|Gas|w/o CC (EJ/yr)", - "Electricity" = "SE|Hydrogen|Electricity (EJ/yr)" + # var.tot <-"SE|Hydrogen (EJ/yr)" + vars <- c( + "Biomass with CC" = "SE|Hydrogen|Biomass|w/ CC (EJ/yr)", + "Biomass without CC" = "SE|Hydrogen|Biomass|w/o CC (EJ/yr)", + "Coal with CC" = "SE|Hydrogen|Coal|w/ CC (EJ/yr)", + "Coal without CC" = "SE|Hydrogen|Coal|w/o CC (EJ/yr)", + "Gas with CC" = "SE|Hydrogen|Gas|w/ CC (EJ/yr)", + "Gas without CC" = "SE|Hydrogen|Gas|w/o CC (EJ/yr)", + "Electricity" = "SE|Hydrogen|Electricity (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " hydrogen secondary energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total hydrogen secondary energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " hydrogen secondary energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total hydrogen secondary energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$SE$'Hydrogen Secondary Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$SE$"Hydrogen Secondary Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$SE$'Hydrogen Secondary Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$SE$"Hydrogen Secondary Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$SE$'Hydrogen Secondary Energy') <- names(regionsList) + names(out$plotly$SE$"Hydrogen Secondary Energy") <- names(regionsList) } - out$legend$'Hydrogen Secondary Energy'$description <- "

Hydrogen secondary energy per carrier

" - out$legend$'Hydrogen Secondary Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Hydrogen Secondary Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Hydrogen Secondary Energy'$contents <- c(list("Total hydrogen secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Hydrogen Secondary Energy'$contents) - out$legend$'Hydrogen Secondary Energy'$units <- "EJ per year" + out$legend$"Hydrogen Secondary Energy"$description <- "

Hydrogen secondary energy per carrier

" + out$legend$"Hydrogen Secondary Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Hydrogen Secondary Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Hydrogen Secondary Energy"$contents <- c(list("Total hydrogen secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Hydrogen Secondary Energy"$contents) + out$legend$"Hydrogen Secondary Energy"$units <- "EJ per year" ##### FINAL ENERGY - #var.tot <-"FE (EJ/yr)" - vars <- c("Electricity" = "FE|Electricity (EJ/yr)", - "Solids" = "FE|Solids (EJ/yr)", - "Liquids" = "FE|Liquids (EJ/yr)", - "Gases" = "FE|Gases (EJ/yr)", - "Heat" = "FE|Heat (EJ/yr)", - "Hydrogen" = "FE|Hydrogen (EJ/yr)"#, - #"FE|Solar (EJ/yr)", - #"FE|Other (EJ/yr)" + # var.tot <-"FE (EJ/yr)" + vars <- c( + "Electricity" = "FE|Electricity (EJ/yr)", + "Solids" = "FE|Solids (EJ/yr)", + "Liquids" = "FE|Liquids (EJ/yr)", + "Gases" = "FE|Gases (EJ/yr)", + "Heat" = "FE|Heat (EJ/yr)", + "Hydrogen" = "FE|Hydrogen (EJ/yr)" # , + # "FE|Solar (EJ/yr)", + # "FE|Other (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",gsub(".*\\|","",as.character(df$variable)), " final energy consumption in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total final energy in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", gsub(".*\\|", "", as.character(df$variable)), " final energy consumption in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total final energy in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Total Final Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Total Final Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Total Final Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$FE$"Total Final Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Total Final Energy') <- names(regionsList) + names(out$plotly$FE$"Total Final Energy") <- names(regionsList) } - out$legend$'Total Final Energy'$description <- "

Final energy per carrier

" - out$legend$'Total Final Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Total Final Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Total Final Energy'$contents <- c(list("Total secondary energy" =list("fill"="#000","linetype"="dashed")),out$legend$'Total Final Energy'$contents) - out$legend$'Total Final Energy'$units <- "EJ per year" - - #price - vars <- c("Price|Final Energy|Buildings (US$2005/GJ)", - #"Price|Final Energy|Transport (US$2005/GJ)", - "Price|Final Energy|Industry (US$2005/GJ)" - ) + out$legend$"Total Final Energy"$description <- "

Final energy per carrier

" + out$legend$"Total Final Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Total Final Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Total Final Energy"$contents <- c(list("Total secondary energy" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Total Final Energy"$contents) + out$legend$"Total Final Energy"$units <- "EJ per year" + + # price + vars <- c( + "Price|Final Energy|Buildings (US$2005/GJ)", + # "Price|Final Energy|Transport (US$2005/GJ)", + "Price|Final Energy|Industry (US$2005/GJ)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2), " US$2005/GJ
", ifelse(reg=="GLO","World", as.character(df$region)), " final energy price for ",gsub("Price\\|Final Energy\\|","",df$variable), "
", "year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, ncol=3, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/GJ
", ifelse(reg == "GLO", "World", as.character(df$region)), " final energy price for ", gsub("Price\\|Final Energy\\|", "", df$variable), "
", "year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, ncol = 3, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Final Energy Prices' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Final Energy Prices" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Final Energy Prices' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - if(length(ggplotly$x$layout$annotations)>4) - for (i in 4:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down + if ("plotly" %in% chartType) { + out$plotly$FE$"Final Energy Prices" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + if (length(ggplotly$x$layout$annotations) > 4) { + for (i in 4:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Final Energy Prices') <- names(regionsList) + names(out$plotly$FE$"Final Energy Prices") <- names(regionsList) } - out$legend$'Final Energy Prices'$description <- "

Final energy prices

" - out$legend$'Final Energy Prices'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"="solid")) }) - names(out$legend$'Final Energy Prices'$contents) <- gsub(".*\\|","",gsub(" \\(.*","",vars)) - out$legend$'Final Energy Prices'$units <- "US$2005/GJ per year" + out$legend$"Final Energy Prices"$description <- "

Final energy prices

" + out$legend$"Final Energy Prices"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = "solid")) + }) + names(out$legend$"Final Energy Prices"$contents) <- gsub(".*\\|", "", gsub(" \\(.*", "", vars)) + out$legend$"Final Energy Prices"$units <- "US$2005/GJ per year" ### FE Transport - #var.tot <-"FE|Transport (EJ/yr)" - vars <- c("Electricity" = "FE|Transport|Electricity (EJ/yr)", - "Liquids - Fossil" = "FE|Transport|Liquids|Fossil (EJ/yr)", - #"Liquids - Oil" = "FE|Transport|Liquids|Oil (EJ/yr)", - #"Liquids - Coal" = "FE|Transport|Liquids|Coal (EJ/yr)", - "Liquids - Biomass" = "FE|Transport|Liquids|Biomass (EJ/yr)", - #"FE|Transport|Gases (EJ/yr)", - "Hydrogen" = "FE|Transport|Hydrogen (EJ/yr)" + # var.tot <-"FE|Transport (EJ/yr)" + vars <- c( + "Electricity" = "FE|Transport|Electricity (EJ/yr)", + "Liquids - Fossil" = "FE|Transport|Liquids|Fossil (EJ/yr)", + # "Liquids - Oil" = "FE|Transport|Liquids|Oil (EJ/yr)", + # "Liquids - Coal" = "FE|Transport|Liquids|Coal (EJ/yr)", + "Liquids - Biomass" = "FE|Transport|Liquids|Biomass (EJ/yr)", + # "FE|Transport|Gases (EJ/yr)", + "Hydrogen" = "FE|Transport|Hydrogen (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " use by transport in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total final energy use by transportat in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " use by transport in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total final energy use by transportat in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Transport Final Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Transport Final Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Transport Final Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$FE$"Transport Final Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Transport Final Energy') <- names(regionsList) + names(out$plotly$FE$"Transport Final Energy") <- names(regionsList) } - out$legend$'Transport Final Energy'$description <- "

Transport final energy use per carrier

" - out$legend$'Transport Final Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Transport Final Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Transport Final Energy'$contents <- c(list("Total final energy use by transport" =list("fill"="#000","linetype"="dashed")),out$legend$'Transport Final Energy'$contents) - out$legend$'Transport Final Energy'$units <- "EJ per year" - - #price - vars <- c("Price|Final Energy|Electricity|Transport (US$2005/GJ)", - "Price|Final Energy|Liquids|Transport (US$2005/GJ)", - "Price|Final Energy|Hydrogen|Transport (US$2005/GJ)") + out$legend$"Transport Final Energy"$description <- "

Transport final energy use per carrier

" + out$legend$"Transport Final Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Transport Final Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Transport Final Energy"$contents <- c(list("Total final energy use by transport" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Transport Final Energy"$contents) + out$legend$"Transport Final Energy"$units <- "EJ per year" + + # price + vars <- c( + "Price|Final Energy|Electricity|Transport (US$2005/GJ)", + "Price|Final Energy|Liquids|Transport (US$2005/GJ)", + "Price|Final Energy|Hydrogen|Transport (US$2005/GJ)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2), " US$2005/GJ
", ifelse(reg=="GLO","World", as.character(df$region)), " ",as.character(variable_label[as.character(df$variable)]), " final energy price for transport
", "year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, ncol=3, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/GJ
", ifelse(reg == "GLO", "World", as.character(df$region)), " ", as.character(variable_label[as.character(df$variable)]), " final energy price for transport
", "year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, ncol = 3, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Transport Final Energy Prices' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Transport Final Energy Prices" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Transport Final Energy Prices' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - if(length(ggplotly$x$layout$annotations)>4) - for (i in 4:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down + if ("plotly" %in% chartType) { + out$plotly$FE$"Transport Final Energy Prices" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + if (length(ggplotly$x$layout$annotations) > 4) { + for (i in 4:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Transport Final Energy Prices') <- names(regionsList) + names(out$plotly$FE$"Transport Final Energy Prices") <- names(regionsList) } - out$legend$'Transport Final Energy Prices'$description <- "

Transport final energy Prices prices

" - out$legend$'Transport Final Energy Prices'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"="solid")) }) - names(out$legend$'Transport Final Energy Prices'$contents) <- as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))) - out$legend$'Transport Final Energy Prices'$units <- "US$2005/GJ per year" + out$legend$"Transport Final Energy Prices"$description <- "

Transport final energy Prices prices

" + out$legend$"Transport Final Energy Prices"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = "solid")) + }) + names(out$legend$"Transport Final Energy Prices"$contents) <- as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))) + out$legend$"Transport Final Energy Prices"$units <- "US$2005/GJ per year" ### FE Industry - #var.tot <-"FE|Industry (EJ/yr)" - vars <- c("Electricity" = "FE|Industry|Electricity (EJ/yr)", - "Liquids" = "FE|Industry|Liquids (EJ/yr)", - "Gases" = "FE|Industry|Gases (EJ/yr)", - "Heat" = "FE|Industry|Heat (EJ/yr)", - "Solids" = "FE|Industry|Solids (EJ/yr)", -# "Solids - Coal" = "FE|Industry|Solids|Coal (EJ/yr)", -# "Solids - Biomass" = "FE|Industry|Solids|Biomass (EJ/yr)", - "Hydrogen" = "FE|Industry|Hydrogen (EJ/yr)"#, - #"FE|Industry|Other (EJ/yr)" + # var.tot <-"FE|Industry (EJ/yr)" + vars <- c( + "Electricity" = "FE|Industry|Electricity (EJ/yr)", + "Liquids" = "FE|Industry|Liquids (EJ/yr)", + "Gases" = "FE|Industry|Gases (EJ/yr)", + "Heat" = "FE|Industry|Heat (EJ/yr)", + "Solids" = "FE|Industry|Solids (EJ/yr)", + # "Solids - Coal" = "FE|Industry|Solids|Coal (EJ/yr)", + # "Solids - Biomass" = "FE|Industry|Solids|Biomass (EJ/yr)", + "Hydrogen" = "FE|Industry|Hydrogen (EJ/yr)" # , + # "FE|Industry|Other (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " use by industry in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total final energy use by industry in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " use by industry in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total final energy use by industry in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Industry Final Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Industry Final Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Industry Final Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$FE$"Industry Final Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Industry Final Energy') <- names(regionsList) + names(out$plotly$FE$"Industry Final Energy") <- names(regionsList) } - out$legend$'Industry Final Energy'$description <- "

Industry final energy use per carrier

" - out$legend$'Industry Final Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Industry Final Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Industry Final Energy'$contents <- c(list("Total final energy use by industry" =list("fill"="#000","linetype"="dashed")),out$legend$'Industry Final Energy'$contents) - out$legend$'Industry Final Energy'$units <- "EJ per year" - - #price - vars <- c("Price|Final Energy|Electricity|Industry (US$2005/GJ)", - "Price|Final Energy|Heating Oil|Industry (US$2005/GJ)", - "Price|Final Energy|Gases|Industry (US$2005/GJ)", - "Price|Final Energy|Heat|Industry (US$2005/GJ)", - "Price|Final Energy|Solids|Industry (US$2005/GJ)", - "Price|Final Energy|Hydrogen|Industry (US$2005/GJ)") + out$legend$"Industry Final Energy"$description <- "

Industry final energy use per carrier

" + out$legend$"Industry Final Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Industry Final Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Industry Final Energy"$contents <- c(list("Total final energy use by industry" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Industry Final Energy"$contents) + out$legend$"Industry Final Energy"$units <- "EJ per year" + + # price + vars <- c( + "Price|Final Energy|Electricity|Industry (US$2005/GJ)", + "Price|Final Energy|Heating Oil|Industry (US$2005/GJ)", + "Price|Final Energy|Gases|Industry (US$2005/GJ)", + "Price|Final Energy|Heat|Industry (US$2005/GJ)", + "Price|Final Energy|Solids|Industry (US$2005/GJ)", + "Price|Final Energy|Hydrogen|Industry (US$2005/GJ)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2), " US$2005/GJ
", ifelse(reg=="GLO","World", as.character(df$region)), " ",as.character(variable_label[as.character(df$variable)]), " final energy price for industry
", "year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color,alpha=aestethics$alpha) + # vertical line at initial year - facet_wrap(~region, ncol=3, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/GJ
", ifelse(reg == "GLO", "World", as.character(df$region)), " ", as.character(variable_label[as.character(df$variable)]), " final energy price for industry
", "year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color, alpha = aestethics$alpha) + # vertical line at initial year + facet_wrap(~region, ncol = 3, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Industry Final Energy Prices' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Industry Final Energy Prices" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Industry Final Energy Prices' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - if(length(ggplotly$x$layout$annotations)>4) - for (i in 4:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down + if ("plotly" %in% chartType) { + out$plotly$FE$"Industry Final Energy Prices" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + if (length(ggplotly$x$layout$annotations) > 4) { + for (i in 4:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Industry Final Energy Prices') <- names(regionsList) + names(out$plotly$FE$"Industry Final Energy Prices") <- names(regionsList) } - out$legend$'Industry Final Energy Prices'$description <- "

Industry final energy Prices prices

" - out$legend$'Industry Final Energy Prices'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"="solid")) }) - names(out$legend$'Industry Final Energy Prices'$contents) <- as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))) - out$legend$'Industry Final Energy Prices'$units <- "US$2005/GJ per year" + out$legend$"Industry Final Energy Prices"$description <- "

Industry final energy Prices prices

" + out$legend$"Industry Final Energy Prices"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = "solid")) + }) + names(out$legend$"Industry Final Energy Prices"$contents) <- as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))) + out$legend$"Industry Final Energy Prices"$units <- "US$2005/GJ per year" ### FE Buildings - #var.tot <-"FE|Buildings (EJ/yr)" - vars <- c("Electricity" = "FE|Buildings|Electricity (EJ/yr)", - "Liquids" = "FE|Buildings|Liquids (EJ/yr)", - "Gases" = "FE|Buildings|Gases (EJ/yr)", - "Heat" = "FE|Buildings|Heat (EJ/yr)", - "Solids - Coal" = "FE|Buildings|Solids|Coal (EJ/yr)", - "Solids - Biomass" = "FE|Buildings|Solids|Biomass (EJ/yr)", - "Hydrogen" = "FE|Buildings|Hydrogen (EJ/yr)"#, - #"FE|Buildings|Other (EJ/yr)" + # var.tot <-"FE|Buildings (EJ/yr)" + vars <- c( + "Electricity" = "FE|Buildings|Electricity (EJ/yr)", + "Liquids" = "FE|Buildings|Liquids (EJ/yr)", + "Gases" = "FE|Buildings|Gases (EJ/yr)", + "Heat" = "FE|Buildings|Heat (EJ/yr)", + "Solids - Coal" = "FE|Buildings|Solids|Coal (EJ/yr)", + "Solids - Biomass" = "FE|Buildings|Solids|Biomass (EJ/yr)", + "Hydrogen" = "FE|Buildings|Hydrogen (EJ/yr)" # , + # "FE|Buildings|Other (EJ/yr)" ) - color <- plotstyle(as.character(gsub("\\+\\|","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub("\\+\\|", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2)," EJ
",names(vars[df$variable]), " use by buildings in ", ifelse(reg=="GLO","the World", as.character(df$region)),"
year: ",df$period) - g <- suppressWarnings( ggplot(data=df,aes_(x=~period,y=~value,fill=~variable,text=~details,group = ~variable)) + - geom_area(alpha=aestethics$alpha) + - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - geom_line(data=df %>% - group_by(.data$region,.data$period) %>% - summarise(value = sum(.data$value,na.rm=TRUE)) %>% - mutate(position= .data$value+max(.data$value)/1000) %>% - mutate(details= paste0(round(.data$value,2)," EJ
","Total final energy use by buildings in ", ifelse(reg=="GLO","the World", as.character(.data$region)),"
year: ",.data$period)) %>% - ungroup(), - aes_(~period,~position,text=~details,group = ~region),color="#000000",size=aestethics$line$size,inherit.aes = FALSE, linetype="dashed",alpha=aestethics$alpha) + - facet_wrap(~region, scales="fixed") + - theme_minimal() + - labs(x = NULL, y = NULL) + - scale_fill_manual(values = color) ) - if (length(reg) == 1) + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " EJ
", names(vars[df$variable]), " use by buildings in ", ifelse(reg == "GLO", "the World", as.character(df$region)), "
year: ", df$period) + g <- suppressWarnings(ggplot(data = df, aes_(x = ~period, y = ~value, fill = ~variable, text = ~details, group = ~variable)) + + geom_area(alpha = aestethics$alpha) + + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + geom_line( + data = df %>% + group_by(.data$region, .data$period) %>% + summarise(value = sum(.data$value, na.rm = TRUE)) %>% + mutate(position = .data$value + max(.data$value) / 1000) %>% + mutate(details = paste0(round(.data$value, 2), " EJ
", "Total final energy use by buildings in ", ifelse(reg == "GLO", "the World", as.character(.data$region)), "
year: ", .data$period)) %>% + ungroup(), + aes_(~period, ~position, text = ~details, group = ~region), color = "#000000", size = aestethics$line$size, inherit.aes = FALSE, linetype = "dashed", alpha = aestethics$alpha + ) + + facet_wrap(~region, scales = "fixed") + + theme_minimal() + + labs(x = NULL, y = NULL) + + scale_fill_manual(values = color)) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Buildings Final Energy' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Buildings Final Energy" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Buildings Final Energy' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - for (i in 1:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down - if(ggplotly$x$layout$annotations[[i]]$y < 0.9) + if ("plotly" %in% chartType) { + out$plotly$FE$"Buildings Final Energy" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + for (i in 1:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down + if (ggplotly$x$layout$annotations[[i]]$y < 0.9) { ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Buildings Final Energy') <- names(regionsList) + names(out$plotly$FE$"Buildings Final Energy") <- names(regionsList) } - out$legend$'Buildings Final Energy'$description <- "

Buildings final energy use per carrier

" - out$legend$'Buildings Final Energy'$contents <- lapply(names(gsub(" \\(.*","",vars)), function(var) { return(list("fill"=color[gsub(" \\(.*","",vars[var])],"linetype"=NULL)) }) - names(out$legend$'Buildings Final Energy'$contents) <- names(gsub(" \\(.*","",vars)) - out$legend$'Buildings Final Energy'$contents <- c(list("Total final energy use by buildings" =list("fill"="#000","linetype"="dashed")),out$legend$'Buildings Final Energy'$contents) - out$legend$'Buildings Final Energy'$units <- "EJ per year" - - #price - vars <- c("Price|Final Energy|Electricity|Buildings (US$2005/GJ)", - "Price|Final Energy|Heating Oil|Buildings (US$2005/GJ)", - "Price|Final Energy|Gases|Buildings (US$2005/GJ)", - "Price|Final Energy|Heat|Buildings (US$2005/GJ)", - "Price|Final Energy|Solids|Buildings (US$2005/GJ)", - "Price|Final Energy|Hydrogen|Buildings (US$2005/GJ)" - ) + out$legend$"Buildings Final Energy"$description <- "

Buildings final energy use per carrier

" + out$legend$"Buildings Final Energy"$contents <- lapply(names(gsub(" \\(.*", "", vars)), function(var) { + return(list("fill" = color[gsub(" \\(.*", "", vars[var])], "linetype" = NULL)) + }) + names(out$legend$"Buildings Final Energy"$contents) <- names(gsub(" \\(.*", "", vars)) + out$legend$"Buildings Final Energy"$contents <- c(list("Total final energy use by buildings" = list("fill" = "#000", "linetype" = "dashed")), out$legend$"Buildings Final Energy"$contents) + out$legend$"Buildings Final Energy"$units <- "EJ per year" + + # price + vars <- c( + "Price|Final Energy|Electricity|Buildings (US$2005/GJ)", + "Price|Final Energy|Heating Oil|Buildings (US$2005/GJ)", + "Price|Final Energy|Gases|Buildings (US$2005/GJ)", + "Price|Final Energy|Heat|Buildings (US$2005/GJ)", + "Price|Final Energy|Solids|Buildings (US$2005/GJ)", + "Price|Final Energy|Hydrogen|Buildings (US$2005/GJ)" + ) - color <- plotstyle(as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))),unknown=missingColorsdf) - names(color) <- gsub(" \\(.*","",vars) + color <- plotstyle(as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))), unknown = missingColorsdf) + names(color) <- gsub(" \\(.*", "", vars) g <- lapply(regionsList, function(reg) { - df <- as.quitte(data[reg,,vars]) - df$details <- paste0(round(df$value,2), " US$2005/GJ
", ifelse(reg=="GLO","World", as.character(df$region)), " ",as.character(variable_label[as.character(df$variable)]), " final energy price for buildings
", "year: ",df$period) - g <- ggplot(data=df,aes_(x=~period,y=~value,color=~variable,text=~details, group = ~variable)) + - geom_line(size=aestethics$line$size,alpha=aestethics$alpha) + # line plot - geom_vline(xintercept=as.numeric(min(df$period)),linetype=2, size=aestethics$`y-axis`$size, color=aestethics$`y-axis`$color) + # vertical line at initial year - facet_wrap(~region, ncol=3, scales="fixed") + + df <- as.quitte(data[reg, , vars]) + df$details <- paste0(round(df$value, 2), " US$2005/GJ
", ifelse(reg == "GLO", "World", as.character(df$region)), " ", as.character(variable_label[as.character(df$variable)]), " final energy price for buildings
", "year: ", df$period) + g <- ggplot(data = df, aes_(x = ~period, y = ~value, color = ~variable, text = ~details, group = ~variable)) + + geom_line(size = aestethics$line$size, alpha = aestethics$alpha) + # line plot + geom_vline(xintercept = as.numeric(min(df$period)), linetype = 2, size = aestethics$`y-axis`$size, color = aestethics$`y-axis`$color) + # vertical line at initial year + facet_wrap(~region, ncol = 3, scales = "fixed") + theme_minimal() + labs(x = NULL, y = NULL) + scale_color_manual(values = color) - if (length(reg) == 1) + if (length(reg) == 1) { g <- g + theme(strip.text.x = element_blank()) - else + } else { g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + } return(g) }) - if ("ggplot" %in% chartType){ - out$ggplot$FE$'Buildings Final Energy Prices' <- g + if ("ggplot" %in% chartType) { + out$ggplot$FE$"Buildings Final Energy Prices" <- g } - if ("plotly" %in% chartType){ - out$plotly$FE$'Buildings Final Energy Prices' <- lapply(names(regionsList), function(reg) { - ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% hide_legend() %>% config(modeBarButtonsToRemove=plotlyButtonsToHide, displaylogo=FALSE) - if(length(ggplotly$x$layout$annotations)>4) - for (i in 4:length(ggplotly$x$layout$annotations)) #displacing facet titles a little down + if ("plotly" %in% chartType) { + out$plotly$FE$"Buildings Final Energy Prices" <- lapply(names(regionsList), function(reg) { + ggplotly <- ggplotly(g[[reg]], tooltip = c("text")) %>% + hide_legend() %>% + config(modeBarButtonsToRemove = plotlyButtonsToHide, displaylogo = FALSE) + if (length(ggplotly$x$layout$annotations) > 4) { + for (i in 4:length(ggplotly$x$layout$annotations)) { # displacing facet titles a little down ggplotly$x$layout$annotations[[i]]$y <- ggplotly$x$layout$annotations[[i]]$y - 0.04 + } + } return(ggplotly) }) - names(out$plotly$FE$'Buildings Final Energy Prices') <- names(regionsList) + names(out$plotly$FE$"Buildings Final Energy Prices") <- names(regionsList) } - out$legend$'Buildings Final Energy Prices'$description <- "

Buildings final energy prices

" - out$legend$'Buildings Final Energy Prices'$contents <- lapply(gsub(" \\(.*","",vars), function(var) { return(list("fill"=color[var],"linetype"="solid")) }) - names(out$legend$'Buildings Final Energy Prices'$contents) <- as.character(gsub(" \\(.*","",shorten_legend(vars,identical_only=TRUE))) - out$legend$'Buildings Final Energy Prices'$units <- "US$2005/GJ per year" + out$legend$"Buildings Final Energy Prices"$description <- "

Buildings final energy prices

" + out$legend$"Buildings Final Energy Prices"$contents <- lapply(gsub(" \\(.*", "", vars), function(var) { + return(list("fill" = color[var], "linetype" = "solid")) + }) + names(out$legend$"Buildings Final Energy Prices"$contents) <- as.character(gsub(" \\(.*", "", shorten_legend(vars, identical_only = TRUE))) + out$legend$"Buildings Final Energy Prices"$units <- "US$2005/GJ per year" return(out) } diff --git a/inst/markdown/dashboard.Rmd b/inst/markdown/dashboard.Rmd index 36d101f2..e6566ec3 100644 --- a/inst/markdown/dashboard.Rmd +++ b/inst/markdown/dashboard.Rmd @@ -22,25 +22,27 @@ params: ```{r loading_libraries, include=FALSE} - #loading required libraries - #library(remind2) - library(knitr) - library(flexdashboard) +#loading required libraries +library(knitr) +library(flexdashboard) ``` ```{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 +# 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 loading_diagnosis_charts, include=FALSE} - # loading diagnosis results - diag <- reportDiagnosis(gdx=gdx, statsFile=statsFile, chartType="plotly") +# loading diagnosis results +diag <- reportDiagnosis(gdx=gdx, statsFile=statsFile, chartType="plotly") ``` ```{r loading_REMIND_charts, include=FALSE} - #loading REMIND results and plots - out <- reportCharts(gdx=gdx, regionMapping=regionMapping, hist=hist, reportfile=reportfile, chartType="plotly") +# loading REMIND results and plots + +# TODO: fix for charttype plotly and default +debugonce(reportCharts) +out <- reportCharts(gdx = gdx, regionMapping = regionMapping, hist = hist, reportfile = reportfile, chartType = "plotly") ``` ```{r creating_info_tooltips, include=FALSE}