-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathexamples_bubble.Rmd
More file actions
124 lines (98 loc) · 3.81 KB
/
examples_bubble.Rmd
File metadata and controls
124 lines (98 loc) · 3.81 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
---
title: "tmap example: bubble map"
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, message = FALSE}
library(tmap)
library(dplyr)
library(sf)
tmap_options(scale = 0.75)
```
## About the data
In tmap, there are two example data sets of the Netherlands:
* one for districts, `NLD_dist` (3340 in total),
* one for municipalities: `NLD_muni` (345 in total), and
* one for provinces: `NLD_prov` (12 in total).
The first two datasets contain demographic data of the Netherlands as of 2022. The third dataset only contains metadata (names and codes) and will only be used to plot province borders.
```{r, out.width="100%", fig.height = 3.5}
tmap_arrange(
qtm(NLD_dist, title = "NLD_dist: 3340 districts"),
qtm(NLD_muni, title = "NLD_mini: 345 municipalities"),
qtm(NLD_prov, title = "NLD_prov: 12 provinces"),
ncol = 3
)
```
The variables in the `NLD_dist` and `NLD_muni` are the same:
```{r}
names(NLD_dist)
```
We will create a choropleth about the variable `"edu_appl_sci"`, which contains the percentage of 15-75 year old people who have a university degree (Dutch: WO) or applied sciences degree (Dutch: HBO), per district as of 1st October 2022.
## Population of Dutch municipalities
The following code creates a bubble map of population per
```{r, fig.height = 7}
tm_shape(NLD_muni) +
tm_polygons(fill = "grey95", col = "grey60") +
tm_symbols(
size = "population",
fill = "steelblue",
col = "black",
lwd = 1,
size.scale = tm_scale(values.scale = 2)
)
```
Note the scale function `tm_scale()` is general purpose. Because the data is categorical, it will forward its arguments to `tm_scale_categorical()`.
## Higher income class in Dutch districts
The following map shows the percentage of people with a higher income per Dutch district. This is encoded via a interval 'rainbow' color scale. Bubble sizes are proportional to the population size.
```{r, fig.height = 7}
tm_shape(NLD_dist) +
tm_symbols(
size = "population",
fill = "income_high",
col = "black",
size.scale = tm_scale_continuous(values.scale = 2),
fill.scale = tm_scale_intervals(n = 5, values = "-tol.rainbow_wh_br"))
```
In order to find a suitable color palette for this example, we used
```{r, eval=FALSE}
cols4all::c4a_gui()
```
and selected a sequential 'rainbow' like color scale which is color blind friendly, and where the colors are not too bright and not too dark, because we would like to have sufficient contrast with both the black borders and the white background (see the [working paper of cols4all](https://mtennekes.github.io/cols4all/articles/01_paper.html)). The `-` symbol indicates that we reverse the palette.
Note that we set `n` the number of class intervals to 5. However, for the standard interval method, which is called `pretty`, the parameter `n` is the preferred number of classes. In this case the resulting number of classes is 5 (just like `pretty(c(0,60), 5)`).