knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  fig.align = "left",
  out.width = "100%"
)

#' Pull the title of a documented object
#' This function helps keep README and help files in sync.
#' @param name String. The name of a documented object.
#' @return String.
#' @examples
#' pull_title("plot_trajectory")
pull_title <- function(name, db = enframe_documentation("r2dii.plot.static")) {
  is_name <- db$name == name
  out <- db$title[is_name]
  tolower(out)
}

enframe_documentation <- function(package) {
  db <- utils::hsearch_db(package, lib.loc = locate_package(package))[["Base"]]
  names(db) <- tolower(names(db))
  db
}

locate_package <- function(pkg) {
  locate <- function(path) any(grepl(pkg, list.files(path)))
  has_pkg <- unlist(lapply(.libPaths(), locate))
  .libPaths()[has_pkg][[1]]
}

r2dii.plot.static

Lifecycle:
experimental Codecov test coverage R-CMD-check CRAN status

The goal of r2dii.plot.static is to provide users with plotting and data processing functions that will allow the users to create standard 2DII plots using PACTA_analysis or banks' output data as input. The plots are in the form of ggplot objects.

Installation

You can install the development version of r2dii.plot.static from GitHub with:

# install.packages("devtools")
devtools::install_github("2DegreesInvesting/r2dii.plot.static")

How to minimize installation errors?

Example

library(dplyr, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
library(r2dii.plot.static)
example_data <- process_input_data(example_data)
data_trajectory <- prepare_for_trajectory_chart(
  example_data,
  sector_filter = "power",
  technology_filter = "renewablescap",
  region_filter = "global",
  scenario_source_filter = "demo_2020",
  value_name = "production",
  end_year_filter = 2025,
  normalize_to_start_year = TRUE
)

scenario_specs <- tibble(
  scenario = c("sds", "sps", "cps"),
  label = c("SDS", "STEPS", "CPS")
)

main_line_metric <- tibble(
  metric = "projected",
  label = "Portfolio"
)

additional_line_metrics <- tibble(
  metric = "corporate_economy",
  label = "Corporate Economy"
)

plot_trajectory(
  data_trajectory,
  scenario_specs_good_to_bad = scenario_specs,
  main_line_metric = main_line_metric,
  additional_line_metrics = additional_line_metrics
)
# Default colours, all data, added title

sector <- "power"

data <- prepare_for_techmix_chart(example_data,
  sector_filter = sector,
  years_filter = c(2020, 2025), region_filter = "global",
  scenario_source_filter = "demo_2020",
  scenario_filter = "sds", value_to_plot = "technology_share"
)

plot <- plot_techmix(data)
plot +
  ggplot2::labs(title = "Technology mix for the Power sector")

# Custom colours, all data, no title
power_colors_custom <- tibble(
  technology = c("coalcap", "oilcap", "gascap", "nuclearcap", "hydrocap", "renewablescap"),
  label = c("Coal Capacity", "Oil Capacity", "Gas Capacity", "Nuclear Capacity", "Hydro Capacity", "Renewables Capacity"),
  colour = c("black", "brown", "grey", "red", "blue", "green4")
)

plot <- plot_techmix(data,
  tech_colours = power_colors_custom
)
plot

# Default colours, selected data and labels (metric_type parameters), added title

sector <- "automotive"

data <- prepare_for_techmix_chart(example_data,
  sector_filter = sector,
  years_filter = c(2020, 2025), region_filter = "global",
  scenario_source_filter = "demo_2020",
  scenario_filter = "sds", value_to_plot = "technology_share"
)

metric_type_order = c(
    "portfolio_2020", "benchmark_2020", "portfolio_2025",
    "benchmark_2025", "scenario_2025"
)
metric_type_labels = c(
    "Portfolio 2020", "Benchmark 2020", "Portfolio 2025",
    "Benchmark 2025", "Target SDS 2025"
  )

plot <- plot_techmix(data,
  metric_type_order = metric_type_order,
  metric_type_labels = metric_type_labels
)
plot +
  ggplot2::labs(title = "Technology mix for the Automotive sector")
# Using default preparation and specs
data <- prepare_for_timelineA(sda_target, sector_filter = "cement")
plot_timelineA(data)

# Using custom preparation
data <- prepare_for_timelineA(
  sda_target,
  sector_filter = "cement",
  year_start = 2020,
  year_end = 2050,
  column_line_names = "emission_factor_metric",
  value_to_plot = "emission_factor_value",
  extrapolate_missing_values = TRUE
)

# Using custom specs and extending the plot with ggplot2
plot_timelineA(data) +
  labs(
    title = "Emission intensity trend for Cement.",
    x = "Year",
    y = "Tons of CO2 per ton",
    caption = "Dashed line is an extrapolation of the last value in the dataset."
  )
# You may use it as a template to create your custom specs
timeline_specs(data)

# You may use it as a reference for 2DII's colour palette
r2dii_palette_colours()


2DegreesInvesting/r2dii.plot.static documentation built on Dec. 17, 2021, 6:37 a.m.