tmap
tmap copied to clipboard
Add frame to tm_scale_bar
When using tmap in plot mode with a basemap, using tm_scale_bar()
warrants the bg.color
argument. If it is not used, the scale bar may be hard to distinguish from the basemap. Further, a frame for the scale bar will allow it to be more easily distinguished.
I am wondering if there is an argument similar to legend.frame = T
within tm_layout()
. If not, would this addition be easy to implement? See the reprex below that demonstrates the difficulty in viewing the scale bar with a basemap shown.
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(raster)
#> Loading required package: sp
#>
#> Attaching package: 'raster'
#> The following object is masked from 'package:dplyr':
#>
#> select
library(tmap)
library(tmaptools)
tdir=tempdir()
stateurl = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip"
if(file.exists(paste(tdir,"/cb_2018_us_state_500k.shp",sep=""))==F){
download.file(stateurl, destfile = file.path(tdir, "States.zip"))
unzip(file.path(tdir,"States.zip"),exdir=tdir)}
NYS = read_sf(paste(tdir,"/cb_2018_us_state_500k.shp",sep="")) %>%
filter(NAME=="New York") %>%
st_transform(.,crs=32618)
osm_NYS = read_osm(NYS,ext=1.1)
tm_shape(osm_NYS)+
tm_rgb()+
tm_shape(NYS)+
tm_borders()+
tm_scale_bar(position = c('left','bottom'))+
tm_layout(main.title = 'No scale bar background')
tm_shape(osm_NYS)+
tm_rgb()+
tm_shape(NYS)+
tm_borders()+
tm_scale_bar(bg.color = 'white',
position = c('left','bottom'))+
tm_layout(main.title = 'With scale bar background')
Created on 2021-11-23 by the reprex package (v2.0.0)
Thanks for the suggestion. It will for sure be implemented in v4, but has low priority atm.
This feature will be available in the upcoming tmap version 4; see https://github.com/r-tmap/tmap/issues/700