From 72f9a5842ce12482c913882cd726151f206d026f Mon Sep 17 00:00:00 2001 From: mtennekes Date: Sat, 24 Aug 2024 11:54:38 +0200 Subject: [PATCH] fixed scalebar width #919 --- R/tmapGridComp.R | 2 ++ R/tmapGridLegend.R | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/R/tmapGridComp.R b/R/tmapGridComp.R index e2c71e3a..5025e75e 100644 --- a/R/tmapGridComp.R +++ b/R/tmapGridComp.R @@ -391,6 +391,8 @@ tmapGridCompWidth.tm_scalebar = function(comp, o) { comp$WnativeID = 3 if (!is.null(comp$breaks)) { comp$WnativeRange = tail(comp$breaks, 1) - comp$breaks[1]# + (comp$breaks[2] - comp$breaks[1]) * 2 + comp$Wextra_line = textS * o$lin# for fitting break labels + } comp diff --git a/R/tmapGridLegend.R b/R/tmapGridLegend.R index 80d1b040..331b5af4 100644 --- a/R/tmapGridLegend.R +++ b/R/tmapGridLegend.R @@ -304,8 +304,17 @@ tmapGridLegend = function(comp, o, facet_row = NULL, facet_col = NULL, facet_pag #newIn = min(totW, bb_facet * cmp$width) newIn = min(totW, cmp$Win) } else { - # in case breaks are defined: allow the total width to be used (as upper bound) - newIn = totW + # in case breaks are defined: compute width + + # for fitting breaks (half of last 'breaks' word + width of units text) + Wextra = (ceiling(nchar(tail(cmp$breaks, 1)) / 2) + 2 + nchar(cmp$units$unit)) * cmp$Wextra_line # for fitting break labels + + # bbw are number of (CRS) units of map width, totalCoords translated to scalebar units + totalCoords = bbw * cmp$units$to + + Wcomp = bb_facet * (cmp$WnativeRange / totalCoords) + Wextra + + newIn = min(totW, Wcomp) } margins = sum(head(as.numeric(cmp$wsu[-cmp$WnativeID]), -1))