Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Feb 7, 2024
1 parent a407842 commit cbdad3f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 67 deletions.
16 changes: 8 additions & 8 deletions R/tmapGridComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 7 additions & 11 deletions R/tmapGridLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions R/tmapLeafletLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}


Expand Down
2 changes: 1 addition & 1 deletion R/tmap_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions examples/tm_text.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
59 changes: 16 additions & 43 deletions sandbox/issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit cbdad3f

Please sign in to comment.