-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathbasics_legends.Rmd
More file actions
147 lines (115 loc) · 4.14 KB
/
basics_legends.Rmd
File metadata and controls
147 lines (115 loc) · 4.14 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
title: "tmap basics: legends"
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(
collapse = TRUE,
out.width = "100%",
dpi = 300,
fig.width = 7.2916667,
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)
```
### About the data
A spatial data object contained in tmap is called `World`. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package):
```{r}
names(World)
```
We specify this object with `tm_shape` (see other vignette) and for convenience assign it to `s`:
```{r}
s = tm_shape(World, crs = "+proj=eqearth")
```
## Legend type
Each visual variable, e.g. `fill` in `tm_polygons` will by default produce a legend. The type of legend depends on used scale (see \href{https://r-tmap.github.io/tmap/articles/basics_scales}{vignette about scales}). E.g. `tm_scale_continuous` applied to `fill` will produce a continuous color gradient:
```{r, fig.height = 3.5}
s + tm_polygons(
fill = "HPI",
fill.scale = tm_scale_continuous(values = "pu_gn"))
```
For `tm_scale_intervals`, there are two styles. A choice can be made via the argument `label.style`. By default it is set to discrete:
```{r, fig.height = 3.5}
s + tm_polygons(
fill = "HPI",
fill.scale = tm_scale_intervals(values = "pu_gn"))
```
Alternatively, it can be set to `"continuous"`:
```{r, fig.height = 3.5}
s + tm_polygons(
fill = "HPI",
fill.scale = tm_scale_intervals(values = "pu_gn", label.style = "continuous"))
```
## `.legend` arguments
Each visual variable also has a `.legend` argment, such as `fill.legend` in `tm_polygons`. This can be used to:
* set the title of the legend,
* change the orientation of the legend (portrait or landscape),
* reverse the legend items,
* hide the legend, and
* change the size and position of the legend.
Note that the content of the legend, so breaks, scales, and ticks are related to the scale and should therefore be specified in the scale functions.
## Title, orientation and size
The following example illustrates how the title, orientation and size and specified:
```{r, fig.height = 3.5}
s + tm_polygons(
fill = "HPI",
fill.scale = tm_scale_continuous(values = "pu_gn"),
fill.legend =
tm_legend(
title = "Happy Planex Index",
orientation = "landscape",
width = 60))
```
The legend width is set in number of text line heights, which corresponds to the number of characters. In other words, the resulting width depends on the font size, which can be set in `tm_legend`, but also via the general `scale` option, which determines the overall 'scale' of the map (all line widths, point sizes and font sizes):
```{r, fig.height = 3.5}
s + tm_polygons(
fill = "HPI",
fill.scale = tm_scale_continuous(values = "pu_gn"),
fill.legend =
tm_legend(
title = "Happy Planex Index",
orientation = "landscape",
width = 60)) +
tm_layout(scale = 0.25)
```
## Hiding legends
A legend can be suppressed by setting the `.legend` argument to `tm_legend_hide()`.
```{r, fig.height = 3.5}
s + tm_polygons(
fill = "HPI",
fill.scale = tm_scale_continuous(values = "pu_gn"),
fill.legend = tm_legend_hide())
```
See the [advanced vignette](https://r-tmap.github.io/tmap/articles/adv_legends) for more options.