Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dry high level docs #246

Merged
merged 4 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 3 additions & 215 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,6 @@ knitr::opts_chunk$set(
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")) {
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 <a href='https://github.com/2DegreesInvesting/r2dii.plot'><img src='https://imgur.com/A5ASZPE.png' align='right' height='43' /></a>
Expand All @@ -48,197 +24,9 @@ experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](h
[![CRAN status](https://www.r-pkg.org/badges/version/r2dii.plot)](https://CRAN.R-project.org/package=r2dii.plot)
<!-- badges: end -->

The goal of r2dii.plot 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 from
[GitHub](https://github.com/2DegreesInvesting/r2dii.plot) with:

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

[How to minimize installation errors?](https://gist.github.com/maurolepore/a0187be9d40aee95a43f20a85f4caed6#installation)

## Example

```{r}
library(dplyr, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
library(r2dii.plot)
```

* `market_share` `r pull_title("market_share")`.

```{r}
market_share
```

* `prep_trajectoryY()`: `r pull_title("prep_trajectory")`.

```{r}
data_trajectory <- prep_trajectoryY(
market_share,
sector_filter = "power",
technology_filter = "oilcap",
region_filter = "global",
scenario_source_filter = "demo_2020",
end_year_filter = 2025,
normalize = TRUE
)
```{r echo=FALSE}
intro <- system.file("intro.Rmd", package = "r2dii.plot")
```

* `plot_trajectoryY()` is an alternative to `plot_trajectoryB()`.

```{r}
# `plot_trajectoryY()` takes more arguments

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_trajectoryY(
data_trajectory,
scenario_specs_good_to_bad = scenario_specs,
main_line_metric = main_line_metric,
additional_line_metrics = additional_line_metrics
)

# more elaborate annotations, title and labels

data_trajectory <- prep_trajectoryY(
market_share,
sector_filter = "power",
technology_filter = "renewablescap",
region_filter = "global",
scenario_source_filter = "demo_2020",
value = "production",
end_year_filter = 2025,
normalize = TRUE
)

scenario_specs <- tibble(
scenario = c("sds", "sps", "cps"),
label = c(
"Sustainable Development Scenario",
"Stated Policies Scenario",
"Current Policies Scenario")
)

plot <- plot_trajectoryY(data_trajectory,
scenario_specs_good_to_bad = scenario_specs,
main_line_metric = main_line_metric,
additional_line_metrics = additional_line_metrics
)

plot +
ggplot2::theme(
plot.margin = ggplot2::unit(c(0.5, 7, 0.5, 0.5), "cm")
) +
ggplot2::labs(
title = "Production trajectory of Renewables Capacity technology\n in the Power sector",
subtitle = "The coloured areas indicate trajectories in reference to a scenario.\n The red area indicates trajectories below any sustainble scenario.",
x = "Year",
y = "Production rate (normalized to 2020)"
)
```

* `prep_techmixY()` `r pull_title("prep_techmix")`.
* `plot_techmixY()` `r pull_title("plot_techmix")`.

```{r}
# Default colours, all data, added title
sector <- "power"

data <- prep_techmixY(
market_share,
sector_filter = sector,
years_filter = c(2020, 2025),
region_filter = "global",
scenario_source_filter = "demo_2020",
scenario_filter = "sds",
value = "technology_share"
)

plot <- plot_techmixY(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"),
hex = palette.colors(n = length(technology), palette = "ggplot2")
)

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

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

sector <- "automotive"

data <- prep_techmixY(market_share,
sector_filter = sector,
years_filter = c(2020, 2025), region_filter = "global",
scenario_source_filter = "demo_2020",
scenario_filter = "sds", value = "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_techmixY(data,
metric_type_order = metric_type_order,
metric_type_labels = metric_type_labels
)
plot +
ggplot2::labs(title = "Technology mix for the Automotive sector")
```

* `prep_timelineY()` `r pull_title("prep_timeline")`.
* `plot_timelineY()` `r pull_title("plot_timelineA")`.

```{r}
data <- prep_timelineY(sda, sector_filter = "cement", extrapolate = TRUE)

# Plot and customize with ggplot2
plot_timelineY(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."
)
```

* `timeline_specs()` `r pull_title("timeline_specs")`.

```{r}
# You may use it as a template to create your custom specs
timeline_specs(data)
```{r child=intro}
```
Loading