Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FYI: tm_group added (layer visibility zoom levels) #969

Open
mtennekes opened this issue Nov 26, 2024 · 2 comments
Open

FYI: tm_group added (layer visibility zoom levels) #969

mtennekes opened this issue Nov 26, 2024 · 2 comments
Labels

Comments

@mtennekes
Copy link
Member

mtennekes commented Nov 26, 2024

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
Copy link
Member Author

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)

@Nowosad
Copy link
Member

Nowosad commented Nov 28, 2024

@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) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants