library(rgcam)
library(DevelopmentVerification)
library(dplyr)
library(magrittr)
library(ggplot2)
library(stringr)
library(tidyr)
# scenario-mapping file, referenced in all transformation functions (see input sections below)
# specific to integration verification work
map <- system.file("map-files", "scen-mapping.csv", package="DevelopmentVerification") %>%
read.csv() %>%
mutate_all(as.character)
# emissions:input ---------------------------------------------------------
proj.emissions <- system.file("extdata", "emissions.dat", package="DevelopmentVerification") %>%
loadProject()
group <- function(df) {
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
filter(year >= 1900) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam)
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
group.ghg <- function(df) {
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam) %>%
filter(!ghg %in% c("CF4", "C2F6"))
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, ghg, region) %>%
summarise(value=sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year, ghg) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
group.clm <- function(df) {
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map, by = "rgcam") %>%
select(-rgcam, -pic) %>%
mutate(region = "Global")
}
transf.emissions <- list("CO2 emissions by region" = group, "Land Use Change Emission" = group,
"CO2 concentrations" = group.clm, "Climate forcing" = group.clm,
"GHG emissions by region" = group.ghg)
# emissions::implementation -----------------------------------------------
em <- GCAMFigsBase(proj.emissions, transf.emissions)
# refining::input ------------------------------------------------
# rgcam project data is a nested list of 22 scenarios, each scenario holding a list of queries, each query itself a dataframe containing data from a single scenario
proj.refining <- system.file("extdata", "refining.dat", package="DevelopmentVerification") %>%
loadProject()
# transformation functions used to restructure rgcam project data
group <- function(df) {
# map to standard scenario names, forcing target, and branch
df <- df %>%
rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam)
df.region <- df %>% # leave regional data for maps
group_by(Units, scenario, forcing, branch, year, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get world aggregation
group_by(Units, scenario, forcing, branch, year) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
group.quosure <- function(column) {
# look-up value of column, convert to quosure
column <- enquo(column)
group.column <- function(df) { # column in parent function env, so will not be an input for returned function
# map to standard scenario names, forcing target, and branch
df <- df %>%
rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam)
df.region <- df %>% # leave regional data for maps
group_by(Units, scenario, forcing, branch, year, region, (!!column)) %>% # unquote quosure
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get world aggregation
group_by(Units, scenario, forcing, branch, year, (!!column)) %>% # unquote quosure
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
return(group.column)
} # returns a function
# input to GCAMFigsBase() constructor function. specifies what function to use for each query in proj when restructuring data
# restructuring = transform proj into list of queries, each query holding 1 dataframe that contains all 22 scenarios
transf.refining <- list("Regional oil production by fuel" = group.quosure(technology),
"Refined liquids production by region" = group,
"Refined liquids use by aggregate end-use" = group.quosure(sector))
# refining::implementation ---------------------------------------
# construct object ref of class "refining", with data restructured to make use of ggplot faceting
ref <- GCAMFigsBase(proj.refining, transf.refining)
# all plotting functions return a list
# [[1]] is the actual plot
# [[2]] is the name of the plot, which can be used to programatically save the figures
lineplot(ref, "Regional oil production by fuel")[[1]]
barchart(ref, "Regional oil production by fuel", "Ref")[[1]]
# this query is calculated on initialization
barchart(ref, "Refining Tech Shares", "Ref")[[1]]
# these have the same lineplot, but 'by region' has no barchart method b/c "Tech shares" is used to split production into conventional and nonconventional oil
lineplot(ref, "Refined liquids production by region")[[1]]
lineplot(ref, "Refined liquids production by technology")[[1]]
barchart(ref, "Refined liquids production by technology", "Ref")[[1]] # barhart() needs the forcing target as input and defaults to the region "Global"
barchart(ref, "Refined liquids production by technology", "Ref", "USA")[[1]]
lineplot(ref, "Refined liquids use by aggregate end-use")[[1]]
barchart(ref, "Refined liquids use by aggregate end-use", "Ref")[[1]]
lineplot(ref, "Refined liquids use by technology and aggregate end-use")[[1]]
# no barchart method for this query because there are two keys (technology, sector)
# would have to concatenate into an artificial key for all combos of tech & sector
# electricity::input ---------------------------------------------
proj.electricity <- system.file("extdata", "electricity.dat", package="DevelopmentVerification") %>%
loadProject()
# transformation functions
group <- function(df) {
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
filter(year >= 1900) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam)
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
group.pe <- function(df) {
df <- df %>%
filter(Units == "EJ") %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam) %>%
mutate(fuel = ifelse(fuel == "traded biomass", "d biomass", fuel)) %>%
mutate(fuel = ifelse(fuel == "traded biomass CCS", "d biomass CCS", fuel))
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, fuel, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year, fuel) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
group.el <- function(df) {
map.tech <- system.file("map-files", "elec_map.csv", package="DevelopmentVerification") %>%
read.csv() %>%
mutate_all(as.character)
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map.tech, by=c("technology")) %>% # agg.tech
left_join(map, by = c("rgcam")) %>% # scenario, forcing, branch
select(-rgcam)
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, agg.tech, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year, agg.tech) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
transf.electricity <- list("Electricity generation by technology (inc solar roofs)" = group.el,
"Primary energy with CCS (Direct Equivalent)" = group.pe,
"Total biomass consumption" = group)
# electricity::implementation ------------------------------------
el <- GCAMFigsBase(proj.electricity, transf.electricity)
lineplot(el, "Electricity generation by technology (inc solar roofs)")[[1]]
barchart(el, "Electricity generation by technology (inc solar roofs)", "Ref")[[1]]
lineplot(el, "Primary energy with CCS (Direct Equivalent)")[[1]]
barchart(el, "Primary energy with CCS (Direct Equivalent)", "Ref")[[1]]
lineplot(el, "Total biomass consumption")[[1]]
# water::input ---------------------------------------------------
proj.h2o <- system.file("extdata", "water.dat", package="DevelopmentVerification") %>%
loadProject()
# transformation functions
group.h2o <- function(df) {
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam) %>%
# elec_(fuel) for fuels in elec sector, just went sector column to describe general sectors drawing water (irrigation, livestock, etc.)
mutate(sector = ifelse(grepl("elec", sector), "electricity", sector)) %>%
# fix irrigation and Irrigation error
mutate(sector = ifelse(sector == "Irrigation", "irrigation", sector))
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, sector, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year, sector) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
get.cool <- function(sector) {
splt <- sector %>%
str_split(., ",", simplify = TRUE)
splt <- splt[, 2] %>%
str_split(., "\\s\\(", simplify = TRUE)
splt <- splt[, 2:3]
for (i in 1:nrow(splt)) {
if (splt[i, 2] == "") {
next
} else if (splt[i, 2] != "") {
splt[i, 1] = splt[i, 2]
splt[i, 2] = ""
}
}
return(str_replace(splt[, 1], "\\)", ""))
}
group.h2o.el <- function(df) {
df <- df %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam) %>%
mutate(sector = get.cool(sector))
df.region <- df %>% # include regional data for regional figs
group_by(Units, scenario, forcing, branch, year, sector, region) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get global aggregate
group_by(Units, scenario, forcing, branch, year, sector) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
transf.h2o <- list("Withdrawals by sector: All Sectors" = group.h2o,
"Withdrawals by sector: Electricity Total" = group.h2o.el)
# water::implementation ------------------------------------------
h2o <- GCAMFigsBase(proj.h2o, transf.h2o)
lineplot(h2o, "Withdrawals by sector: All Sectors")[[1]]
barchart(h2o, "Withdrawals by sector: All Sectors", "Ref")[[1]]
lineplot(h2o, "Withdrawals by sector: Electricity Total")[[1]]
barchart(h2o, "Withdrawals by sector: Electricity Total", "Ref")[[1]]
lineplot(h2o, "Electricity Cooling Technology Shares")[[1]]
barchart(h2o, "Electricity Cooling Technology Shares", "Ref")[[1]]
# land::input ----------------------------------------------------
proj.land <- system.file("extdata", "land.dat", package="DevelopmentVerification") %>%
loadProject()
# transformation functions
expand.allocation <- function(la) {
la <- str_replace_all(la, "Root_Tuber", "Root-Tuber")
if (any(str_detect(la, "_"))) {
# integration branch
la.splt <- la %>%
str_replace_all("biomass_grass", "biomass") %>%
str_replace_all("biomass_tree", "biomass") %>%
str_split("_", simplify = TRUE)
} else {
# master branch
la.splt <- la %>%
str_split("AEZ\\d\\d", simplify = TRUE)
# mirrors c(crop, basin, irr, tech) structure of integration branch
la.splt <- cbind(la.splt[, 1], "", la.splt[, 2], "")
}
la.splt[, 1] <- la.splt[, 1] %>%
str_replace_all("Root-Tuber", "Root_Tuber")
la.splt
}
group.land.al <- function(df) {
map.land <- system.file("map-files", "lty.csv", package="DevelopmentVerification") %>%
read.csv() %>%
mutate_all(as.character)
df <- df %>%
dplyr::rename(rgcam = scenario,
land = "land-allocation") %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam) %>%
mutate(
land_allocation = expand.allocation(land)[, 1],
# should return only one row
water = expand.allocation(land)[, 3], # crop water management
# but leave empty for flexibility
fertilizer = expand.allocation(land)[, 4] # fertilizer?
) %>%
left_join(map.land, by=c("land_allocation")) # land_type
df.region <- df %>% # leave regional data for maps
group_by(Units, scenario, forcing, branch, year, region, land_type, water, fertilizer) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get world aggregation
group_by(Units, scenario, forcing, branch, year, land_type, water, fertilizer) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
group.land.crop <- function(df) {
map.land <- system.file("map-files", "lty.csv", package="DevelopmentVerification") %>%
read.csv() %>%
mutate_all(as.character)
df <- df %>%
filter(Units == "Mt") %>%
dplyr::rename(rgcam = scenario) %>%
left_join(map, by = c("rgcam")) %>%
select(-rgcam) %>%
left_join(map.land, by=c("output" = "land_allocation")) %>% # land_type
filter(!is.na(land_type)) #some output categories remain unmapped (see notes)
df.region <- df %>% # leave regional data for maps
group_by(Units, scenario, forcing, branch, year, region, sector, land_type) %>%
summarise(value = sum(value)) %>%
ungroup()
df.global <- df %>% # get world aggregation
group_by(Units, scenario, forcing, branch, year, sector, land_type) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(region = "Global")
rbind(df.region, df.global)
}
transf.land <- list("Land Allocation" = group.land.al,
"Ag Production by Crop Type" = group.land.crop)
# land::implementation -------------------------------------------
land <- GCAMFigsBase(proj.land, transf.land)
lineplot(land, "Land Allocation")[[1]]
barchart(land, "Land Allocation", "Ref")[[1]]
barchart(land, "Water Management Shares", "Ref")[[1]]
barchart(land, "Fertilizer Tech Shares", "Ref")[[1]]
lineplot(land, "Ag Production by Crop Type")[[1]]
barchart(land, "Ag Production by Crop Type", "Ref")[[1]]
lineplot(land, "Average Yield")[[1]]
barchart(land, "Average Yield", "Ref")[[1]]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.