From f1a72961f9a77a704bbe6164f4024a422b911580 Mon Sep 17 00:00:00 2001 From: mtennekes Date: Fri, 12 Apr 2024 16:33:06 +0200 Subject: [PATCH] added tm_scale_continuous_sqrt and option component.autoscale --- NAMESPACE | 1 + R/misc_trans_data.R | 8 ++++++++ R/tm_scale_.R | 7 +++++++ R/tmapGridLegend.R | 23 ++++++++++++++++------- R/tmap_options.R | 4 +++- man/tm_scale_continuous.Rd | 3 +++ 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 50d51a29..275b05d1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -207,6 +207,7 @@ export(tm_scale_categorical) export(tm_scale_continuous) export(tm_scale_continuous_log) export(tm_scale_continuous_log1p) +export(tm_scale_continuous_sqrt) export(tm_scale_discrete) export(tm_scale_intervals) export(tm_scale_ordinal) diff --git a/R/misc_trans_data.R b/R/misc_trans_data.R index 9ef1c7d4..33d21288 100644 --- a/R/misc_trans_data.R +++ b/R/misc_trans_data.R @@ -14,6 +14,14 @@ trans_log = list( # domain = c(-Inf, Inf) # ) + +trans_sqrt = list( + fun = sqrt, + rev = function(x) x^2, + domain = c(0, Inf) +) + + trans_log1p = list( fun = log1p, rev = expm1, diff --git a/R/tm_scale_.R b/R/tm_scale_.R index e24babc5..6233ef79 100644 --- a/R/tm_scale_.R +++ b/R/tm_scale_.R @@ -309,6 +309,13 @@ tm_scale_continuous_log1p = function(...) { tm_scale_continuous(trans = "log1p", ...) } +#' @export +#' @rdname tm_scale_continuous +#' @name tm_scale_continuous_sqrt +tm_scale_continuous_sqrt = function(...) { + tm_scale_continuous(trans = "sqrt", ...) +} + # # #' @export # #' @rdname tm_scale_continuous diff --git a/R/tmapGridLegend.R b/R/tmapGridLegend.R index 38507d4b..aee7079f 100644 --- a/R/tmapGridLegend.R +++ b/R/tmapGridLegend.R @@ -49,7 +49,14 @@ tmapGridCompCorner = function(comp, o, stack, pos.h, pos.v, maxH, maxW, offsetIn scaleH = legHin / maxH # because of legend frames (for which margins are added in tmapGridLegend), the scale may be a bit above 1, even though automatic layout is applied and there is enough space - if (any(scaleW > 1.1) || any(scaleH > 1.1)) warning("Some legend items or map compoments do not fit well (e.g. due to the specified font size).", call. = FALSE) + + if (o$component.autoscale) { + if (any(scaleW > 1.1) || any(scaleH > 1.1)) message("Some legend items or map compoments do not fit well (e.g. due to the specified font size). They are rescaled Set the tmap option component.autoscale to FALSE to disable this.", call. = FALSE) + } else { + scaleW[scaleW > 1.1] = 1 + scaleH[scaleH > 1.1] = 1 + } + clipW = pmax(1, scaleW) clipH = pmax(1, scaleH) @@ -77,13 +84,15 @@ tmapGridCompCorner = function(comp, o, stack, pos.h, pos.v, maxH, maxW, offsetIn scaleS = sum(legWin) / maxW } - if (scaleS > 1.01) { - if (scaleS > 1.05) { - message("(Some) components or legends are too ", ifelse(stack == "vertical", "high", "wide"), " and are therefore rescaled.") + if (o$component.autoscale) { + if (scaleS > 1.01) { + if (scaleS > 1.05) { + message("(Some) components or legends are too ", ifelse(stack == "vertical", "high", "wide"), " and are therefore rescaled. Set the tmap option component.autoscale to FALSE to disable this.") + } + legWin = legWin / scaleS + legHin = legHin / scaleS + clipT = clipT * scaleS } - legWin = legWin / scaleS - legHin = legHin / scaleS - clipT = clipT * scaleS } legW = grid::unit(legWin, "inches") diff --git a/R/tmap_options.R b/R/tmap_options.R index f1097d3e..e0beddad 100644 --- a/R/tmap_options.R +++ b/R/tmap_options.R @@ -129,7 +129,7 @@ xmod = c(0, 1), ymod = c(0, 1), angle = c(0, 360)), - values.range = list(fill = NA, col = NA, size = c(0, 1), lwd = c(0.1, 1), + values.range = list(fill = NA, col = NA, size = c(0, 1), lwd = c(0, 1), lty = NA, text = NA, fontface = NA, fill_alpha = NA, col_alpha = NA, shape = NA, angle = NA), # NA = automatic, NULL is not applicable value.neutral = list(size = 0.75, @@ -275,6 +275,8 @@ pos.h = "left", pos.v = "top", align.h = "left", align.v = "top", just.h = "left", just.v = "top")), + component.autoscale = TRUE, + # legend legend.show = TRUE, legend.design = "standard", diff --git a/man/tm_scale_continuous.Rd b/man/tm_scale_continuous.Rd index 692a7d6b..f0b4a955 100644 --- a/man/tm_scale_continuous.Rd +++ b/man/tm_scale_continuous.Rd @@ -4,6 +4,7 @@ \alias{tm_scale_continuous} \alias{tm_scale_continuous_log} \alias{tm_scale_continuous_log1p} +\alias{tm_scale_continuous_sqrt} \title{Scales: continuous scale} \usage{ tm_scale_continuous( @@ -29,6 +30,8 @@ tm_scale_continuous( tm_scale_continuous_log(...) tm_scale_continuous_log1p(...) + +tm_scale_continuous_sqrt(...) } \arguments{ \item{n}{Preferred number of tick labels. Only used if \code{ticks} is not specified}