Skip to content

Commit

Permalink
Merge pull request pik-piam#108 from fbenke-pik/feature
Browse files Browse the repository at this point in the history
enhance showLinePlots
  • Loading branch information
fbenke-pik authored Aug 1, 2024
2 parents 20b62e1 + 7e389b5 commit 5bd00b9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '29693265'
ValidationKey: '29744512'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mip: Comparison of multi-model runs'
version: 0.149.1
date-released: '2024-07-11'
version: 0.149.2
date-released: '2024-08-01'
abstract: Package contains generic functions to produce comparison plots of multi-model
runs.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mip
Title: Comparison of multi-model runs
Version: 0.149.1
Date: 2024-07-11
Version: 0.149.2
Date: 2024-08-01
Authors@R: c(
person("David", "Klein", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
41 changes: 32 additions & 9 deletions R/showLinePlots.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
#'
#' @param vars A character vector. Usually just a single string. The variables
#' to be plotted. If \code{NULL} all rows from \code{data} are plotted.
#' @param histVars A character vector. Usually just a single string. The historical variables
#' to be plotted. If \code{NULL}, it is set to \code{vars}.
#' @param scales A single string. choose either \code{"free_y"} or \code{"fixed"}.
#' @param color.dim.name name for the color-dimension used in the legend
#' @param color.dim.manual optional vector with manual colors replacing default colors of color.dim, default is \code{NULL}.
#' @param histModelsExclude A character vector with historical models to exclude.
#' Set to \code{NULL} (default) for all available data.
#' @param color.dim.manual optional vector with manual colors replacing default
#' colors of color.dim, default is \code{NULL}.
#' @inheritParams showAreaAndBarPlots
#' @return \code{NULL} is returned invisible.
#' @section Example Plots:
Expand All @@ -27,43 +32,61 @@
#' @importFrom gridExtra arrangeGrob

showLinePlots <- function(
data, vars = NULL, scales = "free_y", color.dim.name = NULL,
mainReg = getOption("mip.mainReg"), color.dim.manual = NULL
data,
vars = NULL,
histVars = NULL,
scales = "free_y",
color.dim.name = NULL,
mainReg = getOption("mip.mainReg"),
color.dim.manual = NULL,
histModelsExclude = NULL
) {

data <- as.quitte(data) %>%
filter(!is.na(.data$value))

# Validate function arguments.
stopifnot(is.character(vars) || is.null(vars))
stopifnot(is.character(histVars) || is.null(histVars))
stopifnot(is.character(histModelsExclude) || is.null(histModelsExclude))
stopifnot(is.character(scales) && length(scales) == 1)
checkGlobalOptionsProvided("mainReg")
stopifnot(is.character(mainReg) && length(mainReg) == 1)

if (is.null(histVars)) {
histVars <- vars
}

if (!is.null(vars)) {
v <- unique(c(vars, histVars))
d <- data %>%
filter(.data$variable %in% .env$vars) %>%
filter(.data$variable %in% .env$v) %>%
droplevels()
unitlabel <- ifelse(length(levels(d$unit)) == 0, "", paste0(" (", paste0(levels(d$unit), collapse = ","), ")"))
label <- paste0(paste0(vars, collapse = ","), unitlabel)
label <- paste0(paste0(v, collapse = ","), unitlabel)
} else {
d <- data %>%
droplevels()
unitlabel <- ifelse(length(levels(d$unit)) == 0, "", paste0(" (", paste0(levels(d$unit), collapse = ","), ")"))
label <- paste0(paste0(levels(d$variable), collapse = ","), unitlabel)
}

if (!is.null(histModelsExclude)) {
d <- d %>%
filter(.data$scenario != "historical" | !.data$model %in% .env$histModelsExclude)
}

dMainScen <- d %>%
filter(.data$region == .env$mainReg, .data$scenario != "historical") %>%
filter(.data$region == .env$mainReg, .data$scenario != "historical", .data$variable %in% .env$vars) %>%
droplevels()
dMainHist <- d %>%
filter(.data$region == .env$mainReg, .data$scenario == "historical") %>%
filter(.data$region == .env$mainReg, .data$scenario == "historical", .data$variable %in% .env$histVars) %>%
droplevels()
dRegiScen <- d %>%
filter(.data$region != .env$mainReg, .data$scenario != "historical") %>%
filter(.data$region != .env$mainReg, .data$scenario != "historical", .data$variable %in% .env$vars) %>%
droplevels()
dRegiHist <- d %>%
filter(.data$region != .env$mainReg, .data$scenario == "historical") %>%
filter(.data$region != .env$mainReg, .data$scenario == "historical", .data$variable %in% .env$histVars) %>%
droplevels()

# make sure all plots use the same colors for historical models
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Comparison of multi-model runs

R package **mip**, version **0.149.1**
R package **mip**, version **0.149.2**

[![CRAN status](https://www.r-pkg.org/badges/version/mip)](https://cran.r-project.org/package=mip) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1158586.svg)](https://doi.org/10.5281/zenodo.1158586) [![R build status](https://github.com/pik-piam/mip/workflows/check/badge.svg)](https://github.com/pik-piam/mip/actions) [![codecov](https://codecov.io/gh/pik-piam/mip/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mip) [![r-universe](https://pik-piam.r-universe.dev/badges/mip)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -47,7 +47,7 @@ In case of questions / problems please contact David Klein <[email protected]

To cite package **mip** in publications use:

Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 <https://doi.org/10.5281/zenodo.1158586>, R package version 0.149.1, <https://github.com/pik-piam/mip>.
Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O, Rüter T (2024). _mip: Comparison of multi-model runs_. doi:10.5281/zenodo.1158586 <https://doi.org/10.5281/zenodo.1158586>, R package version 0.149.2, <https://github.com/pik-piam/mip>.

A BibTeX entry for LaTeX users is

Expand All @@ -56,7 +56,7 @@ A BibTeX entry for LaTeX users is
title = {mip: Comparison of multi-model runs},
author = {David Klein and Jan Philipp Dietrich and Lavinia Baumstark and Florian Humpenoeder and Miodrag Stevanovic and Stephen Wirth and Pascal Führlich and Oliver Richters and Tonn Rüter},
year = {2024},
note = {R package version 0.149.1},
note = {R package version 0.149.2},
url = {https://github.com/pik-piam/mip},
doi = {10.5281/zenodo.1158586},
}
Expand Down
13 changes: 11 additions & 2 deletions man/showLinePlots.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5bd00b9

Please sign in to comment.