-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.Rmd
More file actions
181 lines (145 loc) · 4.62 KB
/
README.Rmd
File metadata and controls
181 lines (145 loc) · 4.62 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
output: rmarkdown::github_document
editor_options:
chunk_output_type: console
---
```{r pkg-knitr-opts, include=FALSE}
knitr::opts_chunk$set(
collapse=TRUE, fig.retina=2, message=FALSE, warning=FALSE,
fig.path = "man/figures/README-"
)
options(width=120)
```
[](https://travis-ci.org/hrbrmstr/worldtilegrid)
[](https://codecov.io/gh/hrbrmstr/worldtilegrid)
[](https://cran.r-project.org/package=worldtilegrid)
# worldtilegrid
A ggplot2 Geom for World Tile Grids
## Description
A "tile grid map" is a cartogram that uses same-sized tiles in approximate, relative positions of each other to represent a world map. The world tile grid relative position reference system used by this 'ggplot2' 'Geom/Stat' was the original work of 'Jon Schwabish' and converted to 'CSV' by 'Maarten Lambrechts'.
- Ref: <https://policyviz.com/2017/10/12/the-world-tile-grid-map/>
- Ref: <http://www.maartenlambrechts.com/2017/10/22/tutorial-a-worldtilegrid-with-ggplot2.html>
## What's Inside The Tin
The following functions are implemented:
```{r ingredients, results='asis', echo=FALSE, cache=FALSE}
hrbrpkghelpr::describe_ingredients()
```
The following _data_ is included/exported:
`wtg`: World Tile Grid Basemap Data
## Installation
```{r install-ex, results='asis', echo = FALSE}
hrbrpkghelpr::install_block()
```
## Usage
```{r message=FALSE, warning=FALSE, error=FALSE}
library(worldtilegrid)
library(tidyverse)
# current verison
packageVersion("worldtilegrid")
```
### Example (All countries are in the data set)
```{r message=FALSE, warning=FALSE, error=FALSE, fig.width=8, fig.height=8, fig.retina=2}
iso3cs <- worldtilegrid::wtg$alpha.3
set.seed(1)
data_frame(
ctry = iso3cs,
`Thing Val` = sample(1000, length(ctry))
) -> xdf
ggplot(xdf, aes(country = ctry, fill = `Thing Val`)) +
geom_wtg(border_size = 0.5, border_col = "#252a32") +
geom_text(
aes(
label = stat(alpha.2),
colour = I(ifelse(`Thing Val` < 500, "white", "black"))
),
stat = "wtg", size = 2
) +
coord_equal() +
viridis::scale_fill_viridis() +
labs(title = "World Tile Grid") +
hrbrthemes::theme_ft_rc() +
theme_enhance_wtg()
```
### Example (Only a few countries are in the data set)
```{r message=FALSE, warning=FALSE, error=FALSE, fig.width=8, fig.height=8, fig.retina=2}
set.seed(1)
data_frame(
ctry = worldtilegrid::wtg$alpha.3[1:30],
`Thing Val` = sample(1000, length(ctry))
) -> xdf
ggplot(xdf, aes(country = ctry, fill = `Thing Val`)) +
geom_wtg() +
geom_text(
aes(
label = stat(alpha.2),
colour = I(ifelse(`Thing Val` < 400, "#b2b2b2", "#2b2b2b"))
),
stat="wtg", size=2
) +
coord_equal() +
viridis::scale_fill_viridis() +
labs(title = "World Tile Grid") +
hrbrthemes::theme_ft_rc() +
theme_enhance_wtg()
```
### Facet Example (All countries are in the data set)
```{r message=FALSE, warning=FALSE, error=FALSE, fig.width=10, fig.height=6, fig.retina=2}
set.seed(1)
data_frame(
ctry = worldtilegrid::wtg$alpha.3,
`Thing Val` = sample(1000, length(ctry)),
grp = 'Thing One'
) -> xdf1
data_frame(
ctry = worldtilegrid::wtg$alpha.3,
`Thing Val` = sample(1000, length(ctry)),
grp = 'Thing Two'
) -> xdf2
bind_rows(
xdf1,
xdf2
) -> xdf
ggplot(xdf, aes(country = ctry, fill = `Thing Val`)) +
geom_wtg() +
coord_equal() +
facet_wrap(~grp) +
viridis::scale_fill_viridis() +
labs(title = "World Tile Grid Facets") +
hrbrthemes::theme_ft_rc() +
theme_enhance_wtg()
```
### Facet Example (Only a few countries are in the data set)
The geom will fill in the gaps for you:
```{r message=FALSE, warning=FALSE, error=FALSE, fig.width=10, fig.height=6, fig.retina=2}
set.seed(1)
tibble(
ctry = sample(iso3cs, 40),
`Thing Val` = sample(1000, length(ctry)),
grp = 'Thing One'
) -> xdf1
tibble(
ctry = sample(iso3cs, 40),
`Thing Val` = sample(1000, length(ctry)),
grp = 'Thing Two'
) -> xdf2
bind_rows(
xdf1,
xdf2
) -> xdf
ggplot(xdf, aes(country = ctry, fill = `Thing Val`)) +
geom_wtg(border_size = 0.5) +
coord_equal() +
facet_wrap(~grp) +
scale_fill_viridis_c(
na.value = alpha(hrbrthemes::ft_cols$gray, 2/5), direction = -1,
option = "magma"
) +
guides(
fill = guide_colourbar(title.position = "top", title.hjust = 0.5)
) +
labs(title = "World Tile Grid Facets") +
hrbrthemes::theme_ipsum_rc() +
theme_enhance_wtg() +
theme(legend.position = "bottom") +
theme(legend.key.width = unit(2, "lines"))
```