-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathbasics_charts.Rmd
More file actions
157 lines (130 loc) · 3.63 KB
/
basics_charts.Rmd
File metadata and controls
157 lines (130 loc) · 3.63 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
148
149
150
151
152
153
154
155
156
157
---
title: "tmap basics: charts"
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.8)
```
## Introduction
Each visual variable (e.g. `fill` in `tm_polygons()`) has an additional `.chart` argument via which charts can be shown:
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons(
fill = "press",
fill.scale = tm_scale_intervals(n=10, values = "scico.hawaii"),
fill.legend = tm_legend("World Press\nFreedom Index"),
fill.chart = tm_chart_bar()) +
tm_crs("auto")
```
## Chart types
### Numeric variables
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons("HPI",
fill.scale = tm_scale_intervals(),
fill.chart = tm_chart_donut())
```
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons("HPI",
fill.scale = tm_scale_intervals(),
fill.chart = tm_chart_box())
```
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons("HPI",
fill.scale = tm_scale_intervals(),
fill.chart = tm_chart_violin())
```
### Categorical variable
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons("economy",
fill.scale = tm_scale_categorical(),
fill.chart = tm_chart_bar())
```
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons("economy",
fill.scale = tm_scale_categorical(),
fill.chart = tm_chart_donut())
```
### Bivariate charts
```{r, fig.height = 5}
tm_shape(World) +
tm_polygons(tm_vars(c("HPI", "well_being"), multivariate = TRUE),
fill.chart = tm_chart_heatmap())
```
## Position
We can update the position of the chart to bottom right (in a separate frame). See [vignette about positioning](https://r-tmap.github.io/tmap/articles/adv_positions).
```{r, fig.height = 6}
tm_shape(World) +
tm_polygons(
fill = "press",
fill.scale = tm_scale_intervals(n=10, values = "scico.hawaii"),
fill.legend = tm_legend("World Press\nFreedom Index"),
fill.chart = tm_chart_bar(position = tm_pos_out("center", "bottom", pos.h = "right"))) +
tm_crs("auto")
```
Or, in case we would like the chart to be next to the legend, but in a different frame:
```{r, fig.height = 6}
tm_shape(World) +
tm_polygons(
fill = "press",
fill.scale = tm_scale_intervals(n=10, values = "scico.hawaii"),
fill.legend = tm_legend("World Press\nFreedom Index", group.frame = FALSE),
fill.chart = tm_chart_bar(position = tm_pos_out("center", "bottom", align.v = "top"))) +
tm_layout(component.stack_margin = .5) +
tm_crs("auto")
```
## Additional ggplot2 code
```{r, fig.height = 6}
require(ggplot2)
tm_shape(World) +
tm_polygons("HPI",
fill.scale = tm_scale_intervals(),
fill.chart = tm_chart_bar(
extra.ggplot2 = theme(
panel.grid.major.y = element_line(colour = "red")
))
)
```