diff --git a/R/VizCombinedMap.R b/R/VizCombinedMap.R index 484d4efd59ebc4b256c57515a1a2c52ad16fd1b2..55a7ab04c5eb4accd475474077216d7ac2ef50d7 100644 --- a/R/VizCombinedMap.R +++ b/R/VizCombinedMap.R @@ -49,8 +49,9 @@ #' values that go beyond 'display_range'. Takes the value 'white' by default. #'@param mask Optional numeric array with dimensions (latitude, longitude), with #' values in the range [0, 1], indicating the opacity of the mask over each -#' grid point. Cells with a 0 will result in no mask, whereas cells with a 1 -#' will result in a totally opaque superimposed pixel coloured in 'mask_color'. +#' grid point. Cells with a 0 will result in a totally opaque superimposed +#' pixel coloured in 'mask_color', whereas cells with a 1 will have no mask and +#' remain totally visible. #'@param mask_color Colour to be used for the superimposed mask (if specified in #' 'mask'). Takes the value 'grey' by default. #'@param col_mask Deprecated. Use 'mask_color' instead. @@ -504,11 +505,11 @@ VizCombinedMap <- function(maps, lon, lat, lonb <- sort(lon, index.return = TRUE) cols_mask <- sapply(seq(from = 0, to = 1, length.out = 10), - function(x) adjustcolor(mask_color, alpha.f = x)) + function(x) adjustcolor(mask_color, alpha.f = 1 - x)) image(lonb$x, latb$x, t(mask)[lonb$ix, latb$ix], axes = FALSE, col = cols_mask, breaks = seq(from = 0, to = 1, by = 0.1), - xlab='', ylab='', add = TRUE, xpd = TRUE) + xlab = '', ylab = '', add = TRUE, xpd = TRUE) if (!exists('coast_color')) { coast_color <- 'black' } diff --git a/R/VizEquiMap.R b/R/VizEquiMap.R index a9058184b1ce5e3f0060a291dfffa94db959535a..d0e489b7cc1eb26b1f767b47794b5a143fa4a47b 100644 --- a/R/VizEquiMap.R +++ b/R/VizEquiMap.R @@ -127,9 +127,12 @@ #'@param dot_size Scale factor for the dots/symbols to be plotted, specified #' in 'dots'. If a single value is specified, it will be applied to all #' layers in 'dots'. Takes 1 by default. -#'@param mask An array with the same dimensions as 'data' of [0, 1] or logical -#' indicating the grids to not plot data. The value 0 or FALSE is the point not -#' to be plotted. +#'@param mask An array with the same dimensions as 'data' with values in the +#' range of [0, 1] or logical, indicating the opacity of the mask over each +#' grid point. Cells with a 0 or FALSE will result in a totally opaque +#' superimposed pixel coloured in 'mask_color', whereas cells with a 1 or TRUE +#' will have no mask and remain totally visible. If the array is numeric, values +#' between 0 and 1 will have shades of transparency. #'@param mask_color Color of the mask. The default value is 'white'. #'@param arr_subsamp Subsampling factor to select a subset of arrows in #' 'varu' and 'varv' to be drawn. Only one out of arr_subsamp arrows will @@ -865,15 +868,16 @@ VizEquiMap <- function(data, lon, lat, varu = NULL, varv = NULL, } if (!identical(dim(mask), dim(data))) { stop("Parameter 'mask' must have the same dimensions as 'data'.") - } else if (is.numeric(mask)) { - if (all(mask %in% c(0, 1))) { - mask <- array(as.logical(mask), dim = dim(mask)) - } else { - stop("Parameter 'mask' must have only TRUE/FALSE or 0/1.") + } + + if (is.logical(mask)) { + if (!all(mask %in% c(TRUE, FALSE))) { + stop("Parameter 'mask' must contain only TRUE/FALSE or values in the range [0, 1].") } - } else if (is.logical(mask)) { - if (!all(mask %in% c(T, F))) { - stop("Parameter 'mask' must have only TRUE/FALSE or 0/1.") + mask <- as.numeric(mask) + } else if (is.numeric(mask)) { + if (any(mask < 0 | mask > 1, na.rm = TRUE)) { + stop("Parameter 'mask' must contain only TRUE/FALSE or values in the range [0, 1].") } } else { stop("Parameter 'mask' must be a logical or numerical array.") @@ -1287,12 +1291,10 @@ VizEquiMap <- function(data, lon, lat, varu = NULL, varv = NULL, # Adding a mask # ~~~~~~~~~~~~~~~ # - if (!is.null(mask)){ - - mask_logical <- as.logical(mask) - - for (i in 1:length(mask_logical)) { - if (!mask_logical[i]) { + if (!is.null(mask)) { + for (i in 1:length(mask)) { + # Partial/fully masked areas + if (!is.na(mask[i]) && mask[i] < 1) { # Calculate the longitude and latitude indices lon_idx <- (i - 1) %% length(lon) + 1 @@ -1317,7 +1319,8 @@ VizEquiMap <- function(data, lon, lat, varu = NULL, varv = NULL, (lat[lat_idx + 1] + lat[lat_idx]) / 2) # Draw a rectangle over the masked area - rect(lon_min, lat_min, lon_max, lat_max, col = mask_color, border = NA) + rect(lon_min, lat_min, lon_max, lat_max, + col = adjustcolor(mask_color, alpha.f = 1 - mask[i]), border = NA) } } }