Skip to content

Commit

Permalink
added tm_scale_continuous_sqrt and option component.autoscale
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 12, 2024
1 parent 9559083 commit f1a7296
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions R/misc_trans_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions R/tm_scale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions R/tmapGridLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion R/tmap_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions man/tm_scale_continuous.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1a7296

Please sign in to comment.