knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The goal of r2dii.plot is to help you plot 2DII data in an informative, beautiful, and easy way. It is designed to work smoothly with other "r2dii" packages -- r2dii.data, r2dii.match, and r2dii.analysis. It also plays well with the ggplot2 package, which helps you customize your plots.

library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(r2dii.data)
library(r2dii.match)
library(r2dii.analysis)
library(r2dii.plot)

Your data should have a structure similar to that of the demo datasets in the r2dii.data package.

loanbook <- loanbook_demo
ald <- ald_demo
scenario <- co2_intensity_scenario_demo
region <- region_isos_demo

Your workflow involves functions from the packages r2dii.match and r2dii.analysis.

matched <- loanbook %>%
  match_name(ald) %>%
  prioritize() # Remember to validate matches (see `?prioritize`)

r2dii.plot supports three kinds of plots:

Each plot can be created using two types of functions:

For each kind, you'll need to subset the specific rows you want to plot (for example with subset() or dplyr:filter()). For details see the documented "Requirements" of the argument data of each plot_*() or qplot_*() function (e.g. see ?plot_emission_intensity). If you forget to meet the data requirements the error message should guide you.

'Quick' plots

Quick plots allow you to create a good looking plot without extensive knowledge of other R packages. They are perfect to have a quick look into the data and for users who are interested in standard PACTA visualizations. Their common characteristics are:

Use qplot_emission_intensity() with sda-like data.

data <- matched %>%
  target_sda(ald, co2_intensity_scenario = scenario, region_isos = region) %>%
  filter(
    sector == "cement",
    region == "global",
    scenario_source == "demo_2020"
  )

qplot_emission_intensity(data)

Use qplot_trajectory() with market_share-like data.

data <- matched %>%
  target_market_share(ald, scenario = scenario_demo_2020, region_isos = region) %>%
  filter(
    technology == "renewablescap", 
    region == "global", 
    scenario_source == "demo_2020"
  )

qplot_trajectory(data)

Use qplot_techmix() with market_share-like data.

data <- matched %>%
  target_market_share(ald, scenario = scenario_demo_2020, region_isos = region) %>%
  filter(
    sector == "power",
    region == "global",
    scenario_source == "demo_2020",
    metric %in% c("projected", "corporate_economy", "target_sds")
  )

qplot_techmix(data)

Plots

Plots created with plot_*() functions show the data as they are. To customize these plots you can use three strategies:

1) Use parameters of plot_*() functions, 2) Modify the input data, 3) Use ggplot2 functions.

The following sub sections show the three strategies applied to different functions of the plot_*() family.

Plot customization strategies 1 and 3: parameters and ggplot2 functions

We will show how you might customize a plot using strategy 1 and 3 based on plot_emission_intensity().

The basic output of the function looks rather unappealing.

data <- matched %>%
  target_sda(ald, co2_intensity_scenario = scenario, region_isos = region) %>%
  filter(
    sector == "cement",
    region == "global", 
    scenario_source == "demo_2020"
  )

plot_emission_intensity(data)

You can use parameters of plot_emission_intensity() to replicate features of qplot_emission_intensity()(strategy 1), and add plot labels with ggplot2::labs() (strategy 3).

data <- matched %>%
  target_sda(ald, co2_intensity_scenario = scenario, region_isos = region) %>%
  filter(
    sector == "cement",
    region == "global", 
    scenario_source == "demo_2020"
    )

plot_emission_intensity(data, convert_label = to_title, span_5yr = TRUE) +
  labs(
    title = "Emission Intensity Trend for the Cement Sector",
    x = "Year",
    y = "Tons of CO2 per Ton of Production Unit"
  )

Plot customization strategies 2 and 3: input data and ggplot2 functions

You can also polish your plot by modifying the input data (strategy 2), for example:

And by modifying output ggplot object (strategy 3), for example:

Here is how you might customize each of the three kinds of plots using strategy 2 and 3:

data <- sda %>%
  filter(
    sector == "cement",
    region == "global", 
    scenario_source == "demo_2020",
    year <= 2030
  )

plot_emission_intensity(data) +
  labs(
    title = "Emission intensity plot for cement",
    x = "Time",
    y = "Tons of CO2 per ton of cement produced"
  ) +
  scale_color_manual(
    values = c("#4a5e54", "#a63d57", "#78c4d6", "#f2e06e"),
    labels = c("Proj.", "Corp. Economy", "Target (demo)", "Adj. Scenario (demo)")
  )
data <- matched %>%
  target_market_share(ald, scenario = scenario_demo_2020, region_isos = region) %>%
  filter(
    technology == "renewablescap",
    region == "global",
    scenario_source == "demo_2020",
    year <= 2030
  ) %>%
  mutate(
    label = case_when(
      metric == "projected" ~ "Your Portfolio",
      metric == "corporate_economy" ~ "Benchmark (Corp. Economy)",
      metric == "target_sds" ~ "Sustainable Development Scen.",
      metric == "target_sps" ~ "Stated Policies Scen.",
      metric == "target_cps" ~ "Current Policy Scen.",
      TRUE ~ metric
    )
  )

plot_trajectory(data) +
  scale_x_continuous(n.breaks = 3) +
  labs(
    title = "Portfolio Scenario Alignment for Renewables Technology",
    x = "Year",
    y = "Production normalized to start year"
  ) +
  theme(plot.margin = unit(c(0.5, 6, 0.5, 1), "cm")) #so the long labels fit
data <- market_share %>%
  filter(
    metric %in% c("projected", "corporate_economy", "target_sds"),
    sector == "power",
    scenario_source == "demo_2020",
    region == "global",
    year >= 2021,
    year <= 2040 # custom time range
  ) %>%
  mutate(
    label = case_when(
      metric == "projected" ~ "Your Portfolio",
      metric == "corporate_economy" ~ "Corporate Economy Benchmark",
      metric == "target_sds" ~ "SDS Scenario"
    )
  )

plot_techmix(data) +
  scale_fill_manual(
    values = c("black", "brown", "grey", "yellow", "blue", "green4"),
    labels = paste(c("Coal", "Oil", "Gas", "Nuclear", "Hydro", "Renewables"), "Cap.")
  )
data <- market_share %>%
  filter(
    metric %in% c("projected", "corporate_economy", "target_sds"),
    sector == "power",
    region == "global",
    scenario_source == "demo_2020"
  ) %>%
  filter(
    !((metric == "target_sds") & (year == 2020))
  )

plot_techmix(data)

Styling functions

A number of functions allow you to customize any of your plots using the 2DII style:

data <- market_share %>%
  filter(
    metric == "projected",
    sector == "power",
    region == "global",
    year %in% c(2020, 2025)
  )

ggplot(data, aes(x = factor(year), y = production)) +
  geom_col() +
  facet_wrap(~technology) +
  theme_2dii()
data <- market_share %>%
  filter(
    metric != "corporate_economy",
    sector == "power",
    region == "global",
    technology == "renewablescap"
  )

ggplot(data, aes(x = year, y = production, color = metric)) +
  geom_line() +
  scale_colour_r2dii(labels = c("dark_blue", "green", "orange", "ruby_red")) +
  theme_2dii()
data <- market_share %>%
  filter(
    metric == "projected",
    region == "global",
    year %in% c(2020, 2025)
  ) %>%
  group_by(sector, year) %>%
  summarise(production = sum(production))

ggplot(data, aes(x = factor(year), y = production, fill = sector)) +
  geom_col() +
  scale_fill_r2dii_sector(sectors = c("automotive", "oil&gas", "power")) +
  theme_2dii() +
  facet_wrap(~sector)
technologies <- c("coalcap", "oilcap", "gascap", "hydrocap", "renewablescap")

data <- market_share %>%
  filter(
    metric == "projected",
    sector == "power",
    region == "global",
    year %in% c(2020, 2025)
  ) %>%
  mutate(technology = factor(technology, levels = technologies)) %>%
  arrange(technology)

ggplot(data, aes(x = factor(year), y = production, fill = technology)) +
  geom_col() +
  scale_fill_r2dii_tech("power", technologies) +
  facet_wrap(~technology) +
  theme_2dii()

Funding

This project has received funding from the European Union LIFE program and the International Climate Initiative (IKI). The Federal Ministry for the Environment, Nature Conservation and Nuclear Safety (BMU) supports this initiative on the basis of a decision adopted by the German Bundestag. The views expressed are the sole responsibility of the authors and do not necessarily reflect the views of the funders. The funders are not responsible for any use that may be made of the information it contains.



2DegreesInvesting/r2dii.plot documentation built on May 12, 2022, 3:53 a.m.