diff --git a/R/tmapGridComp.R b/R/tmapGridComp.R index ae50463e..fdae5ddc 100644 --- a/R/tmapGridComp.R +++ b/R/tmapGridComp.R @@ -342,12 +342,12 @@ tmapGridCompPrepare.tm_scalebar = function(comp, o) { } } - if (is.na(width)) - width = .25 - else if (width > 1) { - if (show.messages) message("Scale bar width set to 0.25 of the map width") - width = .25 - } + # if (is.na(width)) + # width = .25 + # else if (width > 1) { + # if (show.messages) message("Scale bar width set to 0.25 of the map width") + # width = .25 + # } if (is.na(text.color)) text.color = o$attr.color text.size = text.size * o$scale @@ -389,9 +389,9 @@ tmapGridCompWidth.tm_scalebar = function(comp, o) { marW = comp$margins[c(2,4)] * textS * o$lin - + W = comp$width * textS * o$lin - ws = c(marW[1], 0, marW[2]) + ws = c(marW[1], W, marW[2]) sides = switch(comp$position$align.h, left = "second", right = "first", "both") wsu = set_unit_with_stretch(ws, sides = sides) diff --git a/R/tmapGridLegend.R b/R/tmapGridLegend.R index 71e2a6b9..dac60da2 100644 --- a/R/tmapGridLegend.R +++ b/R/tmapGridLegend.R @@ -287,7 +287,10 @@ tmapGridLegend = function(comp, o, facet_row = NULL, facet_col = NULL, facet_pag ## update scale_bar width: identified by the WnativeID = 3 item (null for other components) comp = mapply(function(cmp, bb) { bbw = bb[3] - bb[1] + + # TRUE if component is scalebar if (!is.null(cmp$WnativeID)) { + # find out whether the bounding of borrowed from the map (yes if scale bar is drawn in another facet) bbox_nb = attr(bbox, "borrow") if (is.null(bbox_nb)) { bb_facet = sum(colsIn) @@ -298,24 +301,17 @@ tmapGridLegend = function(comp, o, facet_row = NULL, facet_col = NULL, facet_pag oldIn = as.numeric(cmp$wsu[cmp$WnativeID]) if (is.null(cmp$WnativeRange)) { - newIn = min(totW, bb_facet * cmp$width) + # in case breaks not defined: allow the width to be maximal fraction of facet width + #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 } - #if (is.null(cmp$WnativeRange)) { - # no specified scare bar width range - # newIn = min(totW, cmp$width * sum(colsIn)) - #} else { - #bbw2 = bbw / sum(colsIn) * totW - # newIn = min(totW, sum(colsIn)) - #newIn = (min(1, ((cmp$WnativeRange / bbw2) / cmp$units$to)) * totW) + (o$lin * cmp$text.size * 3.5) - #} cmp$wsu[cmp$WnativeID] = unit(newIn, "inch") cmp$Win = cmp$Win + (newIn - oldIn) - # get cpi: coordinates per inch - cmp$cpi = unname(bbw / bb_facet) } diff --git a/R/tmapLeafletLegend.R b/R/tmapLeafletLegend.R index 5b0d61e7..b3569f5b 100644 --- a/R/tmapLeafletLegend.R +++ b/R/tmapLeafletLegend.R @@ -220,13 +220,13 @@ tmapLeaflet_legend = function(cmp, lf, o, orientation) { } #' @export -tmapLeafletLegPlot.tm_legend_standard_portrait = function(cmp, lf, o) { - tmapLeaflet_legend(cmp, lf, o, orientation = "vertical") +tmapLeafletLegPlot.tm_legend_standard_portrait = function(comp, lf, o) { + tmapLeaflet_legend(comp, lf, o, orientation = "vertical") } #' @export -tmapLeafletLegPlot.tm_legend_standard_landscape = function(cmp, lf, o) { - tmapLeaflet_legend(cmp, lf, o, orientation = "horizontal") +tmapLeafletLegPlot.tm_legend_standard_landscape = function(comp, lf, o) { + tmapLeaflet_legend(comp, lf, o, orientation = "horizontal") } diff --git a/R/tmap_options.R b/R/tmap_options.R index 9d0ec1a2..7a5f3d9f 100644 --- a/R/tmap_options.R +++ b/R/tmap_options.R @@ -397,7 +397,7 @@ scalebar.show = FALSE, scalebar.breaks=NULL, - scalebar.width=0.3, + scalebar.width=20, scalebar.text.size = .5, scalebar.text.color=NA, scalebar.color.dark="black", diff --git a/examples/tm_text.R b/examples/tm_text.R index 9ba94ff6..32a3e339 100644 --- a/examples/tm_text.R +++ b/examples/tm_text.R @@ -1,3 +1,7 @@ +# Need to be updated. The following is just to illustrate bgcol(_alpha) +tm_shape(World) + tm_polygons() + + tm_text("name", bgcol = "economy", bgcol_alpha = .5, shadow = TRUE) + tm_shape(World) + tm_text(text = "name", size = .4, diff --git a/sandbox/issues.R b/sandbox/issues.R index 8bf493be..a0c41e94 100644 --- a/sandbox/issues.R +++ b/sandbox/issues.R @@ -6,36 +6,12 @@ data(World) tm_shape(World) + tm_polygons("HPI", fill.chart = tm_chart_histogram(position = c("left", "bottom"))) -# issue? -lines <- st_read("example.json") - -tm_shape(lines) + - tm_lines( - col = "Mode", - col.scale = tm_scale( - values = "-Set1"), - lty = "Mode", - lty.scale = tm_scale( - values = c("dotted", "solid")), - lty.legend = tm_legend_combine("col"), - lwd = 2) + - tm_add_legend( - type = "lines", - labels = c("Boat", "Road"), - col.values = "-Set1", - lty.values = c("dotted", "solid"), - title = "Wrong way") - -# bgcol(alpha) - -tm_shape(World) + - tm_text("name", bgcol = "HPI") - -tm_shape(World) + tm_polygons() + - tm_text("name", bgcol = "economy", bgcol_alpha = 1, shadow = TRUE) - - +########################################################### +# https://github.com/r-tmap/tmap/issues/806 +# # credits view mode +########################################################### + library(tmap) # tmap v3.3.4 data(World, package = "tmap") tmap_mode("view") @@ -45,29 +21,26 @@ my_map <- tm_shape(World) + my_map +my_map + tm_credits("Last update in October 2023") + my_map |> tmap_leaflet() |> leaflet::addTiles(attributions = "Last update in October 2023") +########################################################### +# was not working when asp = portrait +# https://github.com/r-tmap/tmap/issues/809 +# changed width back to number of lines +########################################################### -# pos_out tm_shape(World) + - tm_fill() + - tm_scalebar( - position = tm_pos_out()) - - -tm_shape(World) + - tm_fill() + - tm_compass( - position = tm_pos_out()) + tm_fill() + + tm_scalebar(position = tm_pos_out()) -tm_shape(World) + - tm_fill("HPI", fill.legend=tm_legend( - position = tm_pos_out())) - +########################################################### +########################################################### # scale bar outside