Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jan 13, 2025
1 parent 9c51478 commit f2ff0a1
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions vignettes/adv_groups.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: "tmap advanced: layer groups (view mode only)"
output:
bookdown::html_vignette2:
pkgdown:
as_is: true
template:
math-rendering: mathjax
bibliography: '`r system.file("tmap.bib", package="tmap")`'
csl: "`r system.file('ieee.csl', package = 'tmap')`"
editor_options:
chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
fig.width = 8,
comment = "#>"
)
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
more <- "..."
if (length(lines)==1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- c(more, x[lines], more)
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})
```


```{r, echo = FALSE, message = FALSE}
library(tmap)
tmap_options(scale = 0.75)
```



## Layer groups

Via the `group` and `group.control` argument in the map layer functions it is possible group layers and (de)select in the leaflet layer control box.

```{r}
tmap_mode("view")
tm_shape(NLD_dist) +
tm_polygons(
fill = "dwelling_value",
col = NULL,
fill.scale = tm_scale_intervals(values = "-brewer.rd_yl_bu", breaks = c(75, 150, 250, 500, 750, 1000, 1600)),
fill.legend = tm_legend("Dwelling value (x 1000)"),
group = "District data") +
tm_shape(NLD_muni) +
tm_borders(lwd = 1, col = "black", group = "Municipality borders") +
tm_title("Select and deselect groups") +
tm_shape(NLD_prov, name = "Province borders") +
tm_borders(lwd = 3) +
tm_view(set_zoom_limits = c(8,14))
```

Important is that the plotting order of layers is not affected by the grouping, but specified by the `z` argument.
When these are not specified (such as in this example), the order in the plot call is taken (except for `tm_basemap()` which is always drawn at the bottom).

Note that if the `group` argument is not specified, such as in the province layer, the group name is inherited from `tm_shape()`.


## Switching between layers

```{r}
tmap_mode("view")
tm_shape(NLD_dist) +
tm_polygons(
fill = "dwelling_value",
col = NULL,
fill.scale = tm_scale_intervals(values = "-brewer.rd_yl_bu", breaks = c(75, 150, 250, 500, 750, 1000, 1600)),
fill.legend = tm_legend("Dwelling value (x 1000)"),
group = "District data",
group.control = "radio") +
tm_shape(NLD_muni) +
tm_polygons(
fill = "dwelling_value",
col = NULL,
fill.scale = tm_scale_intervals(values = "-brewer.rd_yl_bu", breaks = c(75, 150, 250, 500, 750, 1000, 1600)),
fill.legend = tm_legend("Dwelling value (x 1000)"),
group = "Municipality data",
group.control = "radio") +
tm_title("Switching between layers") +
tm_shape(NLD_prov) +
tm_borders(lwd = 3, group.control = "none") +
tm_basemap("OpenStreetMap") +
tm_view(set_zoom_limits = c(8,14))
```



## Zoom-specific map layers

Via `tm_group` it is possible to set the `group.control` and moreover to specify at which zoom levels this group is plotted.

```{r}
tmap_mode("view")
tm_shape(NLD_dist) +
tm_polygons(
fill = "dwelling_value",
fill.scale = tm_scale_intervals(values = "-brewer.rd_yl_bu", breaks = c(75, 150, 250, 500, 750, 1000, 1600)),
fill.legend = tm_legend("Dwelling value (x 1000)"),
group = "District") +
tm_shape(NLD_muni) +
tm_polygons(
fill = "dwelling_value",
fill.scale = tm_scale_intervals(values = "-brewer.rd_yl_bu", breaks = c(75, 150, 250, 500, 750, 1000, 1600)),
fill.legend = tm_legend_hide(),
group = "Municipality") +
tm_borders(group = "District", lwd = 2) +
tm_title("Zoom in for district level (and out for municipality level)") +
tm_shape(NLD_prov) +
tm_borders(lwd = 3, group.control = "none") +
tm_group("Municipality", zoom_levels = 8:10) +
tm_group("District", zoom_levels = 11:14) +
tm_view(set_zoom_limits = c(8,14))
```

0 comments on commit f2ff0a1

Please sign in to comment.