tmap icon indicating copy to clipboard operation
tmap copied to clipboard

FYI: tm_group added (layer visibility zoom levels)

Open mtennekes opened this issue 1 year ago • 2 comments

Each layer has two arguments group and group.control. I found out that leaflet maps can also handle visibility per zoom level via leaflet::groupOptions(). So instead of added yet another argument to each layer function (group.zoom_levels), I made a separate function tm_group for all the group handling (also group.control). This is also more elegant because there can be multiple layers per group.

If you agree, I'll remove group.control from the layer functions. Still, I need to set the default group.control inside the layer function (under the hood): e.g. basemap -> radio, vector layer -> checkbox, aux layer -> none.

Use case 1: different scaling

library(tmap)
tmap_mode("view")
library(rnaturalearth)
library(rnaturalearthdata)

ne10 = ne_countries(scale = 10)
ne50 = ne_countries(scale = 50)
ne110 = ne_countries(scale = 110)

tm_shape(ne110) +
	tm_polygons(group = "ne110") +
tm_shape(ne50) +
	tm_polygons(group = "ne50") +
tm_shape(ne10) +
	tm_polygons(group = "ne10") +
tm_group("ne110", zoom_levels = 0:2) +
tm_group("ne50", zoom_levels = 3:6) +
tm_group("ne10", zoom_levels = 7:15)

Use case 2: different zonings

library(giscoR)
library(sf)

x = giscoR::gisco_bulk_download(id_giscoR = "nuts", cache_dir = "temp", year = 2021)

s_rg0 = st_read("temp/NUTS_RG_10M_2021_3035_LEVL_0.geojson")
s_rg1 = st_read("temp/NUTS_RG_10M_2021_3035_LEVL_1.geojson")
s_rg2 = st_read("temp/NUTS_RG_10M_2021_3035_LEVL_2.geojson")
s_rg3 = st_read("temp/NUTS_RG_10M_2021_3035_LEVL_3.geojson")

tm_shape(s_rg0) +
	tm_polygons(fill = "gold", group = "NUTS0") + 
	tm_shape(s_rg1) +
	tm_polygons(fill = "steelblue", group = "NUTS1") + 
	tm_shape(s_rg2) +
	tm_polygons(fill = "palegreen", group = "NUTS2") + 
	tm_shape(s_rg3) +
	tm_polygons(fill = "darkred", group = "NUTS3") +
tm_group("NUTS0", zoom_levels = 0:2) +
tm_group("NUTS1", zoom_levels = 6:7) +
tm_group("NUTS2", zoom_levels = 8:10) +
tm_group("NUTS3", zoom_levels = 11:15)

Please review @Nowosad @Robinlovelace @olivroy

I'll update the NLD_prov and NLD_muni datasets, and probably will add NLD_dist #967 . Then I'm able to add some examples to the documentation.

mtennekes avatar Nov 26 '24 09:11 mtennekes

Almost working, but legends are always on...

I'm trying to translate this leaflet example

leaflet(quakes) %>%
	addProviderTiles(providers$Esri.OceanBasemap, group = "basic") %>%
	addMarkers(data = quakes, group = "basic") %>%
	addCircleMarkers(group = "detail", fillOpacity = 0.5,
					 radius = ~mag * 5, color = ~pal(mag), stroke = FALSE) %>%
	addLegend(pal = pal, values = ~mag, group = "detail", position = "bottomleft")  %>%
	groupOptions("detail", zoomLevels = 7:18)

... to tmap:

tm_shape(st_as_sf(quakes, coords = c("long","lat"), crs = 4326)) +
	tm_basemap("Esri.OceanBasemap", group = "basic") +
	tm_bubbles(fill_alpha = 0.5, size = "mag", fill = "mag", col = NA, fill.scale = tm_scale_continuous(values = "yl_or_rd"), size.scale = tm_scale_continuous(values.scale = 3), group = "detail") +
	tm_markers(options = opt_tm_markers(clustering = FALSE), group = "basic") +
	tm_group("detail", zoom_levels = 7:18) +
	tm_view(use_WebGL = FALSE)

mtennekes avatar Nov 27 '24 21:11 mtennekes

@mtennekes -- I checked the first two examples -- they are great. One question, have you considered using the functions in order (thus removing the need to repeating the group name):

tm_shape(s_rg0) +
  tm_polygons(fill = "gold", group = "NUTS0") + 
  tm_group(zoom_levels = 0:2) 

Nowosad avatar Nov 28 '24 12:11 Nowosad