From 714b315787d1102268b780bef801ca42949b9e9a Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Thu, 2 Nov 2023 14:31:46 +0100 Subject: [PATCH] Reveiw and remove unnecessary dependencies of PlotWeeklyClim and PlotForecastPDF --- NAMESPACE | 3 +-- R/PlotForecastPDF.R | 7 ++----- R/PlotWeeklyClim.R | 45 ++++++++++++++++++++++--------------------- man/PlotWeeklyClim.Rd | 11 ++++++----- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a6a4232..1d3bbc6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,7 +21,6 @@ import(graphics) import(maps) import(multiApply) import(rnaturalearth) -import(scales) import(sf) importFrom(CSTools,SplitDim) importFrom(ClimProjDiags,Subset) @@ -57,11 +56,11 @@ importFrom(graphics,points) importFrom(graphics,polygon) importFrom(graphics,text) importFrom(graphics,title) -importFrom(lubridate,ymd) importFrom(maps,map) importFrom(plyr,.) importFrom(plyr,dlply) importFrom(reshape2,melt) importFrom(s2dv,InsertDim) importFrom(s2dv,MeanDims) +importFrom(scales,date_format) importFrom(stats,cor) diff --git a/R/PlotForecastPDF.R b/R/PlotForecastPDF.R index 0868809..80ab857 100644 --- a/R/PlotForecastPDF.R +++ b/R/PlotForecastPDF.R @@ -47,13 +47,10 @@ #'@examples #'fcsts <- data.frame(fcst1 = rnorm(10), fcst2 = rnorm(10, 0.5, 1.2)) #'PlotForecastPDF(fcsts,c(-1,1)) -#'@importFrom data.table data.table -#'@importFrom data.table CJ -#'@importFrom data.table setkey #'@import ggplot2 +#'@importFrom data.table data.table CJ setkey #'@importFrom reshape2 melt -#'@importFrom plyr . -#'@importFrom plyr dlply +#'@importFrom plyr . dlply #'@importFrom s2dv InsertDim #'@export PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = NULL, diff --git a/R/PlotWeeklyClim.R b/R/PlotWeeklyClim.R index 4fa829b..bc1c0e2 100644 --- a/R/PlotWeeklyClim.R +++ b/R/PlotWeeklyClim.R @@ -5,11 +5,12 @@ #'a specified period (reference period) to the observed conditions during the #'target period analyzed in the case study. #' -#'@param data A multidimensional array with named dimensions with at least sdate -#' and time dimensions containing observed daily data. It can also be a -#' dataframe with computed percentiles as input for ggplot. If it's a -#' dataframe, it must contain the following column names: 'week', 'clim', -#' 'p10', 'p90', 'p33', 'p66', 'week_mean', 'day' and 'data'. +#'@param data A multidimensional array with named start date and lead time +#' dimensions; if other dimensions are found, the function drops them with +#' the first index. It can also be a dataframe with computed percentiles as +#' input for ggplot. If it's a dataframe, it must contain the following column +#' names: 'week', 'clim', 'p10', 'p90', 'p33', 'p66', 'week_mean', 'day' and +#' 'data'. #'@param first_date The first date of the observed values of timeseries. It can #' be of class 'Date', 'POSIXct' or a character string in the format #' 'yyyy-mm-dd'. If parameter 'data_years' is not provided, it must be a date @@ -72,11 +73,10 @@ #'@import multiApply #'@import ggplot2 #'@import RColorBrewer -#'@import scales +#'@importFrom scales date_format #'@importFrom ClimProjDiags Subset #'@importFrom s2dv MeanDims #'@importFrom CSTools SplitDim -#'@importFrom lubridate ymd #'@export PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, data_years = NULL, time_dim = 'time', @@ -145,8 +145,8 @@ PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, "indicating the date in the format 'yyyy-mm-dd', 'POSIXct' ", "or 'Dates' class.")) } - first_date <- lubridate::ymd(first_date) - target_year <- lubridate::year(first_date) + first_date <- as.Date(first_date) + target_year <- format(first_date, "%Y") taget_year_outside_reference <- FALSE # data_years if (!is.null(data_years)) { @@ -187,15 +187,15 @@ PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, "indicating the date in the format 'yyyy-mm-dd', 'POSIXct' ", "or 'Dates' class.")) } - last_date <- lubridate::ymd(last_date) + last_date <- as.Date(last_date) dates <- seq(first_date, last_date, by = "1 day") if (length(dates) > dim(data)[time_dim]) { warning(paste0("Parameter 'last_date' is greater than the last date ", "of 'data'. The last date of 'data' will be used.")) - dates <- seq(first_date, first_date + days(dim(data)[time_dim]-1), by = "1 day") + dates <- seq(first_date, first_date + dim(data)[time_dim]-1, by = "1 day") } } else { - dates <- seq(first_date, first_date + days(dim(data)[time_dim]-1), by = "1 day") + dates <- seq(first_date, first_date + dim(data)[time_dim]-1, by = "1 day") } # ylim if (is.character(ylim)) { @@ -216,7 +216,8 @@ PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, dims_subset <- names(dim(data_subset))[which(!names(dim(data_subset)) %in% c(time_dim, sdate_dim))] if (!identical(dims_subset, character(0))) { data_subset <- ClimProjDiags::Subset(data_subset, dims_subset, - as.list(rep(1, length(dims_subset))), drop = TRUE) + as.list(rep(1, length(dims_subset))), + drop = TRUE) } # observed daily data creation daily <- ClimProjDiags::Subset(data_subset, along = sdate_dim, @@ -236,14 +237,14 @@ PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, weekly_means <- s2dv::MeanDims(weekly_aggre, time_dim) weekly_clim <- s2dv::MeanDims(weekly_means, sdate_dim) - weekly_p10 <- Apply(weekly_means, target_dims = sdate_dim, - fun = function(x) {quantile(x, 0.10)})$output1 - weekly_p90 <- Apply(weekly_means, target_dims = sdate_dim, - fun = function(x) {quantile(x, 0.90)})$output1 - weekly_p33 <- Apply(weekly_means, target_dims = sdate_dim, - fun = function(x) {quantile(x, 0.33)})$output1 - weekly_p66 <- Apply(weekly_means, target_dims = sdate_dim, - fun = function(x) {quantile(x, 0.66)})$output1 + weekly_p10 <- apply(weekly_means, MARGIN = which(names(dim(weekly_means)) == 'week'), + FUN = quantile, 0.10) + weekly_p90 <- apply(weekly_means, MARGIN = which(names(dim(weekly_means)) == 'week'), + FUN = quantile, 0.90) + weekly_p33 <- apply(weekly_means, MARGIN = which(names(dim(weekly_means)) == 'week'), + FUN = quantile, 0.33) + weekly_p66 <- apply(weekly_means, MARGIN = which(names(dim(weekly_means)) == 'week'), + FUN = quantile, 0.66) clim <- p10 <- p90 <- p33 <- p66 <- NULL weekly_data <- data.frame(clim = as.vector(weekly_clim), @@ -286,7 +287,7 @@ PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, ggtitle(title, subtitle = subtitle) + scale_x_date(breaks = seq(min(all$day), max(all$day), by = "7 days"), minor_breaks = NULL, expand = c(0.03, 0.03), - labels = date_format("%d %b %Y")) + + labels = scales::date_format("%d %b %Y")) + theme(axis.text.x = element_text(angle = 45, hjust = 1), panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "gray92"), diff --git a/man/PlotWeeklyClim.Rd b/man/PlotWeeklyClim.Rd index 3e064e8..92276a6 100644 --- a/man/PlotWeeklyClim.Rd +++ b/man/PlotWeeklyClim.Rd @@ -27,11 +27,12 @@ PlotWeeklyClim( ) } \arguments{ -\item{data}{A multidimensional array with named dimensions with at least sdate -and time dimensions containing observed daily data. It can also be a -dataframe with computed percentiles as input for ggplot. If it's a -dataframe, it must contain the following column names: 'week', 'clim', -'p10', 'p90', 'p33', 'p66', 'week_mean', 'day' and 'data'.} +\item{data}{A multidimensional array with named start date and lead time +dimensions; if other dimensions are found, the function drops them with +the first index. It can also be a dataframe with computed percentiles as +input for ggplot. If it's a dataframe, it must contain the following column +names: 'week', 'clim', 'p10', 'p90', 'p33', 'p66', 'week_mean', 'day' and +'data'.} \item{first_date}{The first date of the observed values of timeseries. It can be of class 'Date', 'POSIXct' or a character string in the format -- GitLab