From 705c45f9cbee7f5dd490e798c5c1f37335533338 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 8 Nov 2023 10:49:33 +0100 Subject: [PATCH 1/4] Rename CSTools functions to new name --- NAMESPACE | 13 +- R/{PlotCombinedMap.R => VizCombinedMap.R} | 91 ++++++-------- R/{PlotForecastPDF.R => VizForecastPDF.R} | 118 +++++++++--------- ...antileMap.R => VizMostLikelyQuantileMap.R} | 44 ++++--- R/{PlotPDFsOLE.R => VizPDFsOLE.R} | 16 +-- ...Categories.R => VizTriangles4Categories.R} | 34 ++--- R/{PlotWeeklyClim.R => VizWeeklyClim.R} | 38 +++--- man/{PlotCombinedMap.Rd => VizCombinedMap.Rd} | 47 +++---- man/{PlotForecastPDF.Rd => VizForecastPDF.Rd} | 10 +- ...tileMap.Rd => VizMostLikelyQuantileMap.Rd} | 30 ++--- man/{PlotPDFsOLE.Rd => VizPDFsOLE.Rd} | 12 +- ...tegories.Rd => VizTriangles4Categories.Rd} | 20 +-- man/{PlotWeeklyClim.Rd => VizWeeklyClim.Rd} | 20 +-- 13 files changed, 229 insertions(+), 264 deletions(-) rename R/{PlotCombinedMap.R => VizCombinedMap.R} (85%) rename R/{PlotForecastPDF.R => VizForecastPDF.R} (84%) rename R/{PlotMostLikelyQuantileMap.R => VizMostLikelyQuantileMap.R} (83%) rename R/{PlotPDFsOLE.R => VizPDFsOLE.R} (95%) rename R/{PlotTriangles4Categories.R => VizTriangles4Categories.R} (90%) rename R/{PlotWeeklyClim.R => VizWeeklyClim.R} (91%) rename man/{PlotCombinedMap.Rd => VizCombinedMap.Rd} (81%) rename man/{PlotForecastPDF.Rd => VizForecastPDF.Rd} (95%) rename man/{PlotMostLikelyQuantileMap.Rd => VizMostLikelyQuantileMap.Rd} (84%) rename man/{PlotPDFsOLE.Rd => VizPDFsOLE.Rd} (91%) rename man/{PlotTriangles4Categories.Rd => VizTriangles4Categories.Rd} (89%) rename man/{PlotWeeklyClim.Rd => VizWeeklyClim.Rd} (89%) diff --git a/NAMESPACE b/NAMESPACE index 94b5cc2..a5dbed7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,12 +4,6 @@ export(ClimColors) export(ClimPalette) export(ColorBarContinuous) export(ColorBarDiscrete) -export(PlotCombinedMap) -export(PlotForecastPDF) -export(PlotMostLikelyQuantileMap) -export(PlotPDFsOLE) -export(PlotTriangles4Categories) -export(PlotWeeklyClim) export(ShapeToMask) export(Viz2VarsVsLTime) export(VizACC) @@ -17,13 +11,19 @@ export(VizAnimateMap) export(VizAno) export(VizBoxWhisker) export(VizClim) +export(VizCombinedMap) export(VizEquiMap) +export(VizForecastPDF) export(VizLayout) export(VizMatrix) +export(VizMostLikelyQuantileMap) +export(VizPDFsOLE) export(VizRobinson) export(VizSection) export(VizStereoMap) +export(VizTriangles4Categories) export(VizVsLTime) +export(VizWeeklyClim) import(RColorBrewer) import(cowplot) import(easyNCDF) @@ -31,7 +31,6 @@ import(ggplot2) import(graphics) import(mapproj) import(maps) -import(multiApply) import(rnaturalearth) import(sf) import(stats) diff --git a/R/PlotCombinedMap.R b/R/VizCombinedMap.R similarity index 85% rename from R/PlotCombinedMap.R rename to R/VizCombinedMap.R index 5316185..5961958 100644 --- a/R/PlotCombinedMap.R +++ b/R/VizCombinedMap.R @@ -35,15 +35,6 @@ #' colour bar will be automatically interpolated to match the number of breaks. #' Each item in this list can be named, and the name will be used as title for #' the corresponding colour bar (equivalent to the parameter 'bar_titles'). -#'@param bar_limits A numeric vector of 2 indicating the range of color bar. -#' The default is NULL, and the function will decide the range automatically. -#'@param triangle_ends A logical vector of two indicating if the lower and upper -#' triangles of the color bar should be plotted. The default is -#' c(FALSE, FALSE). -#'@param col_inf A character string of recognized color name or code indicating -#' the color of the lower triangle of the color bar. The default is NULL. -#'@param col_sup A character string of recognized color name or code indicating -#' the color of the upper triangle of the color bar. The default is NULL. #'@param col_unknown_map Colour to use to paint the grid cells for which a map #' is not possible to be chosen according to 'map_select_fun' or for those #' values that go beyond 'display_range'. Takes the value 'white' by default. @@ -69,9 +60,6 @@ #' following order: bottom, left, top, and right. If not specified, use the #' default of par("mar"), c(5.1, 4.1, 4.1, 2.1). Used as 'margin_scale' in #' VizEquiMap. -#'@param bar_extra_margin A numeric vector of 4 indicating the extra margins to -#' be added around the color bar, in the format c(y1, x1, y2, x2). The units -#' are margin lines. The default values are c(2, 0, 2, 0). #'@param fileout File where to save the plot. If not specified (default) a #' graphics device will pop up. Extensions allowed: eps/ps, jpeg, png, pdf, bmp #' and tiff @@ -107,12 +95,12 @@ #'lons <- seq(0, 359.5, length = 20) #'lats <- seq(-89.5, 89.5, length = 10) #'\dontrun{ -#'PlotCombinedMap(list(a, b, c), lons, lats, -#' toptitle = 'Maximum map', -#' map_select_fun = max, -#' display_range = c(0, 1), -#' bar_titles = paste('% of belonging to', c('a', 'b', 'c')), -#' brks = 20, width = 12, height = 10) +#'VizCombinedMap(list(a, b, c), lons, lats, +#' toptitle = 'Maximum map', +#' map_select_fun = max, +#' display_range = c(0, 1), +#' bar_titles = paste('% of belonging to', c('a', 'b', 'c')), +#' brks = 20, width = 12, height = 10) #'} #' #'Lon <- c(0:40, 350:359) @@ -122,34 +110,29 @@ #'mask <- sample(c(0,1), replace = TRUE, size = 51 * 26) #'dim(mask) <- c(lat = 26, lon = 51) #'\dontrun{ -#'PlotCombinedMap(data, lon = Lon, lat = Lat, map_select_fun = max, -#' display_range = range(data), mask = mask, -#' width = 14, height = 10) +#'VizCombinedMap(data, lon = Lon, lat = Lat, map_select_fun = max, +#' display_range = range(data), mask = mask, +#' width = 14, height = 10) #'} #' -#'@seealso \code{PlotCombinedMap} and \code{VizEquiMap} -#' -#'@import utils +#'@seealso \code{VizCombinedMap} and \code{VizEquiMap} +#' #'@importFrom maps map #'@importFrom graphics box image layout mtext par plot.new #'@importFrom grDevices adjustcolor bmp colorRampPalette dev.cur dev.new dev.off #' hcl jpeg pdf png postscript svg tiff #'@export -PlotCombinedMap <- function(maps, lon, lat, - map_select_fun, display_range, - map_dim = 'map', - brks = NULL, cols = NULL, - bar_limits = NULL, triangle_ends = c(FALSE, FALSE), - col_inf = NULL, col_sup = NULL, - col_unknown_map = 'white', - mask = NULL, col_mask = 'grey', - dots = NULL, - bar_titles = NULL, legend_scale = 1, - cex_bar_titles = 1.5, - plot_margin = NULL, bar_extra_margin = c(2, 0, 2, 0), - fileout = NULL, width = 8, height = 5, - size_units = 'in', res = 100, drawleg = T, return_leg = FALSE, - ...) { +VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, + map_dim = 'map', brks = NULL, cols = NULL, + bar_limits = NULL, triangle_ends = c(F, F), + col_inf = NULL, col_sup = NULL, + col_unknown_map = 'white', mask = NULL, + col_mask = 'grey', dots = NULL, bar_titles = NULL, + legend_scale = 1, cex_bar_titles = 1.5, + plot_margin = NULL, bar_extra_margin = c(2, 0, 2, 0), + fileout = NULL, width = 8, height = 5, + size_units = 'in', res = 100, drawleg = T, + return_leg = FALSE, ...) { args <- list(...) # If there is any filenames to store the graphics, process them @@ -329,7 +312,7 @@ PlotCombinedMap <- function(maps, lon, lat, #---------------------- #TODO: Consider col_inf if (!is.null(colorbar$col_inf[[1]])) { - warning("Lower triangle is not supported now. Please contact maintainer if you have this need.") + .warning("Lower triangle is not supported now. Please contact maintainer if you have this need.") } if (!is.null(colorbar$col_sup[[1]])) { @@ -463,8 +446,9 @@ PlotCombinedMap <- function(maps, lon, lat, } VizEquiMap(var = ml_map, lon = lon, lat = lat, - brks = tbrks, cols = tcols, drawleg = FALSE, - filled.continents = FALSE, dots = dots, margin_scale = plot_margin, ...) + brks = tbrks, cols = tcols, drawleg = FALSE, + filled.continents = FALSE, dots = dots, + margin_scale = plot_margin, ...) #---------------------- # Add overplot on top @@ -511,12 +495,13 @@ PlotCombinedMap <- function(maps, lon, lat, if (drawleg & !return_leg) { GradientCatsColorBar(nmap = dim(maps)[map_dim], - brks = colorbar$brks, cols = colorbar$cols, vertical = FALSE, - subsampleg = NULL, bar_limits = bar_limits, - var_limits = var_limits_maps, - triangle_ends = triangle_ends, col_inf = colorbar$col_inf, col_sup = colorbar$col_sup, - plot = TRUE, draw_separators = TRUE, - bar_titles = bar_titles, title_scale = cex_bar_titles, label_scale = legend_scale * 1.5, + brks = colorbar$brks, cols = colorbar$cols, + vertical = FALSE, subsampleg = NULL, + bar_limits = bar_limits, var_limits = var_limits_maps, + triangle_ends = triangle_ends, col_inf = colorbar$col_inf, + col_sup = colorbar$col_sup, plot = TRUE, draw_separators = TRUE, + bar_titles = bar_titles, title_scale = cex_bar_titles, + label_scale = legend_scale * 1.5, extra_margin = bar_extra_margin) } @@ -529,12 +514,12 @@ PlotCombinedMap <- function(maps, lon, lat, tmp <- list(nmap = dim(maps)[map_dim], brks = colorbar$brks, cols = colorbar$cols, vertical = FALSE, subsampleg = NULL, bar_limits = bar_limits, - var_limits = var_limits_maps, - triangle_ends = triangle_ends, col_inf = colorbar$col_inf, col_sup = colorbar$col_sup, - plot = TRUE, draw_separators = TRUE, - bar_titles = bar_titles, title_scale = cex_bar_titles, label_scale = legend_scale * 1.5, + var_limits = var_limits_maps, triangle_ends = triangle_ends, + col_inf = colorbar$col_inf, col_sup = colorbar$col_sup, + plot = TRUE, draw_separators = TRUE, bar_titles = bar_titles, + title_scale = cex_bar_titles, label_scale = legend_scale * 1.5, extra_margin = bar_extra_margin) - warning("The device is not off yet. Use dev.off() after plotting the color bars.") + .warning("The device is not off yet. Use dev.off() after plotting the color bars.") return(tmp) #NOTE: The device is not off! Can keep plotting the color bars. } diff --git a/R/PlotForecastPDF.R b/R/VizForecastPDF.R similarity index 84% rename from R/PlotForecastPDF.R rename to R/VizForecastPDF.R index 77e26e4..4dffe78 100644 --- a/R/PlotForecastPDF.R +++ b/R/VizForecastPDF.R @@ -46,23 +46,23 @@ #'@return A ggplot object containing the plot. #'@examples #'fcsts <- data.frame(fcst1 = rnorm(10), fcst2 = rnorm(10, 0.5, 1.2)) -#'PlotForecastPDF(fcsts,c(-1,1)) -#'@import ggplot2 stats +#'VizForecastPDF(fcsts,c(-1,1)) +#'@import ggplot2 #'@importFrom data.table data.table CJ setkey #'@importFrom reshape2 melt #'@importFrom plyr . dlply #'@importFrom s2dv InsertDim #'@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", "vitigeoss"), - memb_dim = 'member') { - value <- init <- extremes <- x <- ymin <- ymax <- tercile <- NULL +VizForecastPDF <- 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", "vitigeoss"), + memb_dim = 'member') { + value <- init <- extremes <- x <- ymin <- ymax <- tercile <- NULL y <- xend <- yend <- yjitter <- MLT <- lab.pos <- NULL ggColorHue <- function(n) { - hues <- seq(15, 375, length = n + 1) - hcl(h = hues, l = 65, c = 100)[1:n] + hues <- seq(15, 375, length = n + 1) + hcl(h = hues, l = 65, c = 100)[1:n] } #------------------------ # Define color sets @@ -301,8 +301,8 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N } else { plot <- plot + geom_segment(data = hatch.df[hatch.df$extremes != "Normal", ], - aes(x = x, y = y, - xend = xend, yend = yend, color = extremes)) + aes(x = x, y = y, + xend = xend, yend = yend, color = extremes)) } } #------------------------ @@ -311,8 +311,8 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N if (!is.null(obs)) { plot <- plot + geom_vline(data = obs.dt, - aes(xintercept = value), - linetype = "dashed", color = colorObs) + aes(xintercept = value), + linetype = "dashed", color = colorObs) } #------------------------ # Add ensemble members @@ -321,22 +321,22 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N plot <- plot + # this adds a grey box for ensmembers geom_rect(aes(xmin = -Inf, xmax = Inf, - ymin = -Inf, ymax = -pan.height / 10), - fill = "gray95", color = "black", width = 0.2) + + ymin = -Inf, ymax = -pan.height / 10), + fill = "gray95", color = "black", width = 0.2) + # this adds the ensemble members geom_point(data = jitter.df, - aes(x = x, - y = -pan.height / 10 - magic.ratio * yjitter, - shape = "Ensemble members"), - color = "black", fill = colorMember, alpha = 1) + aes(x = x, + y = -pan.height / 10 - magic.ratio * yjitter, + shape = "Ensemble members"), + color = "black", fill = colorMember, alpha = 1) } else if (add.ensmemb == "above") { plot <- plot + geom_point(data = jitter.df, - aes(x = x, - y = 0.7 * magic.ratio * yjitter, - shape = "Ensemble members"), - color = "black", fill = colorMember, alpha = 1) + aes(x = x, + y = 0.7 * magic.ratio * yjitter, + shape = "Ensemble members"), + color = "black", fill = colorMember, alpha = 1) } #------------------------ @@ -346,8 +346,8 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N plot <- plot + # this adds the obs diamond geom_point(data = obs.xy, - aes(x = x, y = ymax, size = "Observation"), - shape = 23, color = "black", fill = colorObs) + aes(x = x, y = ymax, size = "Observation"), + shape = 23, color = "black", fill = colorObs) } #------------------------ # Compute probability for each tercile and identify MLT @@ -357,9 +357,9 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N by = .(init, tercile)] # include potentially missing groups pct <- merge(pct, CJ(init = factor(levels(pct$init), levels = levels(pct$init)), - tercile = factor(c("Below normal", "Normal", "Above normal"), - levels = c("Above normal", "Normal", "Below normal"))), - by = c("init", "tercile"), all.y = T) + tercile = factor(c("Below normal", "Normal", "Above normal"), + levels = c("Above normal", "Normal", "Below normal"))), + by = c("init", "tercile"), all.y = T) pct[is.na(pct),"pct"] <- 0 tot <- pct[, .(tot = sum(pct)), by = init] pct <- merge(pct, tot, by = "init") @@ -375,9 +375,9 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N by = .(init, extremes)] # include potentially missing groups pct2 <- merge(pct2, CJ(init = factor(levels(pct2$init), levels = levels(pct2$init)), - extremes = factor(c("Below P10", "Normal", "Above P90"), - levels = c("Above P90", "Normal", "Below P10"))), - by = c("init", "extremes"), all.y=T) + extremes = factor(c("Below P10", "Normal", "Above P90"), + levels = c("Above P90", "Normal", "Below P10"))), + by = c("init", "extremes"), all.y=T) pct2[is.na(pct),"pct"] <- 0 tot2 <- pct2[, .(tot = sum(pct)), by = init] pct2 <- merge(pct2, tot2, by = "init") @@ -398,23 +398,23 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N } plot <- plot + geom_text(data = pct, - aes(x = lab.pos, y = labpos, label = paste0(pct, "%"), - hjust = as.integer(tercile) * -1.5 + 3.5), - vjust = vjust, angle = -90, size = 3.2) + + aes(x = lab.pos, y = labpos, label = paste0(pct, "%"), + hjust = as.integer(tercile) * -1.5 + 3.5), + vjust = vjust, angle = -90, size = 3.2) + geom_text(data = pct[MLT == T, ], - aes(x = lab.pos, y = labpos, label = "*", - hjust = as.integer(tercile) * -3.5 + 9), - vjust = 0.1, angle = -90, size = 7, color = "black") + aes(x = lab.pos, y = labpos, label = "*", + hjust = as.integer(tercile) * -3.5 + 9), + vjust = 0.1, angle = -90, size = 7, color = "black") #------------------------ # Add probability labels for extremes #------------------------ if (!is.null(extreme.limits)) { plot <- plot + geom_text(data = pct2[extremes != "Normal", ], - aes(x = lab.pos, y = 0.9 * y, label = paste0(pct, "%"), - hjust = as.integer(extremes) * -1.5 + 3.5), - vjust = -0.5, angle = -90, size = 3.2, - color = rep(colorLab, dim(fcst.df)[2])) + aes(x = lab.pos, y = 0.9 * y, label = paste0(pct, "%"), + hjust = as.integer(extremes) * -1.5 + 3.5), + vjust = -0.5, angle = -90, size = 3.2, + color = rep(colorLab, dim(fcst.df)[2])) } #------------------------ # Finish all theme and legend details @@ -422,29 +422,29 @@ PlotForecastPDF <- function(fcst, tercile.limits, extreme.limits = NULL, obs = N plot <- plot + theme_minimal() + scale_fill_manual(name = "Probability of\nterciles", - values = colorFill, drop = F) + + values = colorFill, drop = F) + scale_color_manual(name = "Probability of\nextremes", - values = colorHatch) + + values = colorHatch) + scale_shape_manual(name = "Ensemble\nmembers", - values = c(21)) + + values = c(21)) + scale_size_manual(name = "Observation", - values = c(3)) + + values = c(3)) + labs(x = var.name, - y = "Probability density\n(total area=1)", - title = title) + + y = "Probability density\n(total area=1)", + title = title) + theme(axis.text.x = element_blank(), - panel.grid.minor.x = element_blank(), - legend.key.size = unit(0.3, "in"), - panel.border = element_rect(fill = NA, color = "black"), - strip.background = element_rect(colour = "black", fill = "gray80"), - panel.spacing = unit(0.2, "in"), - panel.grid.major.x = element_line(color = "grey93"), - panel.background = element_rect(fill = "white"), - plot.background = element_rect(fill = "white", color = NA)) + + panel.grid.minor.x = element_blank(), + legend.key.size = unit(0.3, "in"), + panel.border = element_rect(fill = NA, color = "black"), + strip.background = element_rect(colour = "black", fill = "gray80"), + panel.spacing = unit(0.2, "in"), + panel.grid.major.x = element_line(color = "grey93"), + panel.background = element_rect(fill = "white"), + plot.background = element_rect(fill = "white", color = NA)) + guides(fill = guide_legend(order = 1), - color = guide_legend(order = 2), - shape = guide_legend(order = 3, label = F), - size = guide_legend(order = 4, label = F)) + color = guide_legend(order = 2), + shape = guide_legend(order = 3, label = F), + size = guide_legend(order = 4, label = F)) #------------------------ # Save to plotfile if needed, and return plot #------------------------ diff --git a/R/PlotMostLikelyQuantileMap.R b/R/VizMostLikelyQuantileMap.R similarity index 83% rename from R/PlotMostLikelyQuantileMap.R rename to R/VizMostLikelyQuantileMap.R index a432805..2de037c 100644 --- a/R/PlotMostLikelyQuantileMap.R +++ b/R/VizMostLikelyQuantileMap.R @@ -38,9 +38,9 @@ #' 'down', 'd', 'D', 'bottom', 'b', 'B', 'south', 's', 'S' (default)\cr #' 'right', 'r', 'R', 'east', 'e', 'E'\cr #' 'left', 'l', 'L', 'west', 'w', 'W' -#'@param ... Additional parameters to be sent to \code{PlotCombinedMap} and +#'@param ... Additional parameters to be sent to \code{VizCombinedMap} and #' \code{PlotEquiMap}. -#'@seealso \code{PlotCombinedMap} and \code{PlotEquiMap} +#'@seealso \code{VizCombinedMap} and \code{PlotEquiMap} #'@examples #'# Simple example #'x <- array(1:(20 * 10), dim = c(lat = 10, lon = 20)) / 200 @@ -50,10 +50,10 @@ #'lons <- seq(0, 359.5, length = 20) #'lats <- seq(-89.5, 89.5, length = 10) #'\dontrun{ -#'PlotMostLikelyQuantileMap(list(a, b, c), lons, lats, -#' toptitle = 'Most likely tercile map', -#' bar_titles = paste('% of belonging to', c('a', 'b', 'c')), -#' brks = 20, width = 10, height = 8) +#'VizMostLikelyQuantileMap(list(a, b, c), lons, lats, +#' toptitle = 'Most likely tercile map', +#' bar_titles = paste('% of belonging to', c('a', 'b', 'c')), +#' brks = 20, width = 10, height = 8) #'} #' #'# More complex example @@ -122,20 +122,20 @@ #' #'# 3. Plotting most likely quantile/bin #'\dontrun{ -#'PlotMostLikelyQuantileMap(bins, lons, lats, -#' toptitle = 'Most likely quantile map', -#' bar_titles = paste('% of belonging to', letters[1:n_bins]), -#' mask = 1 - (w1 + w2 / max(c(w1, w2))), -#' brks = 20, width = 10, height = 8) +#'VizMostLikelyQuantileMap(bins, lons, lats, +#' toptitle = 'Most likely quantile map', +#' bar_titles = paste('% of belonging to', letters[1:n_bins]), +#' mask = 1 - (w1 + w2 / max(c(w1, w2))), +#' brks = 20, width = 10, height = 8) #'} #'@importFrom maps map #'@importFrom graphics box image layout mtext par plot.new #'@importFrom grDevices adjustcolor bmp colorRampPalette dev.cur dev.new dev.off hcl jpeg pdf png postscript svg tiff #'@export -PlotMostLikelyQuantileMap <- function(probs, lon, lat, cat_dim = 'bin', - bar_titles = NULL, - col_unknown_cat = 'white', drawleg = T, - ...) { +VizMostLikelyQuantileMap <- function(probs, lon, lat, cat_dim = 'bin', + bar_titles = NULL, + col_unknown_cat = 'white', drawleg = T, + ...) { # Check probs error <- FALSE if (is.list(probs)) { @@ -210,13 +210,11 @@ PlotMostLikelyQuantileMap <- function(probs, lon, lat, cat_dim = 'bin', minimum_value <- ceiling(1 / nprobs * 10 * 1.1) * 10 - # By now, the PlotCombinedMap function is included below in this file. - # In the future, PlotCombinedMap will be part of s2dverification and will + # By now, the VizCombinedMap function is included below in this file. + # In the future, VizCombinedMap will be part of s2dverification and will # be properly imported. - PlotCombinedMap(probs * 100, lon, lat, map_select_fun = max, - display_range = c(minimum_value, 100), - map_dim = cat_dim, - bar_titles = bar_titles, - col_unknown_map = col_unknown_cat, - drawleg = drawleg, ...) + VizCombinedMap(probs * 100, lon, lat, map_select_fun = max, + display_range = c(minimum_value, 100), map_dim = cat_dim, + bar_titles = bar_titles, col_unknown_map = col_unknown_cat, + drawleg = drawleg, ...) } \ No newline at end of file diff --git a/R/PlotPDFsOLE.R b/R/VizPDFsOLE.R similarity index 95% rename from R/PlotPDFsOLE.R rename to R/VizPDFsOLE.R index a8b55bb..2232321 100644 --- a/R/PlotPDFsOLE.R +++ b/R/VizPDFsOLE.R @@ -29,7 +29,7 @@ #'@param dpi (optional) A numeric value indicating the plot resolution. #' (Default dpi = 300). #' -#'@return PlotPDFsOLE() returns a ggplot object containing the plot. +#'@return VizPDFsOLE() returns a ggplot object containing the plot. #' #'@examples #'# Example 1 @@ -40,12 +40,12 @@ #'attr(pdf_2, "name") <- "NAO2" #'dim(pdf_2) <- c(statistic = 2) #' -#'PlotPDFsOLE(pdf_1, pdf_2) +#'VizPDFsOLE(pdf_1, pdf_2) #'@import ggplot2 stats #'@export -PlotPDFsOLE <- function(pdf_1, pdf_2, nsigma = 3, legendPos = 'bottom', - legendSize = 1.0, plotfile = NULL, width = 30, - height = 15, units = "cm", dpi = 300) { +VizPDFsOLE <- 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)){ @@ -252,8 +252,8 @@ CombinedPDFs <- function(pdf_1, pdf_2) { return(data) } -dnorm_limit <- function(x,mean,sd){ - y <- dnorm(x,mean,sd) - y[x mean+sd] <- NA +dnorm_limit <- function(x, mean, sd){ + y <- dnorm(x, mean, sd) + y[x < mean | x > mean + sd] <- NA return(y) } diff --git a/R/PlotTriangles4Categories.R b/R/VizTriangles4Categories.R similarity index 90% rename from R/PlotTriangles4Categories.R rename to R/VizTriangles4Categories.R index 501536c..15a9dd7 100644 --- a/R/PlotTriangles4Categories.R +++ b/R/VizTriangles4Categories.R @@ -66,28 +66,28 @@ #'names(dim(arr1)) <- c('dimx', 'dimy', 'dimcat') #'arr2 <- array(TRUE, dim = dim(arr1)) #'arr2[which(arr1 < 0.3)] <- FALSE -#'PlotTriangles4Categories(data = arr1, -#' cols = c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59'), -#' brks = c(-1, 0, 0.1, 0.2, 0.3, 0.4), -#' lab_legend = c('NAO+', 'BL','AR','NAO-'), -#' xtitle = "Target month", ytitle = "Lead time", -#' xlabels = c("Jan", "Feb", "Mar", "Apr")) +#'VizTriangles4Categories(data = arr1, +#' cols = c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59'), +#' brks = c(-1, 0, 0.1, 0.2, 0.3, 0.4), +#' lab_legend = c('NAO+', 'BL','AR','NAO-'), +#' xtitle = "Target month", ytitle = "Lead time", +#' xlabels = c("Jan", "Feb", "Mar", "Apr")) #'@importFrom grDevices dev.new dev.off dev.cur #'@importFrom graphics plot points polygon text title axis #'@importFrom RColorBrewer brewer.pal #'@importFrom ClimProjDiags Subset #'@export -PlotTriangles4Categories <- function(data, brks = NULL, cols = NULL, - toptitle = NULL, sig_data = NULL, - pch_sig = 18, col_sig = 'black', - cex_sig = 1, xlab = TRUE, ylab = TRUE, - xlabels = NULL, xtitle = NULL, - ylabels = NULL, ytitle = NULL, - legend = TRUE, lab_legend = NULL, - cex_leg = 1, col_leg = 'black', - cex_axis = 1.5, mar = c(5, 4, 0, 0), - fileout = NULL, size_units = 'px', - res = 100, figure.width = 1, ...) { +VizTriangles4Categories <- function(data, brks = NULL, cols = NULL, + toptitle = NULL, sig_data = NULL, + pch_sig = 18, col_sig = 'black', + cex_sig = 1, xlab = TRUE, ylab = TRUE, + xlabels = NULL, xtitle = NULL, + ylabels = NULL, ytitle = NULL, + legend = TRUE, lab_legend = NULL, + cex_leg = 1, col_leg = 'black', + cex_axis = 1.5, mar = c(5, 4, 0, 0), + fileout = NULL, size_units = 'px', + res = 100, figure.width = 1, ...) { # Checking the dimensions if (length(dim(data)) != 3) { stop("Parameter 'data' must be an array with three dimensions.") diff --git a/R/PlotWeeklyClim.R b/R/VizWeeklyClim.R similarity index 91% rename from R/PlotWeeklyClim.R rename to R/VizWeeklyClim.R index 5ca8e38..3fbe62b 100644 --- a/R/PlotWeeklyClim.R +++ b/R/VizWeeklyClim.R @@ -63,26 +63,26 @@ #' #'@examples #'data <- array(rnorm(49*20*3, 274), dim = c(time = 49, sdate = 20, member = 3)) -#'PlotWeeklyClim(data = data, first_date = '2002-08-09', -#' last_date = '2002-09-15', ref_period = 2010:2019, -#' data_years = 2000:2019, time_dim = 'time', sdate_dim = 'sdate', -#' title = "Observed weekly means and climatology", -#' subtitle = "Target years: 2010 to 2019", -#' ytitle = paste0('tas', " (", "deg.C", ")")) +#'VizWeeklyClim(data = data, first_date = '2002-08-09', +#' last_date = '2002-09-15', ref_period = 2010:2019, +#' data_years = 2000:2019, time_dim = 'time', sdate_dim = 'sdate', +#' title = "Observed weekly means and climatology", +#' subtitle = "Target years: 2010 to 2019", +#' ytitle = paste0('tas', " (", "deg.C", ")")) #' -#'@import multiApply ggplot2 RColorBrewer stats +#'@import ggplot2 RColorBrewer stats #'@importFrom scales date_format #'@importFrom ClimProjDiags Subset #'@importFrom s2dv MeanDims #'@importFrom CSTools SplitDim #'@export -PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, - data_years = NULL, time_dim = 'time', - sdate_dim = 'sdate', ylim = NULL, - title = NULL, subtitle = NULL, - ytitle = NULL, legend = TRUE, - palette = "Blues", fileout = NULL, device = NULL, - width = 8, height = 6, units = 'in', dpi = 300) { +VizWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, + data_years = NULL, time_dim = 'time', + sdate_dim = 'sdate', ylim = NULL, + title = NULL, subtitle = NULL, + ytitle = NULL, legend = TRUE, + palette = "Blues", fileout = NULL, device = NULL, + width = 8, height = 6, units = 'in', dpi = 300) { ## Check input arguments # data if (is.array(data)) { @@ -274,22 +274,22 @@ PlotWeeklyClim <- function(data, first_date, ref_period, last_date = NULL, alpha = 0.7, show.legend = legend) + # terciles clim geom_line(aes(y = clim, group = week, color = "climatological mean", linetype = "climatological mean"), - alpha = 1.0, size = 0.7, show.legend = legend) + # mean clim + alpha = 1.0, linewidth = 0.7, show.legend = legend) + # mean clim geom_line(aes(y = data, color = "observed daily mean", linetype = "observed daily mean"), - alpha = 1, size = 0.2, show.legend = legend) + # daily evolution + alpha = 1, linewidth = 0.2, show.legend = legend) + # daily evolution geom_line(aes(y = week_mean, group = week, color = "observed weekly mean", linetype = "observed weekly mean"), - alpha = 1, size = 0.7, show.legend = legend) + # weekly evolution + alpha = 1, linewidth = 0.7, show.legend = legend) + # weekly evolution theme_bw() + ylab(ytitle) + xlab(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 = 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', + panel.grid.major = element_line(linewidth = 0.5, linetype = 'solid', colour = "gray92"), - panel.grid.minor = element_line(size = 0.25, linetype = 'solid', + panel.grid.minor = element_line(linewidth = 0.25, linetype = 'solid', colour = "gray92"), legend.spacing = unit(-0.2, "cm")) + scale_fill_manual(name = NULL, diff --git a/man/PlotCombinedMap.Rd b/man/VizCombinedMap.Rd similarity index 81% rename from man/PlotCombinedMap.Rd rename to man/VizCombinedMap.Rd index 6461400..a81449a 100644 --- a/man/PlotCombinedMap.Rd +++ b/man/VizCombinedMap.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PlotCombinedMap.R -\name{PlotCombinedMap} -\alias{PlotCombinedMap} +% Please edit documentation in R/VizCombinedMap.R +\name{VizCombinedMap} +\alias{VizCombinedMap} \title{Plot Multiple Lon-Lat Variables In a Single Map According to a Decision Function} \usage{ -PlotCombinedMap( +VizCombinedMap( maps, lon, lat, @@ -14,7 +14,7 @@ PlotCombinedMap( brks = NULL, cols = NULL, bar_limits = NULL, - triangle_ends = c(FALSE, FALSE), + triangle_ends = c(F, F), col_inf = NULL, col_sup = NULL, col_unknown_map = "white", @@ -73,19 +73,6 @@ colour bar will be automatically interpolated to match the number of breaks. Each item in this list can be named, and the name will be used as title for the corresponding colour bar (equivalent to the parameter 'bar_titles').} -\item{bar_limits}{A numeric vector of 2 indicating the range of color bar. -The default is NULL, and the function will decide the range automatically.} - -\item{triangle_ends}{A logical vector of two indicating if the lower and upper -triangles of the color bar should be plotted. The default is -c(FALSE, FALSE).} - -\item{col_inf}{A character string of recognized color name or code indicating -the color of the lower triangle of the color bar. The default is NULL.} - -\item{col_sup}{A character string of recognized color name or code indicating -the color of the upper triangle of the color bar. The default is NULL.} - \item{col_unknown_map}{Colour to use to paint the grid cells for which a map is not possible to be chosen according to 'map_select_fun' or for those values that go beyond 'display_range'. Takes the value 'white' by default.} @@ -119,10 +106,6 @@ following order: bottom, left, top, and right. If not specified, use the default of par("mar"), c(5.1, 4.1, 4.1, 2.1). Used as 'margin_scale' in VizEquiMap.} -\item{bar_extra_margin}{A numeric vector of 4 indicating the extra margins to -be added around the color bar, in the format c(y1, x1, y2, x2). The units -are margin lines. The default values are c(2, 0, 2, 0).} - \item{fileout}{File where to save the plot. If not specified (default) a graphics device will pop up. Extensions allowed: eps/ps, jpeg, png, pdf, bmp and tiff} @@ -170,12 +153,12 @@ c <- 1 - (a + b) lons <- seq(0, 359.5, length = 20) lats <- seq(-89.5, 89.5, length = 10) \dontrun{ -PlotCombinedMap(list(a, b, c), lons, lats, - toptitle = 'Maximum map', - map_select_fun = max, - display_range = c(0, 1), - bar_titles = paste('\% of belonging to', c('a', 'b', 'c')), - brks = 20, width = 12, height = 10) +VizCombinedMap(list(a, b, c), lons, lats, + toptitle = 'Maximum map', + map_select_fun = max, + display_range = c(0, 1), + bar_titles = paste('\% of belonging to', c('a', 'b', 'c')), + brks = 20, width = 12, height = 10) } Lon <- c(0:40, 350:359) @@ -185,14 +168,14 @@ dim(data) <- c(map = 3, lon = 51, lat = 26) mask <- sample(c(0,1), replace = TRUE, size = 51 * 26) dim(mask) <- c(lat = 26, lon = 51) \dontrun{ -PlotCombinedMap(data, lon = Lon, lat = Lat, map_select_fun = max, - display_range = range(data), mask = mask, - width = 14, height = 10) +VizCombinedMap(data, lon = Lon, lat = Lat, map_select_fun = max, + display_range = range(data), mask = mask, + width = 14, height = 10) } } \seealso{ -\code{PlotCombinedMap} and \code{VizEquiMap} +\code{VizCombinedMap} and \code{VizEquiMap} } \author{ Nicolau Manubens, \email{nicolau.manubens@bsc.es} diff --git a/man/PlotForecastPDF.Rd b/man/VizForecastPDF.Rd similarity index 95% rename from man/PlotForecastPDF.Rd rename to man/VizForecastPDF.Rd index 31e3001..cec38a5 100644 --- a/man/PlotForecastPDF.Rd +++ b/man/VizForecastPDF.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PlotForecastPDF.R -\name{PlotForecastPDF} -\alias{PlotForecastPDF} +% Please edit documentation in R/VizForecastPDF.R +\name{VizForecastPDF} +\alias{VizForecastPDF} \title{Plot one or multiple ensemble forecast pdfs for the same event} \usage{ -PlotForecastPDF( +VizForecastPDF( fcst, tercile.limits, extreme.limits = NULL, @@ -77,7 +77,7 @@ 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)) -PlotForecastPDF(fcsts,c(-1,1)) +VizForecastPDF(fcsts,c(-1,1)) } \author{ Llorenç Lledó \email{llledo@bsc.es} diff --git a/man/PlotMostLikelyQuantileMap.Rd b/man/VizMostLikelyQuantileMap.Rd similarity index 84% rename from man/PlotMostLikelyQuantileMap.Rd rename to man/VizMostLikelyQuantileMap.Rd index 0dde63f..7b8ee9a 100644 --- a/man/PlotMostLikelyQuantileMap.Rd +++ b/man/VizMostLikelyQuantileMap.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PlotMostLikelyQuantileMap.R -\name{PlotMostLikelyQuantileMap} -\alias{PlotMostLikelyQuantileMap} +% Please edit documentation in R/VizMostLikelyQuantileMap.R +\name{VizMostLikelyQuantileMap} +\alias{VizMostLikelyQuantileMap} \title{Plot Maps of Most Likely Quantiles} \usage{ -PlotMostLikelyQuantileMap( +VizMostLikelyQuantileMap( probs, lon, lat, @@ -48,7 +48,7 @@ FALSE or:\cr 'right', 'r', 'R', 'east', 'e', 'E'\cr 'left', 'l', 'L', 'west', 'w', 'W'} -\item{...}{Additional parameters to be sent to \code{PlotCombinedMap} and +\item{...}{Additional parameters to be sent to \code{VizCombinedMap} and \code{PlotEquiMap}.} } \description{ @@ -72,10 +72,10 @@ c <- 1 - (a + b) lons <- seq(0, 359.5, length = 20) lats <- seq(-89.5, 89.5, length = 10) \dontrun{ -PlotMostLikelyQuantileMap(list(a, b, c), lons, lats, - toptitle = 'Most likely tercile map', - bar_titles = paste('\% of belonging to', c('a', 'b', 'c')), - brks = 20, width = 10, height = 8) +VizMostLikelyQuantileMap(list(a, b, c), lons, lats, + toptitle = 'Most likely tercile map', + bar_titles = paste('\% of belonging to', c('a', 'b', 'c')), + brks = 20, width = 10, height = 8) } # More complex example @@ -144,15 +144,15 @@ bins <- multiApply::Apply(sample_data, 'time', binning, thresholds)$output1 # 3. Plotting most likely quantile/bin \dontrun{ -PlotMostLikelyQuantileMap(bins, lons, lats, - toptitle = 'Most likely quantile map', - bar_titles = paste('\% of belonging to', letters[1:n_bins]), - mask = 1 - (w1 + w2 / max(c(w1, w2))), - brks = 20, width = 10, height = 8) +VizMostLikelyQuantileMap(bins, lons, lats, + toptitle = 'Most likely quantile map', + bar_titles = paste('\% of belonging to', letters[1:n_bins]), + mask = 1 - (w1 + w2 / max(c(w1, w2))), + brks = 20, width = 10, height = 8) } } \seealso{ -\code{PlotCombinedMap} and \code{PlotEquiMap} +\code{VizCombinedMap} and \code{PlotEquiMap} } \author{ Veronica Torralba, \email{veronica.torralba@bsc.es}, Nicolau Manubens, diff --git a/man/PlotPDFsOLE.Rd b/man/VizPDFsOLE.Rd similarity index 91% rename from man/PlotPDFsOLE.Rd rename to man/VizPDFsOLE.Rd index e2c6606..165b595 100644 --- a/man/PlotPDFsOLE.Rd +++ b/man/VizPDFsOLE.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PlotPDFsOLE.R -\name{PlotPDFsOLE} -\alias{PlotPDFsOLE} +% Please edit documentation in R/VizPDFsOLE.R +\name{VizPDFsOLE} +\alias{VizPDFsOLE} \title{Plotting two probability density gaussian functions and the optimal linear estimation (OLE) as result of combining them.} \usage{ -PlotPDFsOLE( +VizPDFsOLE( pdf_1, pdf_2, nsigma = 3, @@ -52,7 +52,7 @@ unit. (Default units = 'cm').} (Default dpi = 300).} } \value{ -PlotPDFsOLE() returns a ggplot object containing the plot. +VizPDFsOLE() returns a ggplot object containing the plot. } \description{ This function plots two probability density gaussian functions @@ -67,7 +67,7 @@ pdf_2 <- c(1,0.5) attr(pdf_2, "name") <- "NAO2" dim(pdf_2) <- c(statistic = 2) -PlotPDFsOLE(pdf_1, pdf_2) +VizPDFsOLE(pdf_1, pdf_2) } \author{ Eroteida Sanchez-Garcia - AEMET, //email{esanchezg@aemet.es} diff --git a/man/PlotTriangles4Categories.Rd b/man/VizTriangles4Categories.Rd similarity index 89% rename from man/PlotTriangles4Categories.Rd rename to man/VizTriangles4Categories.Rd index 62a076e..b28c1a0 100644 --- a/man/PlotTriangles4Categories.Rd +++ b/man/VizTriangles4Categories.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PlotTriangles4Categories.R -\name{PlotTriangles4Categories} -\alias{PlotTriangles4Categories} +% Please edit documentation in R/VizTriangles4Categories.R +\name{VizTriangles4Categories} +\alias{VizTriangles4Categories} \title{Function to convert any 3-d numerical array to a grid of coloured triangles.} \usage{ -PlotTriangles4Categories( +VizTriangles4Categories( data, brks = NULL, cols = NULL, @@ -123,12 +123,12 @@ arr1 <- array(runif(n = 4 * 5 * 4, min = -1, max = 1), dim = c(4,5,4)) names(dim(arr1)) <- c('dimx', 'dimy', 'dimcat') arr2 <- array(TRUE, dim = dim(arr1)) arr2[which(arr1 < 0.3)] <- FALSE -PlotTriangles4Categories(data = arr1, - cols = c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59'), - brks = c(-1, 0, 0.1, 0.2, 0.3, 0.4), - lab_legend = c('NAO+', 'BL','AR','NAO-'), - xtitle = "Target month", ytitle = "Lead time", - xlabels = c("Jan", "Feb", "Mar", "Apr")) +VizTriangles4Categories(data = arr1, + cols = c('white','#fef0d9','#fdd49e','#fdbb84','#fc8d59'), + brks = c(-1, 0, 0.1, 0.2, 0.3, 0.4), + lab_legend = c('NAO+', 'BL','AR','NAO-'), + xtitle = "Target month", ytitle = "Lead time", + xlabels = c("Jan", "Feb", "Mar", "Apr")) } \author{ History:\cr diff --git a/man/PlotWeeklyClim.Rd b/man/VizWeeklyClim.Rd similarity index 89% rename from man/PlotWeeklyClim.Rd rename to man/VizWeeklyClim.Rd index 92276a6..bba0005 100644 --- a/man/PlotWeeklyClim.Rd +++ b/man/VizWeeklyClim.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/PlotWeeklyClim.R -\name{PlotWeeklyClim} -\alias{PlotWeeklyClim} +% Please edit documentation in R/VizWeeklyClim.R +\name{VizWeeklyClim} +\alias{VizWeeklyClim} \title{Plots the observed weekly means and climatology of a timeseries data} \usage{ -PlotWeeklyClim( +VizWeeklyClim( data, first_date, ref_period, @@ -110,11 +110,11 @@ target period analyzed in the case study. } \examples{ data <- array(rnorm(49*20*3, 274), dim = c(time = 49, sdate = 20, member = 3)) -PlotWeeklyClim(data = data, first_date = '2002-08-09', - last_date = '2002-09-15', ref_period = 2010:2019, - data_years = 2000:2019, time_dim = 'time', sdate_dim = 'sdate', - title = "Observed weekly means and climatology", - subtitle = "Target years: 2010 to 2019", - ytitle = paste0('tas', " (", "deg.C", ")")) +VizWeeklyClim(data = data, first_date = '2002-08-09', + last_date = '2002-09-15', ref_period = 2010:2019, + data_years = 2000:2019, time_dim = 'time', sdate_dim = 'sdate', + title = "Observed weekly means and climatology", + subtitle = "Target years: 2010 to 2019", + ytitle = paste0('tas', " (", "deg.C", ")")) } -- GitLab From 0bdd46de4c867d00ec65c08a3abe89d76e71af5a Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 8 Nov 2023 10:59:50 +0100 Subject: [PATCH 2/4] Correct branch differences --- R/VizCombinedMap.R | 70 +++++++++++++++++++++++++------------------ man/VizCombinedMap.Rd | 33 +++++++++++++++----- 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/R/VizCombinedMap.R b/R/VizCombinedMap.R index 5961958..1feefb3 100644 --- a/R/VizCombinedMap.R +++ b/R/VizCombinedMap.R @@ -35,6 +35,15 @@ #' colour bar will be automatically interpolated to match the number of breaks. #' Each item in this list can be named, and the name will be used as title for #' the corresponding colour bar (equivalent to the parameter 'bar_titles'). +#'@param bar_limits A numeric vector of 2 indicating the range of color bar. +#' The default is NULL, and the function will decide the range automatically. +#'@param triangle_ends A logical vector of two indicating if the lower and upper +#' triangles of the color bar should be plotted. The default is +#' c(FALSE, FALSE). +#'@param col_inf A character string of recognized color name or code indicating +#' the color of the lower triangle of the color bar. The default is NULL. +#'@param col_sup A character string of recognized color name or code indicating +#' the color of the upper triangle of the color bar. The default is NULL. #'@param col_unknown_map Colour to use to paint the grid cells for which a map #' is not possible to be chosen according to 'map_select_fun' or for those #' values that go beyond 'display_range'. Takes the value 'white' by default. @@ -60,6 +69,9 @@ #' following order: bottom, left, top, and right. If not specified, use the #' default of par("mar"), c(5.1, 4.1, 4.1, 2.1). Used as 'margin_scale' in #' VizEquiMap. +#'@param bar_extra_margin A numeric vector of 4 indicating the extra margins to +#' be added around the color bar, in the format c(y1, x1, y2, x2). The units +#' are margin lines. The default values are c(2, 0, 2, 0). #'@param fileout File where to save the plot. If not specified (default) a #' graphics device will pop up. Extensions allowed: eps/ps, jpeg, png, pdf, bmp #' and tiff @@ -96,11 +108,11 @@ #'lats <- seq(-89.5, 89.5, length = 10) #'\dontrun{ #'VizCombinedMap(list(a, b, c), lons, lats, -#' toptitle = 'Maximum map', -#' map_select_fun = max, -#' display_range = c(0, 1), -#' bar_titles = paste('% of belonging to', c('a', 'b', 'c')), -#' brks = 20, width = 12, height = 10) +#' toptitle = 'Maximum map', +#' map_select_fun = max, +#' display_range = c(0, 1), +#' bar_titles = paste('% of belonging to', c('a', 'b', 'c')), +#' brks = 20, width = 12, height = 10) #'} #' #'Lon <- c(0:40, 350:359) @@ -111,12 +123,13 @@ #'dim(mask) <- c(lat = 26, lon = 51) #'\dontrun{ #'VizCombinedMap(data, lon = Lon, lat = Lat, map_select_fun = max, -#' display_range = range(data), mask = mask, -#' width = 14, height = 10) +#' display_range = range(data), mask = mask, +#' width = 14, height = 10) #'} #' #'@seealso \code{VizCombinedMap} and \code{VizEquiMap} -#' +#' +#'@import utils #'@importFrom maps map #'@importFrom graphics box image layout mtext par plot.new #'@importFrom grDevices adjustcolor bmp colorRampPalette dev.cur dev.new dev.off @@ -124,12 +137,13 @@ #'@export VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, map_dim = 'map', brks = NULL, cols = NULL, - bar_limits = NULL, triangle_ends = c(F, F), - col_inf = NULL, col_sup = NULL, - col_unknown_map = 'white', mask = NULL, - col_mask = 'grey', dots = NULL, bar_titles = NULL, - legend_scale = 1, cex_bar_titles = 1.5, - plot_margin = NULL, bar_extra_margin = c(2, 0, 2, 0), + bar_limits = NULL, triangle_ends = c(FALSE, FALSE), + col_inf = NULL, col_sup = NULL, + col_unknown_map = 'white', + mask = NULL, col_mask = 'grey', dots = NULL, + bar_titles = NULL, legend_scale = 1, + cex_bar_titles = 1.5, plot_margin = NULL, + bar_extra_margin = c(2, 0, 2, 0), fileout = NULL, width = 8, height = 5, size_units = 'in', res = 100, drawleg = T, return_leg = FALSE, ...) { @@ -312,7 +326,7 @@ VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, #---------------------- #TODO: Consider col_inf if (!is.null(colorbar$col_inf[[1]])) { - .warning("Lower triangle is not supported now. Please contact maintainer if you have this need.") + warning("Lower triangle is not supported now. Please contact maintainer if you have this need.") } if (!is.null(colorbar$col_sup[[1]])) { @@ -447,8 +461,7 @@ VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, VizEquiMap(var = ml_map, lon = lon, lat = lat, brks = tbrks, cols = tcols, drawleg = FALSE, - filled.continents = FALSE, dots = dots, - margin_scale = plot_margin, ...) + filled.continents = FALSE, dots = dots, margin_scale = plot_margin, ...) #---------------------- # Add overplot on top @@ -495,13 +508,12 @@ VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, if (drawleg & !return_leg) { GradientCatsColorBar(nmap = dim(maps)[map_dim], - brks = colorbar$brks, cols = colorbar$cols, - vertical = FALSE, subsampleg = NULL, - bar_limits = bar_limits, var_limits = var_limits_maps, - triangle_ends = triangle_ends, col_inf = colorbar$col_inf, - col_sup = colorbar$col_sup, plot = TRUE, draw_separators = TRUE, - bar_titles = bar_titles, title_scale = cex_bar_titles, - label_scale = legend_scale * 1.5, + brks = colorbar$brks, cols = colorbar$cols, vertical = FALSE, + subsampleg = NULL, bar_limits = bar_limits, + var_limits = var_limits_maps, + triangle_ends = triangle_ends, col_inf = colorbar$col_inf, col_sup = colorbar$col_sup, + plot = TRUE, draw_separators = TRUE, + bar_titles = bar_titles, title_scale = cex_bar_titles, label_scale = legend_scale * 1.5, extra_margin = bar_extra_margin) } @@ -514,12 +526,12 @@ VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, tmp <- list(nmap = dim(maps)[map_dim], brks = colorbar$brks, cols = colorbar$cols, vertical = FALSE, subsampleg = NULL, bar_limits = bar_limits, - var_limits = var_limits_maps, triangle_ends = triangle_ends, - col_inf = colorbar$col_inf, col_sup = colorbar$col_sup, - plot = TRUE, draw_separators = TRUE, bar_titles = bar_titles, - title_scale = cex_bar_titles, label_scale = legend_scale * 1.5, + var_limits = var_limits_maps, + triangle_ends = triangle_ends, col_inf = colorbar$col_inf, col_sup = colorbar$col_sup, + plot = TRUE, draw_separators = TRUE, + bar_titles = bar_titles, title_scale = cex_bar_titles, label_scale = legend_scale * 1.5, extra_margin = bar_extra_margin) - .warning("The device is not off yet. Use dev.off() after plotting the color bars.") + warning("The device is not off yet. Use dev.off() after plotting the color bars.") return(tmp) #NOTE: The device is not off! Can keep plotting the color bars. } diff --git a/man/VizCombinedMap.Rd b/man/VizCombinedMap.Rd index a81449a..fc59267 100644 --- a/man/VizCombinedMap.Rd +++ b/man/VizCombinedMap.Rd @@ -14,7 +14,7 @@ VizCombinedMap( brks = NULL, cols = NULL, bar_limits = NULL, - triangle_ends = c(F, F), + triangle_ends = c(FALSE, FALSE), col_inf = NULL, col_sup = NULL, col_unknown_map = "white", @@ -73,6 +73,19 @@ colour bar will be automatically interpolated to match the number of breaks. Each item in this list can be named, and the name will be used as title for the corresponding colour bar (equivalent to the parameter 'bar_titles').} +\item{bar_limits}{A numeric vector of 2 indicating the range of color bar. +The default is NULL, and the function will decide the range automatically.} + +\item{triangle_ends}{A logical vector of two indicating if the lower and upper +triangles of the color bar should be plotted. The default is +c(FALSE, FALSE).} + +\item{col_inf}{A character string of recognized color name or code indicating +the color of the lower triangle of the color bar. The default is NULL.} + +\item{col_sup}{A character string of recognized color name or code indicating +the color of the upper triangle of the color bar. The default is NULL.} + \item{col_unknown_map}{Colour to use to paint the grid cells for which a map is not possible to be chosen according to 'map_select_fun' or for those values that go beyond 'display_range'. Takes the value 'white' by default.} @@ -106,6 +119,10 @@ following order: bottom, left, top, and right. If not specified, use the default of par("mar"), c(5.1, 4.1, 4.1, 2.1). Used as 'margin_scale' in VizEquiMap.} +\item{bar_extra_margin}{A numeric vector of 4 indicating the extra margins to +be added around the color bar, in the format c(y1, x1, y2, x2). The units +are margin lines. The default values are c(2, 0, 2, 0).} + \item{fileout}{File where to save the plot. If not specified (default) a graphics device will pop up. Extensions allowed: eps/ps, jpeg, png, pdf, bmp and tiff} @@ -154,11 +171,11 @@ lons <- seq(0, 359.5, length = 20) lats <- seq(-89.5, 89.5, length = 10) \dontrun{ VizCombinedMap(list(a, b, c), lons, lats, - toptitle = 'Maximum map', - map_select_fun = max, - display_range = c(0, 1), - bar_titles = paste('\% of belonging to', c('a', 'b', 'c')), - brks = 20, width = 12, height = 10) + toptitle = 'Maximum map', + map_select_fun = max, + display_range = c(0, 1), + bar_titles = paste('\% of belonging to', c('a', 'b', 'c')), + brks = 20, width = 12, height = 10) } Lon <- c(0:40, 350:359) @@ -169,8 +186,8 @@ mask <- sample(c(0,1), replace = TRUE, size = 51 * 26) dim(mask) <- c(lat = 26, lon = 51) \dontrun{ VizCombinedMap(data, lon = Lon, lat = Lat, map_select_fun = max, - display_range = range(data), mask = mask, - width = 14, height = 10) + display_range = range(data), mask = mask, + width = 14, height = 10) } } -- GitLab From 7d0cb65f326efbd22c4db6247fd3dfc4b7e46638 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 8 Nov 2023 11:03:52 +0100 Subject: [PATCH 3/4] Minor space changes in functiion input arguments --- R/VizCombinedMap.R | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/R/VizCombinedMap.R b/R/VizCombinedMap.R index 1feefb3..9583a0e 100644 --- a/R/VizCombinedMap.R +++ b/R/VizCombinedMap.R @@ -135,18 +135,23 @@ #'@importFrom grDevices adjustcolor bmp colorRampPalette dev.cur dev.new dev.off #' hcl jpeg pdf png postscript svg tiff #'@export -VizCombinedMap <- function(maps, lon, lat, map_select_fun, display_range, - map_dim = 'map', brks = NULL, cols = NULL, +VizCombinedMap <- function(maps, lon, lat, + map_select_fun, display_range, + map_dim = 'map', + brks = NULL, cols = NULL, bar_limits = NULL, triangle_ends = c(FALSE, FALSE), col_inf = NULL, col_sup = NULL, col_unknown_map = 'white', - mask = NULL, col_mask = 'grey', dots = NULL, + mask = NULL, col_mask = 'grey', + dots = NULL, bar_titles = NULL, legend_scale = 1, - cex_bar_titles = 1.5, plot_margin = NULL, - bar_extra_margin = c(2, 0, 2, 0), + cex_bar_titles = 1.5, + plot_margin = NULL, bar_extra_margin = c(2, 0, 2, 0), fileout = NULL, width = 8, height = 5, - size_units = 'in', res = 100, drawleg = T, - return_leg = FALSE, ...) { + size_units = 'in', res = 100, drawleg = T, return_leg = FALSE, + ...) { + + args <- list(...) # If there is any filenames to store the graphics, process them -- GitLab From 26ed12b42c60c06adf34b1106d82fa6809b62152 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Wed, 8 Nov 2023 11:05:08 +0100 Subject: [PATCH 4/4] Correct spaces --- R/VizCombinedMap.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/VizCombinedMap.R b/R/VizCombinedMap.R index 9583a0e..5119385 100644 --- a/R/VizCombinedMap.R +++ b/R/VizCombinedMap.R @@ -150,8 +150,6 @@ VizCombinedMap <- function(maps, lon, lat, fileout = NULL, width = 8, height = 5, size_units = 'in', res = 100, drawleg = T, return_leg = FALSE, ...) { - - args <- list(...) # If there is any filenames to store the graphics, process them -- GitLab