-
Notifications
You must be signed in to change notification settings - Fork 8
/
README.Rmd
344 lines (278 loc) · 15.7 KB
/
README.Rmd
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_knit$set(
progress = TRUE,
base.url = "https://raw.githubusercontent.com/dieghernan/tidyterra/main/"
)
knitr::opts_chunk$set(
collapse = TRUE,
tidy = "styler",
comment = "#>",
fig.path = "img/README-",
warning = FALSE,
message = FALSE,
dev = "ragg_png",
dpi = 300,
out.width = "100%"
)
```
# tidyterra <a href="https://dieghernan.github.io/tidyterra/"><img src="man/figures/logo.png" align="right" height="139"/></a>
<!-- badges: start -->
[![CRAN
status](https://www.r-pkg.org/badges/version/tidyterra)](https://CRAN.R-project.org/package=tidyterra)
[![CRAN
results](https://badges.cranchecks.info/worst/tidyterra.svg)](https://cran.r-project.org/web/checks/check_results_tidyterra.html)
[![Downloads](https://cranlogs.r-pkg.org/badges/tidyterra)](https://CRAN.R-project.org/package=tidyterra)
[![DOI](https://joss.theoj.org/papers/10.21105/joss.05751/status.svg)](https://doi.org/10.21105/joss.05751)
[![R-CMD-check](https://github.com/dieghernan/tidyterra/actions/workflows/check-full.yaml/badge.svg)](https://github.com/dieghernan/tidyterra/actions/workflows/check-full.yaml)
[![R-hub](https://github.com/dieghernan/tidyterra/actions/workflows/rhub.yaml/badge.svg)](https://github.com/dieghernan/tidyterra/actions/workflows/rhub.yaml)
[![codecov](https://codecov.io/gh/dieghernan/tidyterra/branch/main/graph/badge.svg?token=blvDmRjcfb)](https://app.codecov.io/gh/dieghernan/tidyterra)
[![CodeFactor](https://www.codefactor.io/repository/github/dieghernan/tidyterra/badge)](https://www.codefactor.io/repository/github/dieghernan/tidyterra)
[![r-universe](https://dieghernan.r-universe.dev/badges/tidyterra)](https://dieghernan.r-universe.dev/tidyterra)
[![Project Status: Active -- The project has reached a stable, usable state and
is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Stack Exchange
questions](https://img.shields.io/stackexchange/stackoverflow/t/tidyterra?logo=stackoverflow&label=stackoverflow%20q%26a)](https://stackoverflow.com/questions/tagged/tidyterra)
[![Works with
terra-devel](https://github.com/dieghernan/tidyterra/actions/workflows/check-terra-devel.yaml/badge.svg)](https://github.com/dieghernan/tidyterra/actions/workflows/check-terra-devel.yaml)
[![Works with
sf-devel](https://github.com/dieghernan/tidyterra/actions/workflows/check-sf-devel.yaml/badge.svg)](https://github.com/dieghernan/tidyterra/actions/workflows/check-sf-devel.yaml)
[![Works with
ggplot2-devel](https://github.com/dieghernan/tidyterra/actions/workflows/check-ggplot2-devel.yaml/badge.svg)](https://github.com/dieghernan/tidyterra/actions/workflows/check-ggplot2-devel.yaml)
[![Works with dplyr and
readr-devel](https://github.com/dieghernan/tidyterra/actions/workflows/check-dplyr-readr.yaml/badge.svg)](https://github.com/dieghernan/tidyterra/actions/workflows/check-dplyr-readr.yaml)
<!-- badges: end -->
The goal of **tidyterra** is to provide common methods of the [**tidyverse**
packages](https://www.tidyverse.org/packages/) for objects created with the
[**terra**](https://CRAN.R-project.org/package=terra) package: `SpatRaster` and
`SpatVector`. It also provides `geoms` for plotting these objects with
[**ggplot2**](https://ggplot2.tidyverse.org/).
Please cite **tidyterra** as:
> Hernangómez, D., (2023). Using the tidyverse with terra objects: the tidyterra
> package. *Journal of Open Source Software*, *8*(91), 5751,
> <https://doi.org/10.21105/joss.05751>.
A BibTeX entry for LaTeX users is:
``` bib
@article{Hernangómez2023,
doi = {10.21105/joss.05751},
url = {https://doi.org/10.21105/joss.05751},
year = {2023},
publisher = {The Open Journal},
volume = {8},
number = {91},
pages = {5751},
author = {Diego Hernangómez},
title = {Using the {tidyverse} with {terra} objects: the {tidyterra} package},
journal = {Journal of Open Source Software}
}
```
## Overview
Full manual of the most recent release of **tidyterra** on **CRAN** is online:
<https://dieghernan.github.io/tidyterra/>
```{r echo=FALSE, results='asis'}
tterra_v <- packageVersion("tidyterra")
l <- unlist(strsplit(as.character(tterra_v), ".", fixed = TRUE))
if (length(l) == 4) {
cat(
"\n",
"You can have a look to the documentation of",
"the dev version in <https://dieghernan.github.io/tidyterra/dev/>"
)
}
```
**tidyverse** methods implemented on **tidyterra** works differently depending
on the type of `Spat*` object:
- `SpatVector`: the methods are implemented using `terra::as.data.frame()`
coercion. Rows correspond to geometries and columns correspond to attributes
of the geometry.
- `SpatRaster`: The implementation on `SpatRaster` objects differs, since the
methods could be applied to layers or to cells. **tidyterra** overall
approach is to treat the layers as columns of a tibble and the cells as rows
(i.e. `select(SpatRaster, 1)` would select the first layer of a
`SpatRaster`).
The methods implemented return the same type of object used as input, unless the
expected behavior of the method is to return another type of object, (for
example, `as_tibble()` would return a `tibble`).
Current methods and functions provided by **tidyterra** are:
| tidyverse method | `SpatVector` | `SpatRaster` |
|---------------------|---------------------|--------------------------------------|
| `tibble::as_tibble()` | ✔️ | ✔️ |
| `dplyr::select()` | ✔️ | ✔️ Select layers |
| `dplyr::mutate()` | ✔️ | ✔️ Create /modify layers |
| `dplyr::transmute()` | ✔️ | ✔️ |
| `dplyr::filter()` | ✔️ | ✔️ Modify cells values and (additionally) remove outer cells. |
| `dplyr::slice()` | ✔️ | ✔️ Additional methods for slicing by row and column. |
| `dplyr::pull()` | ✔️ | ✔️ |
| `dplyr::rename()` | ✔️ | ✔️ |
| `dplyr::relocate()` | ✔️ | ✔️ |
| `dplyr::distinct()` | ✔️ | |
| `dplyr::arrange()` | ✔️ | |
| `dplyr::glimpse()` | ✔️ | ✔️ |
| `dplyr::inner_join()` family | ✔️ | |
| `dplyr::summarise()` | ✔️ | |
| `dplyr::group_by()` family | ✔️ | |
| `dplyr::rowwise()` | ✔️ | |
| `dplyr::count()`, `tally()` | ✔️ | |
| `dplyr::bind_cols()` / `dplyr::bind_rows()` | ✔️ as `bind_spat_cols()` / `bind_spat_rows()` | |
| `tidyr::drop_na()` | ✔️ | ✔️ Remove cell values with `NA` on any layer. Additionally, outer cells with `NA` are removed. |
| `tidyr::replace_na()` | ✔️ | ✔️ |
| `tidyr::fill()` | ✔️ | |
| `tidyr::pivot_longer()` | ✔️ | |
| `tidyr::pivot_wider()` | ✔️ | |
| `ggplot2::autoplot()` | ✔️ | ✔️ |
| `ggplot2::fortify()` | ✔️ to **sf** via `sf::st_as_sf()` | To a **tibble** with coordinates. |
| `ggplot2::geom_*()` | ✔️ `geom_spatvector()` | ✔️ `geom_spatraster()` and `geom_spatraster_rgb()`. |
## :exclamation: A note on performance
**tidyterra** is conceived as a user-friendly wrapper of **terra** using the
**tidyverse** methods and verbs. This approach therefore has a **cost in terms
of performance**.
If you are a **heavy user** of **terra** or you need to work with **big raster
files**, **terra** is much more focused on terms of performance. When possible,
each function of **tidyterra** references to its equivalent on **terra**.
As a rule of thumb if your raster has less than 10.000.000 data slots counting
cells and layers (i.e. `terra::ncell(your_rast)*terra::nlyr(your_rast) < 10e6`)
you are good to go with **tidyterra**.
When plotting rasters, resampling is performed automatically (as `terra::plot()`
does, see the help page). You can adjust this with the `maxcell` parameter.
## Installation
Install **tidyterra** from
[**CRAN**](https://CRAN.R-project.org/package=tidyterra):
```{r, eval=FALSE}
install.packages("tidyterra")
```
You can install the development version of **tidyterra** like so:
```{r, eval=FALSE }
remotes::install_github("dieghernan/tidyterra")
```
Alternatively, you can install **tidyterra** using the
[r-universe](https://dieghernan.r-universe.dev/tidyterra):
```{r, eval=FALSE}
# Enable this universe
install.packages("tidyterra", repos = c(
"https://dieghernan.r-universe.dev",
"https://cloud.r-project.org"
))
```
## Example
### `SpatRasters`
This is a basic example which shows you how to manipulate and plot `SpatRaster`
objects:
```{r example-temp}
library(tidyterra)
library(terra)
# Temperatures
rastertemp <- rast(system.file("extdata/cyl_temp.tif", package = "tidyterra"))
rastertemp
# Rename
rastertemp <- rastertemp %>%
rename(April = tavg_04, May = tavg_05, June = tavg_06)
# Facet all layers
library(ggplot2)
ggplot() +
geom_spatraster(data = rastertemp) +
facet_wrap(~lyr, ncol = 2) +
scale_fill_whitebox_c(
palette = "muted",
labels = scales::label_number(suffix = "º"),
n.breaks = 12,
guide = guide_legend(reverse = TRUE)
) +
labs(
fill = "",
title = "Average temperature in Castille and Leon (Spain)",
subtitle = "Months of April, May and June"
)
# Create maximum differences of two months
variation <- rastertemp %>%
mutate(diff = June - May) %>%
select(variation = diff)
# Add also a overlay of a SpatVector
prov <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
ggplot(prov) +
geom_spatraster(data = variation) +
geom_spatvector(fill = NA) +
scale_fill_whitebox_c(
palette = "deep", direction = -1,
labels = scales::label_number(suffix = "º"),
n.breaks = 5
) +
theme_minimal() +
coord_sf(crs = 25830) +
labs(
fill = "variation",
title = "Variation of temperature in Castille and Leon (Spain)",
subtitle = "Average temperatures in June vs. May"
)
```
**tidyterra** also provide a geom for plotting RGB `SpatRaster` tiles with
**ggplot2**
```{r example-tile}
rgb_tile <- rast(system.file("extdata/cyl_tile.tif", package = "tidyterra"))
plot <- ggplot(prov) +
geom_spatraster_rgb(data = rgb_tile) +
geom_spatvector(fill = NA) +
theme_light()
plot
# Recognizes coord_sf()
plot +
# Change crs and datum (for relabeling graticules)
coord_sf(crs = 3857, datum = 3857)
```
**tidyterra** provides specific scales for plotting hypsometric maps with
**ggplot2**:
```{r hypso, fig.asp=0.65}
asia <- rast(system.file("extdata/asia.tif", package = "tidyterra"))
terra::plot(asia)
ggplot() +
geom_spatraster(data = asia) +
scale_fill_hypso_tint_c(
palette = "gmt_globe",
labels = scales::label_number(),
# Further refinements
breaks = c(-10000, -5000, 0, 2000, 5000, 8000),
guide = guide_colorbar(reverse = TRUE)
) +
labs(
fill = "elevation (m)",
title = "Hypsometric map of Asia"
) +
theme(
legend.position = "bottom",
legend.title.position = "top",
legend.key.width = rel(3),
legend.ticks = element_line(colour = "black", linewidth = 0.3),
legend.direction = "horizontal"
)
```
### `SpatVectors`
This is a basic example which shows you how to manipulate and plot `SpatVector`
objects:
```{r spatvec }
vect(system.file("ex/lux.shp", package = "terra")) %>%
mutate(pop_dens = POP / AREA) %>%
glimpse() %>%
autoplot(aes(fill = pop_dens)) +
scale_fill_whitebox_c(palette = "pi_y_g") +
labs(
fill = "population per km2",
title = "Population density of Luxembourg",
subtitle = "By canton"
)
```
## I need your feedback
Please leave your feedback or open an issue on
<https://github.com/dieghernan/tidyterra/issues>.
## Need help?
Check our [FAQs](https://dieghernan.github.io/tidyterra/articles/faqs.html) or
open a new [issue](https://github.com/dieghernan/tidyterra/issues)!
You can also ask in [Stack Overflow](https://stackoverflow.com/) using the tag
[[tidyterra]](https://stackoverflow.com/questions/tagged/tidyterra).
## Acknowledgement
**tidyterra** **ggplot2** geoms are based on
[**ggspatial**](https://github.com/paleolimbot/ggspatial) implementation, by
[Dewey Dunnington](https://github.com/paleolimbot) and [**ggspatial**
contributors](https://github.com/paleolimbot/ggspatial/graphs/contributors).