From 9776cd9b84e90be93374b285ac2cde60fa056b69 Mon Sep 17 00:00:00 2001 From: aho Date: Mon, 17 Jul 2023 11:55:16 +0200 Subject: [PATCH 1/4] Make function work when all NAs --- R/PlotEquiMap.R | 17 ++++++++++++++++- R/PlotLayout.R | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/R/PlotEquiMap.R b/R/PlotEquiMap.R index 16f42b5..e553d62 100644 --- a/R/PlotEquiMap.R +++ b/R/PlotEquiMap.R @@ -530,7 +530,22 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, title_scale <- sizetit } - var_limits <- c(min(var, na.rm = TRUE), max(var, na.rm = TRUE)) + if (!all(is.na(var))) { + var_limits <- c(min(var, na.rm = TRUE), max(var, na.rm = TRUE)) + } else { + if (!is.null(brks)) { + #NOTE: var_limits be like this to avoid warnings from ColorBar + var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], + max(brks, na.rm = TRUE)) + } else { + var_limits <- c(-0.5, 0.5) # random range since colorbar is not going to be plotted + if (drawleg) { + drawleg <- FALSE + .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks'.") + } + } + } + # Check: brks, cols, subsampleg, bar_limits, color_fun, bar_extra_labels, draw_bar_ticks # draw_separators, triangle_ends_scale, label_scale, units, units_scale, # bar_label_digits diff --git a/R/PlotLayout.R b/R/PlotLayout.R index c442bf7..5f757c2 100644 --- a/R/PlotLayout.R +++ b/R/PlotLayout.R @@ -346,13 +346,25 @@ PlotLayout <- function(fun, plot_dims, var, ..., special_args = NULL, } # Check the rest of parameters (unless the user simply wants to build an empty layout) - var_limits <- NULL if (!all(sapply(var, is_single_na))) { + + if (!all(is.na(unlist(var)))) { var_limits <- c(min(unlist(var), na.rm = TRUE), max(unlist(var), na.rm = TRUE)) - if ((any(is.infinite(var_limits)) || var_limits[1] == var_limits[2])) { - stop("Arrays in parameter 'var' must contain at least 2 different values.") + } else { + if (!is.null(brks)) { + #NOTE: var_limits be like this to avoid warnings from ColorBar + var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], + max(brks, na.rm = TRUE)) + } else { + var_limits <- c(-0.5, 0.5) # random range since colorbar is not going to be plotted + if (!isFALSE(drawleg)) { + drawleg <- FALSE + .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks'.") + } } } + } + colorbar <- ColorBar(brks, cols, FALSE, subsampleg, bar_limits, var_limits, triangle_ends, col_inf, col_sup, color_fun, plot = FALSE, draw_bar_ticks, -- GitLab From b550f5a3bfec16f23a99d13c01376a4a3219c185 Mon Sep 17 00:00:00 2001 From: aho Date: Mon, 17 Jul 2023 12:34:41 +0200 Subject: [PATCH 2/4] Consider using bar_limits when all data are NAs --- R/PlotEquiMap.R | 5 ++++- R/PlotLayout.R | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/R/PlotEquiMap.R b/R/PlotEquiMap.R index e553d62..0af9759 100644 --- a/R/PlotEquiMap.R +++ b/R/PlotEquiMap.R @@ -537,11 +537,14 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, #NOTE: var_limits be like this to avoid warnings from ColorBar var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], max(brks, na.rm = TRUE)) + } else if (!is.null(bar_limits)) { + var_limits <- c(bar_limits[1] + 0.01, bar_limits[2]) } else { var_limits <- c(-0.5, 0.5) # random range since colorbar is not going to be plotted if (drawleg) { drawleg <- FALSE - .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks'.") + .warning("All data are NAs. Color bar won't be drawn. If you want to have ", + "color bar still, define parameter 'brks' or 'bar_limits'.") } } } diff --git a/R/PlotLayout.R b/R/PlotLayout.R index 5f757c2..81d751b 100644 --- a/R/PlotLayout.R +++ b/R/PlotLayout.R @@ -355,11 +355,13 @@ PlotLayout <- function(fun, plot_dims, var, ..., special_args = NULL, #NOTE: var_limits be like this to avoid warnings from ColorBar var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], max(brks, na.rm = TRUE)) + } else if (!is.null(bar_limits)) { + var_limits <- c(bar_limits[1] + 0.01, bar_limits[2]) } else { var_limits <- c(-0.5, 0.5) # random range since colorbar is not going to be plotted if (!isFALSE(drawleg)) { drawleg <- FALSE - .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks'.") + .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks' or 'bar_limits'.") } } } -- GitLab From 52bea1abfa2f8be198de9d5dbc034f8c948aca33 Mon Sep 17 00:00:00 2001 From: aho Date: Mon, 17 Jul 2023 12:36:17 +0200 Subject: [PATCH 3/4] indentation --- R/PlotLayout.R | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/R/PlotLayout.R b/R/PlotLayout.R index 81d751b..6553f8a 100644 --- a/R/PlotLayout.R +++ b/R/PlotLayout.R @@ -347,25 +347,24 @@ PlotLayout <- function(fun, plot_dims, var, ..., special_args = NULL, # Check the rest of parameters (unless the user simply wants to build an empty layout) if (!all(sapply(var, is_single_na))) { - - if (!all(is.na(unlist(var)))) { - var_limits <- c(min(unlist(var), na.rm = TRUE), max(unlist(var), na.rm = TRUE)) - } else { - if (!is.null(brks)) { - #NOTE: var_limits be like this to avoid warnings from ColorBar - var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], - max(brks, na.rm = TRUE)) - } else if (!is.null(bar_limits)) { - var_limits <- c(bar_limits[1] + 0.01, bar_limits[2]) + if (!all(is.na(unlist(var)))) { + var_limits <- c(min(unlist(var), na.rm = TRUE), max(unlist(var), na.rm = TRUE)) } else { - var_limits <- c(-0.5, 0.5) # random range since colorbar is not going to be plotted - if (!isFALSE(drawleg)) { - drawleg <- FALSE - .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks' or 'bar_limits'.") + if (!is.null(brks)) { + #NOTE: var_limits be like this to avoid warnings from ColorBar + var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], + max(brks, na.rm = TRUE)) + } else if (!is.null(bar_limits)) { + var_limits <- c(bar_limits[1] + 0.01, bar_limits[2]) + } else { + var_limits <- c(-0.5, 0.5) # random range since colorbar is not going to be plotted + if (!isFALSE(drawleg)) { + drawleg <- FALSE + .warning("All data are NAs. Color bar won't be drawn. If you want to have color bar still, define parameter 'brks' or 'bar_limits'.") + } } } } - } colorbar <- ColorBar(brks, cols, FALSE, subsampleg, bar_limits, var_limits, triangle_ends, col_inf, col_sup, color_fun, -- GitLab From e6d151c19320b90fb73ea481dd23e978bb40929a Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 30 Aug 2023 12:07:22 +0200 Subject: [PATCH 4/4] Add warning when all data is NA --- R/PlotEquiMap.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/PlotEquiMap.R b/R/PlotEquiMap.R index 0af9759..3b8f861 100644 --- a/R/PlotEquiMap.R +++ b/R/PlotEquiMap.R @@ -533,7 +533,8 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, if (!all(is.na(var))) { var_limits <- c(min(var, na.rm = TRUE), max(var, na.rm = TRUE)) } else { - if (!is.null(brks)) { + .warning("All the data are NAs. The map will be filled with colNA.") + if (!is.null(brks) && length(brks) > 1) { #NOTE: var_limits be like this to avoid warnings from ColorBar var_limits <- c(min(brks, na.rm = TRUE) + diff(brks)[1], max(brks, na.rm = TRUE)) -- GitLab