diff --git a/.buildlibrary b/.buildlibrary index 8037291..be34cf5 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -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' diff --git a/CITATION.cff b/CITATION.cff index 427b2a3..51394ce 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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: diff --git a/DESCRIPTION b/DESCRIPTION index dfc06d8..7e88a7c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "dklein@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/R/mipArea.R b/R/mipArea.R index 72467f2..4d4e5a9 100644 --- a/R/mipArea.R +++ b/R/mipArea.R @@ -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) diff --git a/R/showAreaAndBarPlots.R b/R/showAreaAndBarPlots.R index 3739476..ccca9d3 100644 --- a/R/showAreaAndBarPlots.R +++ b/R/showAreaAndBarPlots.R @@ -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, diff --git a/R/showLinePlots.R b/R/showLinePlots.R index 307b7e0..d3bb9eb 100644 --- a/R/showLinePlots.R +++ b/R/showLinePlots.R @@ -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: @@ -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) %>% @@ -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() @@ -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, diff --git a/README.md b/README.md index 2fd6fe8..1e38222 100644 --- a/README.md +++ b/README.md @@ -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) @@ -47,7 +47,7 @@ In case of questions / problems please contact David Klein , R package version 0.149.3, . +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 , R package version 0.150.0, . A BibTeX entry for LaTeX users is @@ -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}, } diff --git a/man/showLinePlots.Rd b/man/showLinePlots.Rd index 62b3f3f..8d90ed2 100644 --- a/man/showLinePlots.Rd +++ b/man/showLinePlots.Rd @@ -12,7 +12,8 @@ showLinePlots( color.dim.name = NULL, mainReg = getOption("mip.mainReg"), color.dim.manual = NULL, - histModelsExclude = NULL + histModelsExclude = NULL, + vlines = NULL ) } \arguments{ @@ -37,6 +38,8 @@ colors of color.dim, default is \code{NULL}.} \item{histModelsExclude}{A character vector with historical models to exclude. Set to \code{NULL} (default) for all available data.} + +\item{vlines}{period used for vertical line} } \value{ \code{NULL} is returned invisible.