From 494f6f8f29f37a120fe596742befb6744cf9f514 Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 21 Oct 2022 11:36:27 +0200 Subject: [PATCH 1/2] PlotForecastPDF mem_dim to member --- R/PlotForecastPDF.R | 12 +++++++----- man/PlotForecastPDF.Rd | 7 +++++-- tests/testthat/test-PlotForecastPDF.R | 2 +- vignettes/PlotForecastPDF.Rmd | 6 +++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/R/PlotForecastPDF.R b/R/PlotForecastPDF.R index 63116289..5fd86ce8 100644 --- a/R/PlotForecastPDF.R +++ b/R/PlotForecastPDF.R @@ -13,6 +13,7 @@ #'@param fcst.names (optional) an array of strings with the titles of each individual forecast. #'@param add.ensmemb either to add the ensemble members \code{'above'} (default) or \code{'below'} the pdf, or not (\code{'no'}). #'@param color.set a selection of predefined color sets: use \code{'ggplot'} (default) for blue/green/red, \code{'s2s4e'} for blue/grey/orange, or \code{'hydro'} for yellow/gray/blue (suitable for precipitation and inflows). +#'@param memb_dim A character string indicating the name of the member dimension. #' #'@return a ggplot object containing the plot. #' @@ -29,14 +30,15 @@ #' fcst3 = rnorm(10, -0.5, 0.9)) #'PlotForecastPDF(fcsts,c(-1,1)) #'\donttest{ -#'fcsts2 <- array(rnorm(100), dim = c(members = 20, fcst = 5)) +#'fcsts2 <- array(rnorm(100), dim = c(member = 20, fcst = 5)) #'PlotForecastPDF(fcsts2, c(-0.66, 0.66), extreme.limits = c(-1.2, 1.2), #' fcst.names = paste0('random fcst ', 1 : 5), obs = 0.7) #'} #'@export PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = NULL, plotfile = NULL, title = "Set a title", var.name = "Varname (units)", fcst.names = NULL, - add.ensmemb = c("above", "below", "no"), color.set = c("ggplot", "s2s4e", "hydro")) { + add.ensmemb = c("above", "below", "no"), color.set = c("ggplot", "s2s4e", "hydro"), + memb_dim = 'member') { value <- init <- extremes <- x <- ymin <- ymax <- tercile <- NULL y <- xend <- yend <- yjitter <- MLT <- lab.pos <- NULL ggColorHue <- function(n) { @@ -76,10 +78,10 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N # Check fcst type and convert to data.frame if needed #------------------------ if (is.array(fcst)) { - if (!"members" %in% names(dim(fcst)) | length(dim(fcst)) != 2) { - stop("Parameter 'fcst' should be a two-dimensional array with labelled dimensions and one of them should be 'members'") + if (!memb_dim %in% names(dim(fcst)) | length(dim(fcst)) != 2) { + stop("Parameter 'fcst' should be a two-dimensional array with labelled dimensions and one of them should be for member. The name of this dimension can be adjusted with 'memb_dim'.") } - dim.members <- which(names(dim(fcst)) == "members") + dim.members <- which(names(dim(fcst)) == memb_dim) if (dim.members == 1) { fcst.df <- data.frame(fcst) } else { diff --git a/man/PlotForecastPDF.Rd b/man/PlotForecastPDF.Rd index c04b43c1..3d94867d 100644 --- a/man/PlotForecastPDF.Rd +++ b/man/PlotForecastPDF.Rd @@ -14,7 +14,8 @@ PlotForecastPDF( var.name = "Varname (units)", fcst.names = NULL, add.ensmemb = c("above", "below", "no"), - color.set = c("ggplot", "s2s4e", "hydro") + color.set = c("ggplot", "s2s4e", "hydro"), + memb_dim = "member" ) } \arguments{ @@ -37,6 +38,8 @@ PlotForecastPDF( \item{add.ensmemb}{either to add the ensemble members \code{'above'} (default) or \code{'below'} the pdf, or not (\code{'no'}).} \item{color.set}{a selection of predefined color sets: use \code{'ggplot'} (default) for blue/green/red, \code{'s2s4e'} for blue/grey/orange, or \code{'hydro'} for yellow/gray/blue (suitable for precipitation and inflows).} + +\item{memb_dim}{A character string indicating the name of the member dimension.} } \value{ a ggplot object containing the plot. @@ -49,7 +52,7 @@ fcsts <- data.frame(fcst1 = rnorm(10), fcst2 = rnorm(10, 0.5, 1.2), fcst3 = rnorm(10, -0.5, 0.9)) PlotForecastPDF(fcsts,c(-1,1)) \donttest{ -fcsts2 <- array(rnorm(100), dim = c(members = 20, fcst = 5)) +fcsts2 <- array(rnorm(100), dim = c(member = 20, fcst = 5)) PlotForecastPDF(fcsts2, c(-0.66, 0.66), extreme.limits = c(-1.2, 1.2), fcst.names = paste0('random fcst ', 1 : 5), obs = 0.7) } diff --git a/tests/testthat/test-PlotForecastPDF.R b/tests/testthat/test-PlotForecastPDF.R index f9926324..f39b9667 100644 --- a/tests/testthat/test-PlotForecastPDF.R +++ b/tests/testthat/test-PlotForecastPDF.R @@ -12,7 +12,7 @@ test_that("Sanity checks", { expect_error( PlotForecastPDF(fcst, tercile.limits = c(10, 20)), "object 'fcst' not found") - fcsts2 <- array(rnorm(100),dim = c(members = 20, fcst = 5)) + fcsts2 <- array(rnorm(100),dim = c(member = 20, fcst = 5)) expect_error( PlotForecastPDF(fcst = fcsts2, tercile.limits), "object 'tercile.limits' not found") diff --git a/vignettes/PlotForecastPDF.Rmd b/vignettes/PlotForecastPDF.Rmd index 757f4047..28fb8ebe 100644 --- a/vignettes/PlotForecastPDF.Rmd +++ b/vignettes/PlotForecastPDF.Rmd @@ -30,10 +30,10 @@ PlotForecastPDF(fcst, tercile.limits = c(20, 26)) ![Example 1](./Figures/PlotForecastPDF_ex1.png) -Input data can also be provided as an two-dimensional array, as far as one of the dimensions is named 'members': +Input data can also be provided as an two-dimensional array, as far as one of the dimensions is named 'member' or adjusted in 'memb_dim' parameter: ```{r,fig.show = 'hide',warning=F} -fcst <- array(rnorm(mean=25, sd=2, n=90), dim=c(members=30, 3)) +fcst <- array(rnorm(mean = 25, sd = 2, n = 90), dim = c(member = 30, 3)) PlotForecastPDF(fcst, tercile.limits = c(23, 27)) ``` @@ -70,7 +70,7 @@ PlotForecastPDF uses ggplot2, so you can save the output of the function to a va ``` library(ggplot2) -fcst <- array(rnorm(mean=25, sd=2, n=90), dim=c(members=30, 3)) +fcst <- array(rnorm(mean = 25, sd = 2, n = 90), dim = c(member = 30, 3)) plot <-PlotForecastPDF(fcst, tercile.limits = c(23, 27)) ggsave("outfile.pdf", plot, width=7, height=5) ``` -- GitLab From 3fc0ed9b4ce108257e7f67d61a07afd8057610af Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Mon, 24 Oct 2022 16:43:53 +0200 Subject: [PATCH 2/2] Correct documentation format --- R/PlotForecastPDF.R | 60 +++++++++++++++++++++++++++++++----------- man/PlotForecastPDF.Rd | 54 ++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 29 deletions(-) diff --git a/R/PlotForecastPDF.R b/R/PlotForecastPDF.R index 5fd86ce8..5cb90c16 100644 --- a/R/PlotForecastPDF.R +++ b/R/PlotForecastPDF.R @@ -1,21 +1,49 @@ #'Plot one or multiple ensemble forecast pdfs for the same event #' #'@author Llorenç Lledó \email{llledo@bsc.es} -#'@description This function plots the probability distribution function of several ensemble forecasts. Separate panels are used to plot forecasts valid or initialized at different times or by different models or even at different locations. Probabilities for tercile categories are computed, plotted in colors and annotated. An asterisk marks the tercile with higher probabilities. Probabilities for extreme categories (above P90 and below P10) can also be included as hatched areas. Individual ensemble members can be plotted as jittered points. The observed value is optionally shown as a diamond. +#'@description This function plots the probability distribution function of +#'several ensemble forecasts. Separate panels are used to plot forecasts valid +#'or initialized at different times or by different models or even at different +#'locations. Probabilities for tercile categories are computed, plotted in +#'colors and annotated. An asterisk marks the tercile with higher probabilities. +#'Probabilities for extreme categories (above P90 and below P10) can also be +#'included as hatched areas. Individual ensemble members can be plotted as +#'jittered points. The observed value is optionally shown as a diamond. #' -#'@param fcst a dataframe or array containing all the ensember members for each forecast. If \code{'fcst'} is an array, it should have two labelled dimensions, and one of them should be \code{'members'}. If \code{'fcsts'} is a data.frame, each column shoul be a separate forecast, with the rows beeing the different ensemble members. -#'@param tercile.limits an array or vector with P33 and P66 values that define the tercile categories for each panel. Use an array of dimensions (nforecasts,2) to define different terciles for each forecast panel, or a vector with two elements to reuse the same tercile limits for all forecast panels. -#'@param extreme.limits (optional) an array or vector with P10 and P90 values that define the extreme categories for each panel. Use an array of (nforecasts,2) to define different extreme limits for each forecast panel, or a vector with two elements to reuse the same tercile limits for all forecast panels. (Default: extreme categories are not shown). -#'@param obs (optional) A vector providing the observed values for each forecast panel or a single value that will be reused for all forecast panels. (Default: observation is not shown). -#'@param plotfile (optional) a filename (pdf, png...) where the plot will be saved. (Default: the plot is not saved). -#'@param title a string with the plot title. -#'@param var.name a string with the variable name and units. -#'@param fcst.names (optional) an array of strings with the titles of each individual forecast. -#'@param add.ensmemb either to add the ensemble members \code{'above'} (default) or \code{'below'} the pdf, or not (\code{'no'}). -#'@param color.set a selection of predefined color sets: use \code{'ggplot'} (default) for blue/green/red, \code{'s2s4e'} for blue/grey/orange, or \code{'hydro'} for yellow/gray/blue (suitable for precipitation and inflows). -#'@param memb_dim A character string indicating the name of the member dimension. +#'@param fcst A dataframe or array containing all the ensember members for each +#' forecast. If \code{'fcst'} is an array, it should have two labelled +#' dimensions, and one of them should be \code{'members'}. If \code{'fcsts'} is +#' a data.frame, each column shoul be a separate forecast, with the rows beeing +#' the different ensemble members. +#'@param tercile.limits An array or vector with P33 and P66 values that define +#' the tercile categories for each panel. Use an array of dimensions +#' (nforecasts,2) to define different terciles for each forecast panel, or a +#' vector with two elements to reuse the same tercile limits for all forecast +#' panels. +#'@param extreme.limits (optional) An array or vector with P10 and P90 values +#' that define the extreme categories for each panel. Use an array of +#' (nforecasts,2) to define different extreme limits for each forecast panel, +#' or a vector with two elements to reuse the same tercile limits for all +#' forecast panels. (Default: extreme categories are not shown). +#'@param obs (optional) A vector providing the observed values for each forecast +#' panel or a single value that will be reused for all forecast panels. +#' (Default: observation is not shown). +#'@param plotfile (optional) A filename (pdf, png...) where the plot will be +#' saved. (Default: the plot is not saved). +#'@param title A string with the plot title. +#'@param var.name A string with the variable name and units. +#'@param fcst.names (optional) An array of strings with the titles of each +#' individual forecast. +#'@param add.ensmemb Either to add the ensemble members \code{'above'} (default) +#' or \code{'below'} the pdf, or not (\code{'no'}). +#'@param color.set A selection of predefined color sets: use \code{'ggplot'} +#' (default) for blue/green/red, \code{'s2s4e'} for blue/grey/orange, or +#' \code{'hydro'} for yellow/gray/blue (suitable for precipitation and +#' inflows). +#'@param memb_dim A character string indicating the name of the member +#' dimension. #' -#'@return a ggplot object containing the plot. +#'@return A ggplot object containing the plot. #' #'@importFrom data.table data.table #'@importFrom data.table CJ @@ -36,9 +64,9 @@ #'} #'@export PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = NULL, - plotfile = NULL, title = "Set a title", var.name = "Varname (units)", fcst.names = NULL, - add.ensmemb = c("above", "below", "no"), color.set = c("ggplot", "s2s4e", "hydro"), - memb_dim = 'member') { + plotfile = NULL, title = "Set a title", var.name = "Varname (units)", + fcst.names = NULL, add.ensmemb = c("above", "below", "no"), + color.set = c("ggplot", "s2s4e", "hydro"), memb_dim = 'member') { value <- init <- extremes <- x <- ymin <- ymax <- tercile <- NULL y <- xend <- yend <- yjitter <- MLT <- lab.pos <- NULL ggColorHue <- function(n) { diff --git a/man/PlotForecastPDF.Rd b/man/PlotForecastPDF.Rd index 3d94867d..c6442bbb 100644 --- a/man/PlotForecastPDF.Rd +++ b/man/PlotForecastPDF.Rd @@ -19,33 +19,61 @@ PlotForecastPDF( ) } \arguments{ -\item{fcst}{a dataframe or array containing all the ensember members for each forecast. If \code{'fcst'} is an array, it should have two labelled dimensions, and one of them should be \code{'members'}. If \code{'fcsts'} is a data.frame, each column shoul be a separate forecast, with the rows beeing the different ensemble members.} +\item{fcst}{A dataframe or array containing all the ensember members for each +forecast. If \code{'fcst'} is an array, it should have two labelled +dimensions, and one of them should be \code{'members'}. If \code{'fcsts'} is +a data.frame, each column shoul be a separate forecast, with the rows beeing +the different ensemble members.} -\item{tercile.limits}{an array or vector with P33 and P66 values that define the tercile categories for each panel. Use an array of dimensions (nforecasts,2) to define different terciles for each forecast panel, or a vector with two elements to reuse the same tercile limits for all forecast panels.} +\item{tercile.limits}{An array or vector with P33 and P66 values that define +the tercile categories for each panel. Use an array of dimensions +(nforecasts,2) to define different terciles for each forecast panel, or a +vector with two elements to reuse the same tercile limits for all forecast +panels.} -\item{extreme.limits}{(optional) an array or vector with P10 and P90 values that define the extreme categories for each panel. Use an array of (nforecasts,2) to define different extreme limits for each forecast panel, or a vector with two elements to reuse the same tercile limits for all forecast panels. (Default: extreme categories are not shown).} +\item{extreme.limits}{(optional) An array or vector with P10 and P90 values +that define the extreme categories for each panel. Use an array of +(nforecasts,2) to define different extreme limits for each forecast panel, +or a vector with two elements to reuse the same tercile limits for all +forecast panels. (Default: extreme categories are not shown).} -\item{obs}{(optional) A vector providing the observed values for each forecast panel or a single value that will be reused for all forecast panels. (Default: observation is not shown).} +\item{obs}{(optional) A vector providing the observed values for each forecast +panel or a single value that will be reused for all forecast panels. +(Default: observation is not shown).} -\item{plotfile}{(optional) a filename (pdf, png...) where the plot will be saved. (Default: the plot is not saved).} +\item{plotfile}{(optional) A filename (pdf, png...) where the plot will be +saved. (Default: the plot is not saved).} -\item{title}{a string with the plot title.} +\item{title}{A string with the plot title.} -\item{var.name}{a string with the variable name and units.} +\item{var.name}{A string with the variable name and units.} -\item{fcst.names}{(optional) an array of strings with the titles of each individual forecast.} +\item{fcst.names}{(optional) An array of strings with the titles of each +individual forecast.} -\item{add.ensmemb}{either to add the ensemble members \code{'above'} (default) or \code{'below'} the pdf, or not (\code{'no'}).} +\item{add.ensmemb}{Either to add the ensemble members \code{'above'} (default) +or \code{'below'} the pdf, or not (\code{'no'}).} -\item{color.set}{a selection of predefined color sets: use \code{'ggplot'} (default) for blue/green/red, \code{'s2s4e'} for blue/grey/orange, or \code{'hydro'} for yellow/gray/blue (suitable for precipitation and inflows).} +\item{color.set}{A selection of predefined color sets: use \code{'ggplot'} +(default) for blue/green/red, \code{'s2s4e'} for blue/grey/orange, or +\code{'hydro'} for yellow/gray/blue (suitable for precipitation and +inflows).} -\item{memb_dim}{A character string indicating the name of the member dimension.} +\item{memb_dim}{A character string indicating the name of the member +dimension.} } \value{ -a ggplot object containing the plot. +A ggplot object containing the plot. } \description{ -This function plots the probability distribution function of several ensemble forecasts. Separate panels are used to plot forecasts valid or initialized at different times or by different models or even at different locations. Probabilities for tercile categories are computed, plotted in colors and annotated. An asterisk marks the tercile with higher probabilities. Probabilities for extreme categories (above P90 and below P10) can also be included as hatched areas. Individual ensemble members can be plotted as jittered points. The observed value is optionally shown as a diamond. +This function plots the probability distribution function of +several ensemble forecasts. Separate panels are used to plot forecasts valid +or initialized at different times or by different models or even at different +locations. Probabilities for tercile categories are computed, plotted in +colors and annotated. An asterisk marks the tercile with higher probabilities. +Probabilities for extreme categories (above P90 and below P10) can also be +included as hatched areas. Individual ensemble members can be plotted as +jittered points. The observed value is optionally shown as a diamond. } \examples{ fcsts <- data.frame(fcst1 = rnorm(10), fcst2 = rnorm(10, 0.5, 1.2), -- GitLab