-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathbasics_basemaps.Rmd
More file actions
128 lines (99 loc) · 2.93 KB
/
basics_basemaps.Rmd
File metadata and controls
128 lines (99 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
title: "tmap basics: basemaps"
output:
bookdown::html_vignette2:
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(
collapse = TRUE,
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)
```
## Basemaps
By default, basemaps are only shown in `"view"` mode, so not in `"plot"` mode. Basemaps can be enabled or disabled via `tm_basemap()`.
## Plot mode
To enable a basemap in plot mode, the package [`maptiles`](https://github.com/riatelab/maptiles/) is required. When specified without arguments, the default basemap is "Esri.WorldGrayCanvas". This can be changed, e.g. `"OpenTopoMap"`.
```{r, fig.height = 3.5}
tm_shape(metro) +
tm_bubbles(size = "pop2020") +
tm_basemap("OpenTopoMap")
```
The options are:
```{r, eval=FALSE}
tmap_providers()
```
<details>
<summary>Show</summary>
```{r, eval=TRUE, echo=FALSE}
tmap_providers()
```
</details>
Tip: `.tmap_providers` contains the same information, but stored as an environment, which is convenient with auto-completion (e.g. RStudio); type `.tmap_providers$` and the list of options will appear.
Note: for Thunderforest and Stadia, a (free for personal use) API key is required.
See [previews](https://leaflet-extras.github.io/leaflet-providers/preview/)
```{r, fig.height = 3.5}
tm_shape(metro) +
tm_bubbles(size = "pop2020") +
tm_basemap("CartoDB.PositronNoLabels")
```
## View mode
In view mode, there are even more options. There can be obtained via `names(leaflet::providers)`.
```{r}
tmap_mode("view")
tm_shape(metro) +
tm_bubbles(size = "pop2020") +
tm_basemap("Esri.OceanBasemap")
```
Disabling basemaps:
```{r}
tmap_mode("view")
tm_shape(World) +
tm_polygons(
fill = "grey80",
col = "grey60") +
tm_shape(metro) +
tm_bubbles(size = "pop2020") +
tm_basemap(NULL)
```
Now it is also possible to use different map projections (see [vignette](https://r-tmap.github.io/tmap/articles/foundations_crs)):
```{r}
tmap_mode("view")
tm_shape(World, crs = "+proj=robin") +
tm_polygons(
fill = "grey80",
col = "grey60") +
tm_shape(metro) +
tm_bubbles(size = "pop2020") +
tm_basemap(NULL)
```
## Defaults
(run `tmap_options("basemap.server")` to see this option)