From 35654c17085d83a03b966e229b3e93a0ab31cc85 Mon Sep 17 00:00:00 2001 From: Eroteida Sanchez Garcia Date: Tue, 28 Jul 2020 07:31:47 +0000 Subject: [PATCH 1/4] Adding parameter 'legendPos' and 'legendSize' to select the legend's position --- R/PlotPDFsOLE.R | 40 ++++++++++++++++++++++++++----- man/PlotPDFsOLE.Rd | 24 +++++++++++++++++-- tests/testthat/test-PlotPDFsOLE.R | 16 +++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/R/PlotPDFsOLE.R b/R/PlotPDFsOLE.R index c7239c73..8838b51f 100644 --- a/R/PlotPDFsOLE.R +++ b/R/PlotPDFsOLE.R @@ -14,6 +14,10 @@ #' to combining. #' @param nsigma (optional) A numeric value for setting the limits of X axis. #' (Default nsigma = 3). +#' @param legendPos (optional) A character value for setting the position of the +#' legend ("bottom", "top", "right" or "left")(Default 'bottom'). +#' @param legendSize (optional) A numeric value for setting the size of the +#' legend text. (Default 1.0). #' @param plotfile (optional) A filename where the plot will be saved. #' (Default: the plot is not saved). #' @param width (optional) A numeric value indicating the plot width in @@ -40,11 +44,24 @@ #' #' PlotPDFsOLE(pdf_1, pdf_2) #' +#' # Example 2 +#' Glosea5PDF <- c(2.25, 0.67) +#' attr(Glosea5PDF, "name") <- "Glosea5" +#' dim(Glosea5PDF) <- c(statistic = 2) +#' ECMWFPDF <- c(2.38, 0.61) +#' attr(ECMWFPDF, "name") <- "ECMWF" +#' dim(ECMWFPDF) <- c(statistic = 2) +#' MFPDF <- c(4.52, 0.34) +#' attr(MFPDF, "name") <- "MF" +#' dim(MFPDF) <- c(statistic = 2) +#' PlotPDFsOLE(pdf_1 = Glosea5PDF, pdf_2 = ECMWFPDF, legendPos = 'left') +#' PlotPDFsOLE(pdf_1 = Glosea5PDF, pdf_2 = MFPDF, legendPos = 'top') +#' PlotPDFsOLE(pdf_1 = ECMWFPDF, pdf_2 = MFPDF, legendSize = 1.2) + #'@export -PlotPDFsOLE <- function(pdf_1, pdf_2, nsigma = 3, plotfile = NULL, - width = 30, height = 15, - units = "cm", dpi = 300) { - +PlotPDFsOLE <- function(pdf_1, pdf_2, nsigma = 3, legendPos = 'bottom', + legendSize = 1.0, plotfile = NULL, width = 30, + height = 15, units = "cm", dpi = 300) { if(!is.null(plotfile)){ if (!is.numeric(dpi)) { @@ -87,6 +104,15 @@ PlotPDFsOLE <- function(pdf_1, pdf_2, nsigma = 3, plotfile = NULL, "indicating the path and name of output png file.") } } + if (!is.character(legendPos)) { + stop("Parameter 'legendPos' must be character") + } + if(!(legendPos %in% c("bottom", "top", "right", "left"))) { + stop("Parameter 'legendPos' must be equal to 'bottom', 'top', 'right' or 'left'.") + } + if (!is.numeric(legendSize)) { + stop("Parameter 'legendSize' must be numeric.") + } if (!is.numeric(nsigma)) { stop("Parameter 'nsigma' must be numeric.") } @@ -196,14 +222,16 @@ PlotPDFsOLE <- function(pdf_1, pdf_2, nsigma = 3, plotfile = NULL, g <- g + scale_colour_manual(values = cols, limits = c(name1, name2, nameBest), labels = c(label1, label2, labelBest)) + g <- g + theme(plot.title=element_text(size=rel(1.1), colour="black", face= "bold"), axis.text.x = element_text(size=rel(1.2)), axis.text.y = element_text(size=rel(1.2)), axis.title.x = element_blank(), legend.title = element_blank(), - legend.position = c(1,1), legend.justification = c(1,1), - legend.text = element_text(face = "bold")) + legend.position = legendPos, + legend.text = element_text(face = "bold", size=rel(legendSize))) + g <- g + ggtitle(graphicTitle) g <- g + labs(y="probability", size=rel(1.9)) g <- g + stat_function(fun = dnorm_limit, args = list(mean=mean1, sd=sigma1), diff --git a/man/PlotPDFsOLE.Rd b/man/PlotPDFsOLE.Rd index 6980cc56..070bb8b2 100644 --- a/man/PlotPDFsOLE.Rd +++ b/man/PlotPDFsOLE.Rd @@ -5,8 +5,9 @@ \title{Plotting two probability density gaussian functions and the optimal linear estimation (OLE) as result of combining them.} \usage{ -PlotPDFsOLE(pdf_1, pdf_2, nsigma = 3, plotfile = NULL, width = 30, - height = 15, units = "cm", dpi = 300) +PlotPDFsOLE(pdf_1, pdf_2, nsigma = 3, legendPos = "bottom", + legendSize = 1, plotfile = NULL, width = 30, height = 15, + units = "cm", dpi = 300) } \arguments{ \item{pdf_1}{A numeric array with a dimension named 'statistic', containg @@ -20,6 +21,12 @@ two parameters: mean' and 'standard deviation' of the second gaussian pdf \item{nsigma}{(optional) A numeric value for setting the limits of X axis. (Default nsigma = 3).} +\item{legendPos}{(optional) A character value for setting the position of the +legend ("bottom", "top", "right" or "left")(Default 'bottom').} + +\item{legendSize}{(optional) A numeric value for setting the size of the +legend text. (Default 1.0).} + \item{plotfile}{(optional) A filename where the plot will be saved. (Default: the plot is not saved).} @@ -53,6 +60,19 @@ dim(pdf_2) <- c(statistic = 2) PlotPDFsOLE(pdf_1, pdf_2) +# Example 2 +Glosea5PDF <- c(2.25, 0.67) +attr(Glosea5PDF, "name") <- "Glosea5" +dim(Glosea5PDF) <- c(statistic = 2) +ECMWFPDF <- c(2.38, 0.61) +attr(ECMWFPDF, "name") <- "ECMWF" +dim(ECMWFPDF) <- c(statistic = 2) +MFPDF <- c(4.52, 0.34) +attr(MFPDF, "name") <- "MF" +dim(MFPDF) <- c(statistic = 2) +PlotPDFsOLE(pdf_1 = Glosea5PDF, pdf_2 = ECMWFPDF, legendPos = 'left') +PlotPDFsOLE(pdf_1 = Glosea5PDF, pdf_2 = MFPDF, legendPos = 'top') +PlotPDFsOLE(pdf_1 = ECMWFPDF, pdf_2 = MFPDF, legendSize = 1.2) } \author{ Eroteida Sanchez-Garcia - AEMET, //email{esanchezg@aemet.es} diff --git a/tests/testthat/test-PlotPDFsOLE.R b/tests/testthat/test-PlotPDFsOLE.R index bd1279d9..91e61d08 100644 --- a/tests/testthat/test-PlotPDFsOLE.R +++ b/tests/testthat/test-PlotPDFsOLE.R @@ -39,6 +39,22 @@ test_that("Sanity checks", { paste0("Parameter 'plotfile' must be a character string ", "indicating the path and name of output png file.")) + expect_error(PlotPDFsOLE(pdf_1, pdf_2, legendPos = 1, plotfile = NULL, + width = 30, height = 15, + units = "cm", dpi = 300) , + "Parameter 'legendPos' must be character") + + expect_error(PlotPDFsOLE(pdf_1, pdf_2, legendPos = 'arriba', + plotfile = "plot.png", + width = 30, height = 15, + units = "cm", dpi = 300) , + "Parameter 'legendPos' must be equal to 'bottom', 'top', 'right' or 'left'.") + + expect_error(PlotPDFsOLE(pdf_1, pdf_2, legendSize = '3', plotfile = NULL, + width = 30, height = 15, + units = "cm", dpi = 300) , + "Parameter 'legendSize' must be numeric.") + expect_error(PlotPDFsOLE(pdf_1, pdf_2, nsigma = '3', plotfile = NULL, width = 30, height = 15, units = "cm", dpi = 300) , -- GitLab From 70a8c19a32a31008fdd49c336f7ba885bcb57b7b Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 26 Oct 2020 11:57:00 +0100 Subject: [PATCH 2/4] Documentation updated with devtools --- man/PlotPDFsOLE.Rd | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/man/PlotPDFsOLE.Rd b/man/PlotPDFsOLE.Rd index 070bb8b2..ff3c568e 100644 --- a/man/PlotPDFsOLE.Rd +++ b/man/PlotPDFsOLE.Rd @@ -5,9 +5,18 @@ \title{Plotting two probability density gaussian functions and the optimal linear estimation (OLE) as result of combining them.} \usage{ -PlotPDFsOLE(pdf_1, pdf_2, nsigma = 3, legendPos = "bottom", - legendSize = 1, plotfile = NULL, width = 30, height = 15, - units = "cm", dpi = 300) +PlotPDFsOLE( + pdf_1, + pdf_2, + nsigma = 3, + legendPos = "bottom", + legendSize = 1, + plotfile = NULL, + width = 30, + height = 15, + units = "cm", + dpi = 300 +) } \arguments{ \item{pdf_1}{A numeric array with a dimension named 'statistic', containg -- GitLab From 4844c4c0f0c92c7086a10a9d82cbbad15b4f5815 Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 26 Oct 2020 12:37:16 +0100 Subject: [PATCH 3/4] adding run tests --- .Rbuildignore | 2 +- R/PlotPDFsOLE.R | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index fa596e70..b2d8e5fc 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,4 +5,4 @@ ./.nc$ .*^(?!data)\.RData$ .*\.gitlab-ci.yml$ -^tests$ +#^tests$ diff --git a/R/PlotPDFsOLE.R b/R/PlotPDFsOLE.R index 80216569..25c669a4 100644 --- a/R/PlotPDFsOLE.R +++ b/R/PlotPDFsOLE.R @@ -62,7 +62,8 @@ PlotPDFsOLE <- function(pdf_1, pdf_2, nsigma = 3, legendPos = 'bottom', legendSize = 1.0, plotfile = NULL, width = 30, height = 15, units = "cm", dpi = 300) { - + y <- type <- NULL + if(!is.null(plotfile)){ if (!is.numeric(dpi)) { stop("Parameter 'dpi' must be numeric.") -- GitLab From 8437de6ee0a42aee7c2ace890240a6fdb2d034c4 Mon Sep 17 00:00:00 2001 From: nperez Date: Mon, 26 Oct 2020 13:13:59 +0100 Subject: [PATCH 4/4] Update News and version number --- DESCRIPTION | 2 +- NEWS.md | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 07433f27..d7627660 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: CSTools Title: Assessing Skill of Climate Forecasts on Seasonal-to-Decadal Timescales -Version: 3.1.0 +Version: 4.0.0 Authors@R: c( person("Nuria", "Perez-Zanon", , "nuria.perez@bsc.es", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-8568-3071")), person("Louis-Philippe", "Caron", , "louis-philippe.caron@bsc.es", role = "aut", comment = c(ORCID = "0000-0001-5221-0147")), diff --git a/NEWS.md b/NEWS.md index fab70c70..722bd0e1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ +### CSTools 4.0.0 +**Submission date to CRAN: XX-12-2020** +- New features: + + PlotPDFsOLE includes parameters to modify legend style + ### CSTools 3.1.0 -**Submission date to CRAN: XX-06-2020** +**Submission date to CRAN: 02-07-2020** - New features: + EnsClustering vignette -- GitLab