Skip to content

Commit

Permalink
allow to use mipBarYearData() with different models
Browse files Browse the repository at this point in the history
  • Loading branch information
orichters committed Jan 2, 2023
1 parent 7f072e2 commit 4ad331e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '27084132'
ValidationKey: '27296190'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/lucode2-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
curl -OLs https://eddelbuettel.github.io/r-ci/run.sh
chmod 0755 run.sh
./run.sh bootstrap
rm -f bspm_*.tar.gz
- name: Enable r-universe repo, modify bspm integration
run: |
Expand All @@ -48,6 +49,7 @@ jobs:
pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]),
pkgs[pkgs %%in%% noBinaryInstallRPackages])
}
type <- "source"
})
trace(utils::install.packages, expr, print = FALSE)
})
Expand All @@ -67,9 +69,9 @@ jobs:
uses: pat-s/always-upload-cache@v3
with:
path: /usr/local/lib/R/
key: 2-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }}
key: 3-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }}
restore-keys: |
2-${{ runner.os }}-usr-local-lib-R-
3-${{ runner.os }}-usr-local-lib-R-
- name: Restore R library permissions
run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9003
rev: v0.3.2.9007
hooks:
- id: parsable-R
- id: deps-in-desc
Expand Down
5 changes: 4 additions & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "mip: Comparison of multi-model runs",
"version": "0.140.1",
"version": "0.141.0",
"description": "<p>Package contains generic functions to produce comparison\n plots of multi-model runs.<\/p>",
"creators": [
{
Expand All @@ -23,6 +23,9 @@
},
{
"name": "Führlich, Pascal"
},
{
"name": "Richters, Oliver"
}
]
}
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Type: Package
Package: mip
Title: Comparison of multi-model runs
Version: 0.140.1
Date: 2022-12-06
Version: 0.141.0
Date: 2023-01-02
Authors@R: c(
person("David", "Klein", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
person("Lavinia", "Baumstark", , "[email protected]", role = "aut"),
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"),
person("Miodrag", "Stevanovic", , "[email protected]", role = "aut"),
person("Stephen", "Wirth", , "[email protected]", role = "aut"),
person("Pascal", "Führlich", , "[email protected]", role = "aut")
person("Pascal", "Führlich", , "[email protected]", role = "aut"),
person("Oliver", "Richters", role = "aut")
)
Description: Package contains generic functions to produce comparison
plots of multi-model runs.
Expand Down Expand Up @@ -46,4 +47,4 @@ VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: yes
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
47 changes: 26 additions & 21 deletions R/mipBarYearData.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' @title mipBarYearData
#' @description Function for plotting (bar-plot) MAgPIE objects and compare different scenarios,
#' on the x-axis for some time steps one bar for each scenario is generated
#' @description Function for plotting (bar-plot) MAgPIE objects and compare different scenarios
#' or models, on the x-axis for some time steps one bar for each scenario/model is generated
#'
#'
#' @param x Data to plot. Allowed data formats: magpie or quitte
#' @param ylab y-axis text
#' @param xlab x-axis text
#' @param title title appering at the top of the plot
#' @param title title appearing at the top of the plot
#' @param colour Dimension to be colored, default: "Scenario"
#' @param scenario_markers Use markers to conserve space with long scenario
#' names. Symbols are either picked automatically (default), or can be
Expand All @@ -15,7 +15,7 @@
#' and 20, or a ggplot2 shape name
#' (see \code{vignette("ggplot2-specs")}). Set to \code{FALSE} to not
#' use markers.
#' @author Lavinia Baumstark
#' @author Lavinia Baumstark, Oliver Richters
#' @section Example Plot:
#' \if{html}{\figure{mipBarYearData.png}{example plot}}
#' \if{html}{\figure{mipBarYearData_oneRegi.png}{example plot}}
Expand All @@ -40,16 +40,22 @@
mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = NULL,
scenario_markers = TRUE) { #nolint
scenarioMarkers <- scenario_markers
x <- as.quitte(x)
x <- droplevels(as.quitte(x))

if (length(unique(x$model)) > 1) {
stop("this plot can only deal with data that have only one model")
}
x$identifier <- as.factor(paste0(if (nlevels(x$model) > 1) x$model,
if (nlevels(x$model) > 1 && nlevels(x$scenario) > 1) " ",
if (nlevels(x$scenario) > 1 || nlevels(x$model) == 1) x$scenario
))

if (!is.integer(x$period)) {
stop("this plot can only deal with data that have integer periods")
}

if (nrow(x) == 0) {
warning("Quitte object is empty.")
return()
}

# calculate y-axis label
x$variable <- shorten_legend(x$variable, identical_only = TRUE)

Expand All @@ -61,19 +67,18 @@ mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = N
}

# add dummy-dimension for space between the time-steps
xpos <- crossing(period = unique(x$period),
scenario = factor(c(levels(x$scenario), "\x13"))) %>%
order.levels(scenario = c(levels(x$scenario), "\x13")) %>%
arrange(!!sym("period"), !!sym("scenario")) %>%
mutate(xpos = 1:n()) %>%
filter("\x13" != !!sym("scenario")) %>%
droplevels()
xpos <- crossing(period = unique(x$period),
identifier = factor(c(levels(x$identifier), "\x13"))) %>%
order.levels(identifier = c(levels(x$identifier), "\x13")) %>%
arrange(!!sym("period"), !!sym("identifier")) %>%
mutate(xpos = 1:n()) %>%
filter("\x13" != !!sym("identifier")) %>%
droplevels()

x <- x %>%
inner_join(
xpos,

c("scenario", "period")
c("identifier", "period")
)

if (scenarioMarkers) {
Expand All @@ -93,8 +98,8 @@ mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = N
}

if (scenarioMarkers) {
scenarioMarkers <- stats::setNames((1:20)[seq_along(unique(x$scenario))],
levels(x$scenario))
scenarioMarkers <- stats::setNames((1:20)[seq_along(unique(x$identifier))],
levels(x$identifier))
}

# calculate positions of period labels
Expand Down Expand Up @@ -126,15 +131,15 @@ mipBarYearData <- function(x, colour = NULL, ylab = NULL, xlab = NULL, title = N
labels = xpos$period) +
geom_point(data = yMarker,
mapping = aes(x = !!sym("xpos"), y = !!sym("y"),
shape = !!sym("scenario")),
shape = !!sym("identifier")),
size = 1.5) +
scale_shape_manual(values = scenarioMarkers, name = NULL) +
theme(legend.box = "vertical")
} else {
p <- p +
scale_x_continuous(breaks = xpos$xpos,
labels = xpos %>%
unite(!!sym("label"), !!sym("scenario"),
unite(!!sym("label"), !!sym("identifier"),
!!sym("period"), sep = " ") %>%
getElement("label")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
Expand Down
10 changes: 5 additions & 5 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.140.1**
R package **mip**, version **0.141.0**

[![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/ui#builds)

Expand Down Expand Up @@ -47,16 +47,16 @@ 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 (2022). _mip: Comparison of multi-model runs_. doi: 10.5281/zenodo.1158586 (URL: https://doi.org/10.5281/zenodo.1158586), R package version 0.140.1, <URL: https://github.com/pik-piam/mip>.
Klein D, Dietrich J, Baumstark L, Humpenoeder F, Stevanovic M, Wirth S, Führlich P, Richters O (2023). _mip: Comparison of multi-model runs_. doi: 10.5281/zenodo.1158586 (URL: https://doi.org/10.5281/zenodo.1158586), R package version 0.141.0, <URL: https://github.com/pik-piam/mip>.

A BibTeX entry for LaTeX users is

```latex
@Manual{,
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},
year = {2022},
note = {R package version 0.140.1},
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},
year = {2023},
note = {R package version 0.141.0},
doi = {10.5281/zenodo.1158586},
url = {https://github.com/pik-piam/mip},
}
Expand Down
8 changes: 4 additions & 4 deletions man/mipBarYearData.Rd

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

0 comments on commit 4ad331e

Please sign in to comment.