From 7d239fc0d69e0561afa78c9113b12f6ce34a63e3 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Mon, 11 Nov 2024 16:36:48 +0100 Subject: [PATCH 01/22] Add caption parameter in description and include a check --- R/VizEquiMap.R | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 2633ceb..5a71bcf 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -38,6 +38,8 @@ #' 'title_scale'. #'@param sizetit Scale factor for the figure top title provided in parameter #' 'toptitle'. Deprecated. Use 'title_scale' instead. +#'@param caption A character string of the caption located at left-bottom of the +#' plot. #'@param units Title at the top of the colour bar, most commonly the units of #' the variable provided in parameter 'var'. #'@param brks,cols,bar_limits,triangle_ends Usually only providing 'brks' is @@ -250,8 +252,8 @@ #' @importFrom s2dv InsertDim #'@export VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, - toptitle = NULL, sizetit = NULL, units = NULL, - brks = NULL, cols = NULL, bar_limits = NULL, + toptitle = NULL, sizetit = NULL, caption = NULL, + units = NULL, brks = NULL, cols = NULL, bar_limits = NULL, triangle_ends = NULL, col_inf = NULL, col_sup = NULL, colNA = NULL, color_fun = ClimPalette(), square = TRUE, filled.continents = FALSE, @@ -523,6 +525,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, title_scale <- sizetit } + # caption + if (!is.null(caption) && !is.character(caption)) { + stop("Parameter 'caption' must be a character string.") + } + # Check include_lower_boundary and include_upper_boundary if (!is.null(include_lower_boundary) && (!is.logical(include_lower_boundary) || length(include_lower_boundary) != 1)) { stop("Parameter 'include_lower_boundary' must be a logical element.") -- GitLab From acce7052630b18425a803fcceb28eb7cd6ddf1ff Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Mon, 11 Nov 2024 17:48:46 +0100 Subject: [PATCH 02/22] Draft: inclusion of caption in plot --- R/VizEquiMap.R | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 5a71bcf..8243a38 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1159,6 +1159,14 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, col = contour_color, drawlabels = contour_draw_label) } + # + # Adding a caption + # ~~~~~~~~~~~~~~~~~ + # + if (!is.null(caption)) { + mtext(caption, side = 1, line = 2, adj = 0, cex = 0.8, col = "gray10") + } + # # Adding black dots or symbols # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1173,7 +1181,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, lwd = dot_size[counter] * 3 / sqrt(sqrt(length(var)))) } } - + # # Adding a mask # ~~~~~~~~~~~~~~~ -- GitLab From daef80ebdf55985aa9ef5731de2902aa2499bad4 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 12 Nov 2024 15:29:23 +0100 Subject: [PATCH 03/22] Set caption color to black --- R/VizEquiMap.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 8243a38..a127d32 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1019,7 +1019,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, cex_axes_labels <- 1.3 * axes_label_scale cex_axes_ticks <- -0.5 * axes_tick_scale spaceticklab <- 0 - if (axelab) { + if (axelab) { # Y axis label if (!is.null(ylabels)) { ypos <- seq(latmin, latmax, intylat) + ylatshft @@ -1164,7 +1164,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~ # if (!is.null(caption)) { - mtext(caption, side = 1, line = 2, adj = 0, cex = 0.8, col = "gray10") + mtext(caption, side = 1, line = 2, adj = 0, cex = 0.8, col = "black") } # -- GitLab From 395f2fb6ed45255e60772ba15864f1479f5b8287 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 12 Nov 2024 15:55:10 +0100 Subject: [PATCH 04/22] Add 'caption_size' parameter for setting caption size --- R/VizEquiMap.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index a127d32..1728b53 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -190,6 +190,7 @@ #' the format c(y1, x1, y2, x2). Defaults to rep(1, 4). If drawleg = TRUE, #' then margin_scale[1] is subtracted 1 unit. #'@param title_scale Scale factor for the figure top title. Defaults to 1. +#'@param caption_size Scale factor for the figure caption. Default is 0.8. #'@param numbfig Number of figures in the layout the plot will be put into. #' A higher numbfig will result in narrower margins and smaller labels, #' axe labels, ticks, thinner lines, ... Defaults to 1. @@ -281,7 +282,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, bar_extra_margin = rep(0, 4), boxlim = NULL, boxcol = 'purple2', boxlwd = 5, margin_scale = rep(1, 4), title_scale = 1, - numbfig = NULL, fileout = NULL, + caption_size = 0.8, numbfig = NULL, fileout = NULL, width = 8, height = 5, size_units = 'in', res = 100, include_lower_boundary = TRUE, include_upper_boundary = TRUE, ...) { @@ -948,6 +949,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (!is.numeric(title_scale)) { stop("Parameter 'title_scale' must be numeric.") } + + # Check caption_size + if (!is.numeric(caption_size)) { + stop("Parameter 'caption_size' must be numeric.") + } # Check axes_tick_scale if (!is.numeric(axes_tick_scale)) { @@ -1164,7 +1170,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~ # if (!is.null(caption)) { - mtext(caption, side = 1, line = 2, adj = 0, cex = 0.8, col = "black") + mtext(caption, side = 1, line = 2, adj = 0, cex = caption_size, col = "black") } # -- GitLab From dae3df087617c149a21af9363fff04eebe9e4069 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Thu, 14 Nov 2024 17:40:05 +0100 Subject: [PATCH 05/22] New parameter for vertical color bar --- R/VizEquiMap.R | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 1728b53..6dd9556 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -179,6 +179,8 @@ #'@param drawleg Whether to plot a color bar (legend, key) or not. Defaults to #' TRUE. It is not possible to plot the colour bar if 'add = TRUE'. Use #' ColorBar() and the return values of PlotEquiMap() instead. +#'@param vertical TRUE/FALSE for vertical/horizontal colour bar +#' (disregarded if plot = FALSE). Default is FALSE. #'@param boxlim Limits of a box to be added to the plot, in degrees: #' c(x1, y1, x2, y2). A list with multiple box specifications can also be #' provided. @@ -274,7 +276,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, intylat = 20, intxlon = 20, xlonshft = 0, ylatshft = 0, xlabels = NULL, ylabels = NULL, axes_tick_scale = 1, axes_label_scale = 1, - drawleg = TRUE, subsampleg = NULL, + drawleg = TRUE, vertical = FALSE, subsampleg = NULL, bar_extra_labels = NULL, draw_bar_ticks = TRUE, draw_separators = FALSE, triangle_ends_scale = 1, bar_label_digits = 4, bar_label_scale = 1, @@ -548,8 +550,8 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # draw_separators, triangle_ends_scale, label_scale, units, units_scale, # bar_label_digits # Build: brks, cols, bar_limits, col_inf, col_sup - colorbar <- ColorBarContinuous(brks, cols, FALSE, subsampleg, bar_limits, var_limits, - triangle_ends, col_inf, col_sup, color_fun, FALSE, + colorbar <- ColorBarContinuous(brks, cols, vertical = vertical, subsampleg, bar_limits, + var_limits, triangle_ends, col_inf, col_sup, color_fun, FALSE, extra_labels = bar_extra_labels, draw_ticks = draw_bar_ticks, draw_separators = draw_separators, triangle_ends_scale = triangle_ends_scale, @@ -954,6 +956,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (!is.numeric(caption_size)) { stop("Parameter 'caption_size' must be numeric.") } + if (vertical) { + if (missing(caption_size)) { + caption_size <- 1 + } + } # Check axes_tick_scale if (!is.numeric(axes_tick_scale)) { @@ -1021,6 +1028,9 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } margins <- rep(0.4, 4) * margin_scale margins[4] <- margins[4] + 1 + if (vertical) { + margins[1] <- margins[1] + 2 + } cex_title <- 2 * title_scale cex_axes_labels <- 1.3 * axes_label_scale cex_axes_ticks <- -0.5 * axes_tick_scale @@ -1096,7 +1106,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } if (drawleg) { - layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) + if (vertical) { + layout(matrix(1:2, ncol = 2, nrow = 1), widths = c(5, 1.3)) + } else { + layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) + } } plot.new() # Load the user parameters @@ -1170,7 +1184,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~ # if (!is.null(caption)) { - mtext(caption, side = 1, line = 2, adj = 0, cex = caption_size, col = "black") + if (vertical) { + mtext(caption, side = 1, line = 3, adj = 0, cex = caption_size, col = "black") + } else { + mtext(caption, side = 1, line = 2, adj = 0, cex = caption_size, col = "black") + } } # @@ -1360,8 +1378,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~ # if (drawleg) { - ColorBarContinuous(brks, cols, FALSE, subsampleg, bar_limits, var_limits, - triangle_ends, col_inf = col_inf, col_sup = col_sup, + if (vertical) { + par(mfg = c(1, 2)) + } + ColorBarContinuous(brks, cols, vertical = vertical, subsampleg, bar_limits, + var_limits, triangle_ends, col_inf = col_inf, col_sup = col_sup, extra_labels = bar_extra_labels, draw_ticks = draw_bar_ticks, draw_separators = draw_separators, title = units, title_scale = units_scale, triangle_ends_scale = triangle_ends_scale, -- GitLab From 976b6c29314ab9c92961590977595f9cda0b819c Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Mon, 9 Dec 2024 14:48:41 +0100 Subject: [PATCH 06/22] Set proportions for color bar in vertical mode --- R/VizEquiMap.R | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 6dd9556..819f8d9 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1024,12 +1024,18 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~~~~~ # if (drawleg) { - margin_scale[1] <- margin_scale[1] - 1 + margin_scale[1] <- margin_scale[1] - 1 + } margins <- rep(0.4, 4) * margin_scale margins[4] <- margins[4] + 1 - if (vertical) { - margins[1] <- margins[1] + 2 + # if (vertical) { + # margins[1] <- margins[1] + 2 + # } + if (!is.null(caption)) { + num_lines <- length(strsplit(caption, "\n")[[1]]) + } else { + num_lines <- 1 } cex_title <- 2 * title_scale cex_axes_labels <- 1.3 * axes_label_scale @@ -1107,9 +1113,13 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (vertical) { - layout(matrix(1:2, ncol = 2, nrow = 1), widths = c(5, 1.3)) + par(mar = margins) + layout(matrix(c(1, 2, 3, 3), ncol = 2, nrow = 2, byrow = TRUE), + widths = c(5, 1.3), + heights = c(5, num_lines*0.2)) + } else { - layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) + layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1 + num_lines)) # bueno el num_lines és nou també } } plot.new() @@ -1179,17 +1189,6 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, col = contour_color, drawlabels = contour_draw_label) } - # - # Adding a caption - # ~~~~~~~~~~~~~~~~~ - # - if (!is.null(caption)) { - if (vertical) { - mtext(caption, side = 1, line = 3, adj = 0, cex = caption_size, col = "black") - } else { - mtext(caption, side = 1, line = 2, adj = 0, cex = caption_size, col = "black") - } - } # # Adding black dots or symbols @@ -1373,6 +1372,20 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, #come back to the previous xpd value par(xpd = xpdsave) } + + # + # Adding a caption + # ~~~~~~~~~~~~~~~~~ + # + if (!is.null(caption)) { + if (vertical) { + par(mfg = c(2,1)) + } + base_line <- 2 + mtext(caption, side = 1, line = 1, adj = 0, + cex = caption_size, col = "black") + } + # # Colorbar # ~~~~~~~~~~ -- GitLab From d6095a6b0e9b4cb53aa093577ab0baa933a4a6e8 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Mon, 9 Dec 2024 17:09:14 +0100 Subject: [PATCH 07/22] Set margins for caption in horizontal mode --- R/VizEquiMap.R | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 819f8d9..ca8c883 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1029,14 +1029,6 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } margins <- rep(0.4, 4) * margin_scale margins[4] <- margins[4] + 1 - # if (vertical) { - # margins[1] <- margins[1] + 2 - # } - if (!is.null(caption)) { - num_lines <- length(strsplit(caption, "\n")[[1]]) - } else { - num_lines <- 1 - } cex_title <- 2 * title_scale cex_axes_labels <- 1.3 * axes_label_scale cex_axes_ticks <- -0.5 * axes_tick_scale @@ -1112,14 +1104,26 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } if (drawleg) { - if (vertical) { - par(mar = margins) - layout(matrix(c(1, 2, 3, 3), ncol = 2, nrow = 2, byrow = TRUE), - widths = c(5, 1.3), - heights = c(5, num_lines*0.2)) - - } else { - layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1 + num_lines)) # bueno el num_lines és nou també + if (!is.null(caption)) { + num_lines <- length(strsplit(caption, "\n")[[1]]) + if (vertical) { # vertical bar, caption + #par(mar = margins) + margins[2] <- margins[2] + num_lines*0.5 + margins[4] <- margins[4] + num_lines*0.5 + layout(matrix(c(1, 2, 3, 3), ncol = 2, nrow = 2, byrow = TRUE), + widths = c(5, 1.3), + heights = c(5, num_lines*0.2)) + + } else { # horizontal bar, caption + layout(matrix(c(1, 2, 3), ncol = 1, nrow = 3), + heights = c(5, num_lines*0.2, 1)) + } + } else { + if (vertical) { # vertical bar, no caption + layout(matrix(1:2, ncol = 2, nrow = 1), widths = c(5, 1.3)) + } else { # horizontal bar, no caption + layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) + } } } plot.new() @@ -1378,10 +1382,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~ # if (!is.null(caption)) { - if (vertical) { - par(mfg = c(2,1)) - } - base_line <- 2 + par(mfg = c(2, 1)) mtext(caption, side = 1, line = 1, adj = 0, cex = caption_size, col = "black") } -- GitLab From 82b2fa3b4c9a41df956d8ef59f12319ca62365a6 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 10 Dec 2024 10:45:40 +0100 Subject: [PATCH 08/22] Adapt margins plot margins also when colorbar is horizontal --- R/VizEquiMap.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index ca8c883..21a30f4 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1106,10 +1106,9 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (!is.null(caption)) { num_lines <- length(strsplit(caption, "\n")[[1]]) + margins[2] <- margins[2] + num_lines*0.5 + margins[4] <- margins[4] + num_lines*0.5 if (vertical) { # vertical bar, caption - #par(mar = margins) - margins[2] <- margins[2] + num_lines*0.5 - margins[4] <- margins[4] + num_lines*0.5 layout(matrix(c(1, 2, 3, 3), ncol = 2, nrow = 2, byrow = TRUE), widths = c(5, 1.3), heights = c(5, num_lines*0.2)) -- GitLab From 952b2539ae2533f6956c307eb659ee8622d62061 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 10 Dec 2024 13:09:55 +0100 Subject: [PATCH 09/22] Configure appearance for caption when drawleg is FALSE --- R/VizEquiMap.R | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 21a30f4..293eab7 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -529,10 +529,14 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } # caption - if (!is.null(caption) && !is.character(caption)) { - stop("Parameter 'caption' must be a character string.") + if (!is.null(caption)) { + if (!is.character(caption)) { + stop("Parameter 'caption' must be a character string.") + } else { + num_lines <- length(strsplit(caption, "\n")[[1]]) + } } - + # Check include_lower_boundary and include_upper_boundary if (!is.null(include_lower_boundary) && (!is.logical(include_lower_boundary) || length(include_lower_boundary) != 1)) { stop("Parameter 'include_lower_boundary' must be a logical element.") @@ -1105,7 +1109,6 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (!is.null(caption)) { - num_lines <- length(strsplit(caption, "\n")[[1]]) margins[2] <- margins[2] + num_lines*0.5 margins[4] <- margins[4] + num_lines*0.5 if (vertical) { # vertical bar, caption @@ -1124,6 +1127,12 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) } } + } else { + if (!is.null(caption)) { + margins[2] <- margins[2] + num_lines*0.5 + margins[4] <- margins[4] + num_lines*0.5 + layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1 + num_lines*0.2)) + } } plot.new() # Load the user parameters @@ -1382,7 +1391,8 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # if (!is.null(caption)) { par(mfg = c(2, 1)) - mtext(caption, side = 1, line = 1, adj = 0, + base_line <- ifelse(drawleg, 1, -1) + mtext(caption, side = 1, line = base_line, adj = 0, cex = caption_size, col = "black") } -- GitLab From 0398a68c994cd3ebbb78e99f84b3ebff5e8b15f8 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 10 Dec 2024 18:03:06 +0100 Subject: [PATCH 10/22] Improve 'vertical' description --- R/VizEquiMap.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 293eab7..1b8cfa8 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -179,8 +179,9 @@ #'@param drawleg Whether to plot a color bar (legend, key) or not. Defaults to #' TRUE. It is not possible to plot the colour bar if 'add = TRUE'. Use #' ColorBar() and the return values of PlotEquiMap() instead. -#'@param vertical TRUE/FALSE for vertical/horizontal colour bar -#' (disregarded if plot = FALSE). Default is FALSE. +#'@param vertical TRUE/FALSE for vertical/horizontal colour bar. Default is +#' FALSE. Parameters 'width' and 'height' might need to be modified to +#' accommodate the vertical colour bar. #'@param boxlim Limits of a box to be added to the plot, in degrees: #' c(x1, y1, x2, y2). A list with multiple box specifications can also be #' provided. -- GitLab From 640d0a19c667e0059044ce1e5ed82e54fc0fce43 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Wed, 11 Dec 2024 13:39:37 +0100 Subject: [PATCH 11/22] Add check for 'vertical' parameter --- R/VizEquiMap.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 1b8cfa8..111821a 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -529,7 +529,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, title_scale <- sizetit } - # caption + # Check caption if (!is.null(caption)) { if (!is.character(caption)) { stop("Parameter 'caption' must be a character string.") @@ -544,7 +544,12 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } if (!is.null(include_upper_boundary) && (!is.logical(include_upper_boundary) || length(include_upper_boundary) != 1)) { stop("Parameter 'include_upper_boundary' must be a logical element.") - } + } + + # Check vertical + if (!is.logical(vertical)) { + stop("Parameter 'vertical' must be TRUE or FALSE.") + } tmp <- .create_var_limits(data = var, brks = brks, bar_limits = bar_limits, drawleg = drawleg) -- GitLab From bed97f6c7d6f32db879fb108830932fbb41a2c4c Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Thu, 12 Dec 2024 17:49:15 +0100 Subject: [PATCH 12/22] Caption below the color bar if the latter is horizontal --- R/VizEquiMap.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 111821a..5b4231a 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1124,7 +1124,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } else { # horizontal bar, caption layout(matrix(c(1, 2, 3), ncol = 1, nrow = 3), - heights = c(5, num_lines*0.2, 1)) + heights = c(5, 1, 0.2 + num_lines*0.2)) } } else { if (vertical) { # vertical bar, no caption @@ -1396,7 +1396,22 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~ # if (!is.null(caption)) { - par(mfg = c(2, 1)) + if (drawleg) { + if (!vertical) { + par(mfg = c(3, 1)) + } + } else { + par(mfg = c(2, 1)) + } + if (drawleg) { + if (vertical) { + base_line <- 1 + } else { + base_line <- 3 + } + } else { + base_line <- -1 + } base_line <- ifelse(drawleg, 1, -1) mtext(caption, side = 1, line = base_line, adj = 0, cex = caption_size, col = "black") @@ -1409,6 +1424,10 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (vertical) { par(mfg = c(1, 2)) + } else { + if (!is.null(caption)) { + par(mfg = c(2, 1)) + } } ColorBarContinuous(brks, cols, vertical = vertical, subsampleg, bar_limits, var_limits, triangle_ends, col_inf = col_inf, col_sup = col_sup, -- GitLab From 8847f310eaf204337abcd8ce08a68b797555682e Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Fri, 13 Dec 2024 13:56:03 +0100 Subject: [PATCH 13/22] Add parameter input checks to test-VizEquiMap.R --- tests/testthat/test-VizEquiMap.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testthat/test-VizEquiMap.R b/tests/testthat/test-VizEquiMap.R index 8ae3f21..b3e366c 100644 --- a/tests/testthat/test-VizEquiMap.R +++ b/tests/testthat/test-VizEquiMap.R @@ -58,6 +58,12 @@ test_that("1. Input checks", { "Parameter 'toptitle' must be a character string." ) + # Check caption + expect_error( + VizEquiMap(var = data1, lon = lons1, lat = lats1, caption = 1), + "Parameter 'caption' must be a character string." + ) + # Check include_lower_boundary and include_upper_boundary expect_error( VizEquiMap(var = data1, lon = lons1, lat = lats1, include_lower_boundary = 1), @@ -68,6 +74,11 @@ test_that("1. Input checks", { "Parameter 'include_upper_boundary' must be a logical element." ) + expect_error( + VizEquiMap(var = data1, lon = lons1, lat = lats1, vertical = "test"), + "Parameter 'vertical' must be TRUE or FALSE." + ) + # Check colNA expect_error( VizEquiMap(var = data1, lon = lons1, lat = lats1, colNA = "test"), @@ -268,6 +279,12 @@ test_that("1. Input checks", { "Parameter 'title_scale' must be numeric." ) + # Check caption_size + expect_error( + VizEquiMap(var = data1, lon = lons1, lat = lats1, caption_size = "test"), + "Parameter 'caption_size' must be numeric." + ) + # Check axes_tick_scale expect_error( VizEquiMap(var = data1, lon = lons1, lat = lats1, axes_tick_scale = "test"), -- GitLab From 6024a36c57b00831cf2cd9d887ada36d8718bd15 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Fri, 13 Dec 2024 14:09:54 +0100 Subject: [PATCH 14/22] Improve caption documentation --- R/VizEquiMap.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 5b4231a..2d30583 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -38,8 +38,10 @@ #' 'title_scale'. #'@param sizetit Scale factor for the figure top title provided in parameter #' 'toptitle'. Deprecated. Use 'title_scale' instead. -#'@param caption A character string of the caption located at left-bottom of the -#' plot. +#'@param caption A character string of the caption located at the left-bottom of +#' the plot. Captions with multiple lines can be constructed using string +#' manipulation functions like \code{paste()} or \code{paste0()}, using +#' \code{"\n"} to indicate line breaks. #'@param units Title at the top of the colour bar, most commonly the units of #' the variable provided in parameter 'var'. #'@param brks,cols,bar_limits,triangle_ends Usually only providing 'brks' is @@ -248,7 +250,9 @@ #' #'VizEquiMap(var[1, 1, 1, 1, , ], lon = lons, lat = lats, #' toptitle = 'Near-surface temperature anomaly, Nov. 2000', -#' filled.continents = FALSE, title_scale = 0.7) +#' filled.continents = FALSE, title_scale = 0.7, +#' caption = paste0("This is a test caption.", "\n", +#' "This is a new line.") #' } #'@import graphics maps utils #'@importFrom grDevices dev.cur dev.new dev.off gray -- GitLab From 79d02ed3421ce00f394dcd208091c77dcbe599ac Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Fri, 13 Dec 2024 17:16:35 +0100 Subject: [PATCH 15/22] More layout configurations --- R/VizEquiMap.R | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 2d30583..f0719a1 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1124,11 +1124,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (vertical) { # vertical bar, caption layout(matrix(c(1, 2, 3, 3), ncol = 2, nrow = 2, byrow = TRUE), widths = c(5, 1.3), - heights = c(5, num_lines*0.2)) + heights = c(5, 0.2 + num_lines*caption_size/6)) } else { # horizontal bar, caption layout(matrix(c(1, 2, 3), ncol = 1, nrow = 3), - heights = c(5, 1, 0.2 + num_lines*0.2)) + heights = c(5, 1, 0.2 + num_lines*caption_size/4)) } } else { if (vertical) { # vertical bar, no caption @@ -1400,23 +1400,18 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # ~~~~~~~~~~~~~~~~~ # if (!is.null(caption)) { - if (drawleg) { - if (!vertical) { - par(mfg = c(3, 1)) - } - } else { - par(mfg = c(2, 1)) - } if (drawleg) { if (vertical) { + par(mfg = c(2, 1)) base_line <- 1 } else { - base_line <- 3 + par(mfg = c(3, 1)) + base_line <- 1 } } else { - base_line <- -1 + par(mfg = c(2, 1)) + base_line <- 1 } - base_line <- ifelse(drawleg, 1, -1) mtext(caption, side = 1, line = base_line, adj = 0, cex = caption_size, col = "black") } -- GitLab From 3bfa6c4fe1857f4e45a847afa58561b73a538708 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Fri, 13 Dec 2024 20:24:20 +0100 Subject: [PATCH 16/22] Reduce fixed space in bottom row of matrix when there is a caption and no colorbar --- R/VizEquiMap.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index f0719a1..eb085eb 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1139,9 +1139,9 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } } else { if (!is.null(caption)) { - margins[2] <- margins[2] + num_lines*0.5 - margins[4] <- margins[4] + num_lines*0.5 - layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1 + num_lines*0.2)) + margins[2] <- margins[2] + num_lines*0.4 + margins[4] <- margins[4] + num_lines*0.4 + layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 0.1 + num_lines*caption_size/4)) } } plot.new() -- GitLab From f23265dfd7e42eb60b84d0fb6756a2d4f20b079a Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 17 Dec 2024 11:01:39 +0100 Subject: [PATCH 17/22] Improve caption_size documentation in VisEquiMap.R --- R/VizEquiMap.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index eb085eb..7751203 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -195,7 +195,8 @@ #' the format c(y1, x1, y2, x2). Defaults to rep(1, 4). If drawleg = TRUE, #' then margin_scale[1] is subtracted 1 unit. #'@param title_scale Scale factor for the figure top title. Defaults to 1. -#'@param caption_size Scale factor for the figure caption. Default is 0.8. +#'@param caption_size Scale factor for the figure caption. Default is 0.8 (1 if +#' vertical = TRUE). #'@param numbfig Number of figures in the layout the plot will be put into. #' A higher numbfig will result in narrower margins and smaller labels, #' axe labels, ticks, thinner lines, ... Defaults to 1. -- GitLab From 4ee2a1170550e034e2731e60fbe7f337423b080e Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 17 Dec 2024 11:03:11 +0100 Subject: [PATCH 18/22] Update man/VizEquiMap.Rd --- man/VizEquiMap.Rd | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/man/VizEquiMap.Rd b/man/VizEquiMap.Rd index 7fadb87..bad14ae 100644 --- a/man/VizEquiMap.Rd +++ b/man/VizEquiMap.Rd @@ -12,6 +12,7 @@ VizEquiMap( varv = NULL, toptitle = NULL, sizetit = NULL, + caption = NULL, units = NULL, brks = NULL, cols = NULL, @@ -63,6 +64,7 @@ VizEquiMap( axes_tick_scale = 1, axes_label_scale = 1, drawleg = TRUE, + vertical = FALSE, subsampleg = NULL, bar_extra_labels = NULL, draw_bar_ticks = TRUE, @@ -78,6 +80,7 @@ VizEquiMap( boxlwd = 5, margin_scale = rep(1, 4), title_scale = 1, + caption_size = 0.8, numbfig = NULL, fileout = NULL, width = 8, @@ -125,6 +128,11 @@ longitudinal and latitudinal coordinate dimensions are interchanged.} \item{sizetit}{Scale factor for the figure top title provided in parameter 'toptitle'. Deprecated. Use 'title_scale' instead.} +\item{caption}{A character string of the caption located at the left-bottom of +the plot. Captions with multiple lines can be constructed using string +manipulation functions like \code{paste()} or \code{paste0()}, using \code{"\n"} +to indicate line breaks.} + \item{units}{Title at the top of the colour bar, most commonly the units of the variable provided in parameter 'var'.} @@ -304,6 +312,10 @@ and latitude axes.} TRUE. It is not possible to plot the colour bar if 'add = TRUE'. Use ColorBar() and the return values of PlotEquiMap() instead.} +\item{vertical}{TRUE/FALSE for vertical/horizontal colour bar. Default is FALSE. +Parameters 'width' and 'height' might need to be modified to accommodate the +vertical colour bar.} + \item{draw_separators, triangle_ends_scale, bar_label_digits}{Set of parameters to control the visual aspect of the drawn colour bar (2/3). See ?ColorBar for a full explanation.} @@ -328,6 +340,9 @@ then margin_scale[1] is subtracted 1 unit.} \item{title_scale}{Scale factor for the figure top title. Defaults to 1.} +\item{caption_size}{Scale factor for the figure caption. Default is 0.8 (1 if +vertical = TRUE).} + \item{numbfig}{Number of figures in the layout the plot will be put into. A higher numbfig will result in narrower margins and smaller labels, axe labels, ticks, thinner lines, ... Defaults to 1.} @@ -401,6 +416,8 @@ lons <- attr(map_temp$exp, "Variables")$common$lon VizEquiMap(var[1, 1, 1, 1, , ], lon = lons, lat = lats, toptitle = 'Near-surface temperature anomaly, Nov. 2000', - filled.continents = FALSE, title_scale = 0.7) + filled.continents = FALSE, title_scale = 0.7, + caption = paste0("This is a test caption.", "\n", + "This is a new line.") } } -- GitLab From e134b658403c9c9de043272c560b86e4a77b7040 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 28 Jan 2025 12:21:34 +0100 Subject: [PATCH 19/22] Fix left placement of caption (for vertical colorbar) --- R/VizEquiMap.R | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 7751203..37fb4fa 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1131,7 +1131,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, layout(matrix(c(1, 2, 3), ncol = 1, nrow = 3), heights = c(5, 1, 0.2 + num_lines*caption_size/4)) } - } else { + } else { if (vertical) { # vertical bar, no caption layout(matrix(1:2, ncol = 2, nrow = 1), widths = c(5, 1.3)) } else { # horizontal bar, no caption @@ -1404,16 +1404,19 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (vertical) { par(mfg = c(2, 1)) - base_line <- 1 + at_value <- par("usr")[1] - (0.3 + (num_lines - 1) * 0.1) } else { par(mfg = c(3, 1)) - base_line <- 1 + at_value <- NA } } else { par(mfg = c(2, 1)) - base_line <- 1 + at_value <- NA } - mtext(caption, side = 1, line = base_line, adj = 0, + base_line <- 1 + mtext(caption, side = 1, line = base_line, + at = at_value, # left placement + adj = 0.5, cex = caption_size, col = "black") } -- GitLab From 6b102e151a56e70fa23b17e133abdade5a34bdd0 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Tue, 28 Jan 2025 12:46:55 +0100 Subject: [PATCH 20/22] Adjustments to previous commit --- R/VizEquiMap.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 37fb4fa..1f6b7d4 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1404,7 +1404,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (vertical) { par(mfg = c(2, 1)) - at_value <- par("usr")[1] - (0.3 + (num_lines - 1) * 0.1) + at_value <- par("usr")[1] - (0.35 + (num_lines - 1) * 0.15) } else { par(mfg = c(3, 1)) at_value <- NA @@ -1416,7 +1416,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, base_line <- 1 mtext(caption, side = 1, line = base_line, at = at_value, # left placement - adj = 0.5, + adj = 0, cex = caption_size, col = "black") } -- GitLab From 97a9134730b425923911d1c472b64414f8bcec70 Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Wed, 5 Feb 2025 16:27:07 +0100 Subject: [PATCH 21/22] Adjustments to layout combining vertical bar + no caption --- R/VizEquiMap.R | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index 1f6b7d4..f74c305 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1133,7 +1133,9 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } } else { if (vertical) { # vertical bar, no caption - layout(matrix(1:2, ncol = 2, nrow = 1), widths = c(5, 1.3)) + layout(matrix(c(1, 2, 1, 3), ncol = 2, nrow = 2, byrow = TRUE), + widths = c(5, 1.2), + heights = c(0.1, 7)) } else { # horizontal bar, no caption layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) } @@ -1404,7 +1406,7 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (drawleg) { if (vertical) { par(mfg = c(2, 1)) - at_value <- par("usr")[1] - (0.35 + (num_lines - 1) * 0.15) + at_value <- par("usr")[1] - (0.38 + (num_lines - 1) * 0.15) } else { par(mfg = c(3, 1)) at_value <- NA @@ -1426,7 +1428,11 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # if (drawleg) { if (vertical) { - par(mfg = c(1, 2)) + if(is.null(caption)) { + par(mfg = c(2, 1)) + } else { + par(mfg = c(1, 2)) + } } else { if (!is.null(caption)) { par(mfg = c(2, 1)) -- GitLab From f6a64d69b4a1587ab2084db20fcb36a37c2920de Mon Sep 17 00:00:00 2001 From: ARIADNA BATALLA FERRES Date: Wed, 5 Feb 2025 17:10:00 +0100 Subject: [PATCH 22/22] Adjusting the previous adjustment --- R/VizEquiMap.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index f74c305..07db737 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -1134,8 +1134,8 @@ VizEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } else { if (vertical) { # vertical bar, no caption layout(matrix(c(1, 2, 1, 3), ncol = 2, nrow = 2, byrow = TRUE), - widths = c(5, 1.2), - heights = c(0.1, 7)) + widths = c(5, 1.3), + heights = c(0.1, 5)) } else { # horizontal bar, no caption layout(matrix(1:2, ncol = 1, nrow = 2), heights = c(5, 1)) } -- GitLab