From ad3e758397c81f387c160384760cfcad83447b6d Mon Sep 17 00:00:00 2001 From: mtennekes Date: Wed, 13 Mar 2024 15:26:47 +0100 Subject: [PATCH] tm_scale discrete and rank working --- R/step2_helper_data.R | 5 ++++- R/tm_scale_.R | 3 ++- R/tmapChart.R | 6 +++--- R/tmapScaleRank.R | 14 +++++++++++--- R/tmap_options.R | 3 ++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/R/step2_helper_data.R b/R/step2_helper_data.R index 7d440a71..5286a334 100644 --- a/R/step2_helper_data.R +++ b/R/step2_helper_data.R @@ -259,7 +259,10 @@ getdts = function(aes, unm, p, q, o, dt, shpvars, layer, mfun, args, plot.order) # the condition has length > 1 # Calls: ... with -> with.default -> eval -> eval -> apply_scale # Detected in types of titles. # Example to illustrate the type of titles - if (all(is.ena(l$title))) l$title = paste0(names(v), attr(cls, "units")) + unit = if (!is.null(s$unit)) s$unit else "" + if (unit != "") unit = paste0(" (", unit, ")") + + if (all(is.ena(l$title))) l$title = paste0(names(v), attr(cls, "units"), unit) } diff --git a/R/tm_scale_.R b/R/tm_scale_.R index 8204f2a8..26981b05 100644 --- a/R/tm_scale_.R +++ b/R/tm_scale_.R @@ -269,7 +269,8 @@ tm_scale_rank = function(n = NULL, labels = NULL, label.na = NA, label.null = NA, - label.format = list()) { + label.format = list(), + unit = "rank") { structure(c(list(FUN = "tmapScaleRank"), as.list(environment())), class = c("tm_scale_rank", "tm_scale", "list")) } diff --git a/R/tmapChart.R b/R/tmapChart.R index a14dfc40..9a1ad086 100644 --- a/R/tmapChart.R +++ b/R/tmapChart.R @@ -105,7 +105,7 @@ bin_num = function(x1, breaks_def, chart) { if (is.null(chart$breaks)) { if (!predefined) { breaks = pretty(x1) - ids = 1L + ids = rep(1L, length(breaks) - 1) } else { breaks = breaks_def @@ -117,10 +117,10 @@ bin_num = function(x1, breaks_def, chart) { break_mids = (breaks[-1] + head(breaks, -1)) / 2 - if (!is.null(breaks_def)) { + if (predefined) { ids = as.integer(cut(break_mids, breaks_def, include.lowest = TRUE, right = FALSE)) } else { - ids = 1 + ids = rep(1L, length(breaks) - 1) } } diff --git a/R/tmapScaleRank.R b/R/tmapScaleRank.R index a22c4c76..a6eaba90 100644 --- a/R/tmapScaleRank.R +++ b/R/tmapScaleRank.R @@ -165,15 +165,23 @@ tmapScaleRank = function(x1, scale, legend, chart, o, aes, layer, layer_args, so limits = limits }) # NOTE: tr and limits are included in the output to facilitate the transformation of the leaflet continuous legend ticks (https://github.com/rstudio/leaflet/issues/665) + chartFun = paste0("tmapChart", toTitleCase(chart$summary)) + + chart = do.call(chartFun, list(chart, + bin_colors = NULL, + breaks_def = NULL, + na.show = na.show, + x1 = x1)) + if (submit_legend) { if (bypass_ord) { - format_aes_results(vals, legend = legend) + format_aes_results(vals, legend = legend, chart = chart) } else { - format_aes_results(vals, ids, legend) + format_aes_results(vals, ids, legend, chart = chart) } } else { - list(vals = vals, ids = ids, legend = legend, bypass_ord = bypass_ord) + list(vals = vals, ids = ids, legend = legend, chart = chart, bypass_ord = bypass_ord) } }) diff --git a/R/tmap_options.R b/R/tmap_options.R index 53722785..0add6d2a 100644 --- a/R/tmap_options.R +++ b/R/tmap_options.R @@ -156,7 +156,8 @@ outliers.trunc = c(FALSE, FALSE), trans = "identity", limits = list(fill = NA, col = NA, 0)), - rank = list(n = 5)), # NA means take data range, 0 means include 0 + rank = list(n = 5, + unit = "rank")), # NA means take data range, 0 means include 0