From d133496b886c4e021e427cb29415fe06fe459b90 Mon Sep 17 00:00:00 2001 From: mtennekes Date: Tue, 18 Jun 2024 19:27:34 +0200 Subject: [PATCH] moved composition to tmap.glyphs --- NAMESPACE | 1 - R/tm_scale_composition.R | 33 --------------- R/tmapLeafletLegend.R | 1 - R/tmapScaleComposition.R | 91 ---------------------------------------- R/tmapScale_.R | 3 +- 5 files changed, 2 insertions(+), 127 deletions(-) delete mode 100644 R/tm_scale_composition.R delete mode 100644 R/tmapScaleComposition.R diff --git a/NAMESPACE b/NAMESPACE index 36e0e483..124d48c1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -207,7 +207,6 @@ export(tm_scale_asis) export(tm_scale_bar) export(tm_scale_bivariate) export(tm_scale_categorical) -export(tm_scale_composition) export(tm_scale_continuous) export(tm_scale_continuous_log) export(tm_scale_continuous_log10) diff --git a/R/tm_scale_composition.R b/R/tm_scale_composition.R deleted file mode 100644 index 545b0574..00000000 --- a/R/tm_scale_composition.R +++ /dev/null @@ -1,33 +0,0 @@ -#' Scales: composition -#' -#' Scales in tmap are configured by the family of functions with prefix `tm_scale`. -#' Such function should be used for the input of the `.scale` arguments in the -#' layer functions (e.g. `fill.scale` in [tm_polygons()]). -#' The function `tm_scale_composition()` is used for the creation of composition glyphs, such as pie charts and donut charts. -#' -#' @param values (generic scale argument) The visual values. For colors (e.g. `fill` or `col` for `tm_polygons()`) this is a palette name from the `cols4all` package (see [cols4all::c4a()]) or vector of colors, for size (e.g. `size` for `tm_symbols()`) these are a set of sizes (if two values are specified they are interpret as range), for symbol shapes (e.g. `shape` for [tm_symbols()]) these are a set of symbols, etc. The tmap option `values.var` contains the default values per visual variable and in some cases also per data type. -#' @param values.repeat (generic scale argument) Should the values be repeated in case there are more categories? -#' @param values.range (generic scale argument) Range of the values. Vector of two numbers (both between 0 and 1) where the first determines the minimum and the second the maximum. Full range, which means that all values are used, is encoded as `c(0, 1)`. For instance, when a grey scale is used for color (from black to white), `c(0,1)` means that all colors are used, `0.25, 0.75` means that only colors from dark grey to light grey are used (more precisely `"grey25"` to `"grey75"`), and `0, 0.5` means that only colors are used from black to middle grey (`"grey50"`). When only one number is specified, this is interpreted as the second number (where the first is set to 0). Default values can be set via the tmap option `values.range`. -#' @param values.scale (generic scale argument) Scaling of the values. Only useful for size-related visual variables, such as `size` of [tm_symbols()] and `lwd` of [tm_lines()]. -#' @param value.na (generic scale argument) Value used for missing values. See tmap option `"value.na"` for defaults per visual variable. -#' @param value.null (generic scale argument) Value used for NULL values. See tmap option `"value.null"` for defaults per visual variable. Null data values occur when out-of-scope features are shown (e.g. for a map of Europe showing a data variable per country, the null values are applied to countries outside Europe). -#' @param value.neutral (generic scale argument) Value that can be considered neutral. This is used for legends of other visual variables of the same map layer. E.g. when both `fill` and `size` are used for [tm_symbols()] (using filled circles), the size legend items are filled with the `value.neutral` color from the `fill.scale` scale, and fill legend items are bubbles of size `value.neutral` from the `size.scale` scale. -#' @param labels (generic scale argument) Labels -#' @param label.na (generic scale argument) Label for missing values -#' @param label.null (generic scale argument) Label for null (out-of-scope) values -#' @export -#' @name tm_scale_glyph_composition -#' @rdname tm_scale_glyph_composition -tm_scale_composition = function( - values = NA, - values.repeat = FALSE, - values.range = NA, - values.scale = 1, - value.na = NA, - value.null = NA, - value.neutral = NA, - labels = NULL, - label.na = NA, - label.null = NA) { - structure(c(list(FUN = "tmapScaleComposition"), as.list(environment())), class = c("tm_scale_composition", "tm_scale", "list")) -} diff --git a/R/tmapLeafletLegend.R b/R/tmapLeafletLegend.R index defd1583..dde83d5b 100644 --- a/R/tmapLeafletLegend.R +++ b/R/tmapLeafletLegend.R @@ -84,7 +84,6 @@ make_equal_list = function(x) { tmapLeaflet_legend = function(cmp, lf, o, orientation) { - group = "tmp" # TODO leg_className = paste("info legend", gsub(" ", "", group, fixed = TRUE)) layerId = paste0("legend", sprintf("%02d", .TMAP_LEAFLET$leg_id)) # "legend401" #todo diff --git a/R/tmapScaleComposition.R b/R/tmapScaleComposition.R deleted file mode 100644 index bebafa7f..00000000 --- a/R/tmapScaleComposition.R +++ /dev/null @@ -1,91 +0,0 @@ -tmapScaleComposition = function(..., scale, legend, chart, o, aes, layer, layer_args, sortRev, bypass_ord, submit_legend = TRUE) { - args = list(...) - - ct = NULL # what is it again? - cls = c("fact", "unord") - - scale = get_scale_defaults(scale, o, aes, layer, cls, ct) - - show.messages <- o$show.messages - show.warnings <- o$show.warnings - - n = length(args) - - with(scale, { - fun_getCVV = paste0("tmapValuesCVV_", aes) - VV = do.call(fun_getCVV, list(x = values, value.na = value.na, n = n, range = values.range, scale = values.scale, rep = values.repeat, o = o)) - - values = VV$vvalues - value.na = VV$value.na - - sfun = paste0("tmapValuesScale_", aes) - cfun = paste0("tmapValuesColorize_", aes) - if (is.na(value.neutral)) value.neutral = VV$value.neutral else value.neutral = do.call(sfun, list(x = do.call(cfun, list(x = value.neutral, pc = o$pc)), scale = values.scale)) - - mfun = paste0("tmapValuesSubmit_", aes) - values = do.call(mfun, list(x = values, args = layer_args)) - value.na = do.call(mfun, list(x = value.na, args = layer_args)) - value.neutral = do.call(mfun, list(x = value.neutral, args = layer_args)) - - totals = Reduce("+", args) - mx = max(totals) - val_list = lapply(args, function(a) a / mx) - - vals = do.call(encode_mv, c(val_list, list(digits = 4))) - labs = paste0("label", 1:n) - - value.neutral = vals[1] - - icon_scale = if (getOption("tmap.mode") == "plot") layer_args$icon.scale else 1 - - legend = within(legend, { - nitems = length(labs) - labels = labs - dvalues = values - vvalues = values - vneutral = value.neutral - icon_scale = icon_scale - na.show = FALSE - scale = "composition" - }) - - - chartFun = paste0("tmapChart", toTitleCase(chart$summary)) - chart = do.call(chartFun, list(chart)) - - if (submit_legend) { - if (bypass_ord) { - format_aes_results(vals, legend = legend, chart = chart) - } else { - format_aes_results(vals, ord = 1L, legend = legend, chart = chart) - } - } else { - list(vals = vals, ids = 1L, legend = legend, chart = chart, bypass_ord = bypass_ord) - } - }) - - - - -} - -encode_mv = function(..., digits = 4) { - args = list(...) - - k = length(args) - m = seq(0, by = digits, length.out = k) - - lst = mapply(function(v, mi) { - v * 10^(digits-1+mi) - }, args, m, SIMPLIFY = FALSE) - Reduce("+", lst) -} - -decode_mv = function(x, digits = 4) { - m_temp = seq(0, by = digits, length.out = 20) - k = which(vapply(m_temp, function(mi) all(x < 10^mi), FUN.VALUE = logical(1)))[1] - 1L - m = seq(0, by = digits, length.out = k) - lst = lapply(m, function(mi) { - (floor(x / 10^mi) %% 10^digits) / 10^(digits-1L) - }) -} \ No newline at end of file diff --git a/R/tmapScale_.R b/R/tmapScale_.R index 34d1c682..eaa75271 100644 --- a/R/tmapScale_.R +++ b/R/tmapScale_.R @@ -189,7 +189,8 @@ tmapScaleAuto = function(x1, scale, legend, chart, o, aes, layer, layer_args, so if (k == 2) { sc = "bivariate" } else if (k > 2) { - sc = "composition" + stop("No default scale for multivariate variables", call. = FALSE) + #sc = "composition" } else if (cls[1] == "asis") { sc = "asis" } else if (attr(cls, "unique") && !(sc_opt == "asis")) {