Skip to content

Commit

Permalink
Merge pull request #111 from orichters/emptylegend
Browse files Browse the repository at this point in the history
make sure totals in showAreaAndBarPlots always find area plots
  • Loading branch information
orichters authored Sep 11, 2024
2 parents 158a58f + 1ad7b2d commit 8e189c1
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '29818196'
ValidationKey: '29964000'
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.3
date-released: '2024-09-06'
version: 0.150.0
date-released: '2024-09-10'
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.3
Date: 2024-09-06
Version: 0.150.0
Date: 2024-09-10
Authors@R: c(
person("David", "Klein", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
52 changes: 29 additions & 23 deletions R/mipArea.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,40 @@ mipArea <- function(x, stack_priority = c("variable", "region"), total = TRUE, s
###
### Find out which variables to stack and which to put to facet_grid
###

# count levels of the given columns
nLevels <- vapply(subset(x, select = c("variable", "region", "scenario", "model")), nlevels, integer(1))

# Find first dimension in stack_priority that has more than one element.
# Initialize dimToStack (this applies if if all dimensions have only one element)
dimToStack <- stack_priority[1]
for (s in stack_priority) {
if (nLevels[s] > 1) {
dimToStack <- s
break
if ("identifier" %in% names(x)) {
# if the identifier is specified externally, us that + region as facet and stack variables
facets <- c("identifier", "region")
if (!is.null(hist)) x <- rbind(x, hist)
dimToStack <- "variable"
} else {
# Identify stacking and facet_grid based on data
# count levels of the given columns
nLevels <- vapply(subset(x, select = c("variable", "region", "scenario", "model")), nlevels, integer(1))

# Find first dimension in stack_priority that has more than one element.
# Initialize dimToStack (this applies if if all dimensions have only one element)
dimToStack <- stack_priority[1]
for (s in stack_priority) {
if (nLevels[s] > 1) {
dimToStack <- s
break
}
}
}

# find and sort dimension with more than one element, exclude dimToStack
# sort: to be able to build the interaction with the smallest number of resulting combinations (further down)
facets <- sort(nLevels[nLevels > 1])
facets <- setdiff(names(facets), dimToStack)
# find and sort dimension with more than one element, exclude dimToStack
# sort: to be able to build the interaction with the smallest number of resulting combinations (further down)
facets <- sort(nLevels[nLevels > 1])
facets <- setdiff(names(facets), dimToStack)

# Combine data and historical data into one object
if (!is.null(hist)) x <- rbind(x, hist)
# Combine data and historical data into one object
if (!is.null(hist)) x <- rbind(x, hist)

# if there are three facet dimensions that have more than one element combine the two
# smallest ones into the first one to be able to create a 2-D facet_grid later on
if (length(facets) == 3) {
x[, facets[1]] <- paste(x[[facets[2]]], x[[facets[1]]])
# if there are three facet dimensions that have more than one element combine the two
# smallest ones into the first one to be able to create a 2-D facet_grid later on
if (length(facets) == 3) {
x[, facets[1]] <- paste(x[[facets[2]]], x[[facets[1]]])
}
}

# if not provided by user calculate total by summing over dimToStack
if (isTRUE(total)) {
dimToGroup <- setdiff(c("model", "scenario", "region", "variable", "unit", "period"), dimToStack)
Expand Down
1 change: 1 addition & 0 deletions R/showAreaAndBarPlots.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ showAreaAndBarPlots <- function(
.data$region == .env$mainReg,
.data$variable == .env$tot) %>%
droplevels()
dMainTot$scenario <- dMainTot$identifier
p1 <- p1 +
geom_line(
data = dMainTot,
Expand Down
10 changes: 9 additions & 1 deletion R/showLinePlots.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#' 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}.
#' @param vlines period used for vertical line
#' @inheritParams showAreaAndBarPlots
#' @return \code{NULL} is returned invisible.
#' @section Example Plots:
Expand All @@ -39,7 +40,8 @@ showLinePlots <- function(
color.dim.name = NULL,
mainReg = getOption("mip.mainReg"),
color.dim.manual = NULL,
histModelsExclude = NULL
histModelsExclude = NULL,
vlines = NULL
) {

data <- as.quitte(data) %>%
Expand Down Expand Up @@ -119,6 +121,9 @@ showLinePlots <- function(
color.dim.manual = color.dim.manual,
color.dim.manual.hist = color.dim.manual.hist[mainHistModels]
)
if (! is.null(vlines)) {
p1 <- p1 + geom_vline(xintercept = vlines, linetype = 3)
}
}
if (NROW(dRegiScen) == 0) {
p2 <- ggplot() + theme_minimal()
Expand All @@ -134,6 +139,9 @@ showLinePlots <- function(
color.dim.manual = color.dim.manual,
color.dim.manual.hist = color.dim.manual.hist[regiHistModels]
)
if (! is.null(vlines)) {
p2 <- p2 + geom_vline(xintercept = vlines, linetype = 3)
}
}

# If a legend of the plots can be used as common legend for both plots,
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.3**
R package **mip**, version **0.150.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/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.3, <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.150.0, <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.3},
note = {R package version 0.150.0},
url = {https://github.com/pik-piam/mip},
doi = {10.5281/zenodo.1158586},
}
Expand Down
5 changes: 4 additions & 1 deletion man/showLinePlots.Rd

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

0 comments on commit 8e189c1

Please sign in to comment.