-
Notifications
You must be signed in to change notification settings - Fork 14
/
README.Rmd
88 lines (68 loc) · 2.98 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dpi = 150
)
```
# isoband <img width="120" alt="" align="right" src="man/figures/logo.png">
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/isoband)](https://CRAN.R-project.org/package=isoband)
[![R-CMD-check](https://github.com/r-lib/isoband/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/isoband/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/isoband/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/isoband?branch=main)
<!-- badges: end -->
Generate contour lines (isolines) and contour polygons (isobands) from regularly spaced grids containing elevation data. Package originally written by Claus Wilke and donated to r-lib in 2022.
## Installation
Install the latest official release from CRAN via:
``` r
install.packages("isoband")
```
Install the current development from github via:
``` r
# install.packages("pak")
pak::pak("r-lib/isoband")
```
## Examples
The two main workhorses of the package are the functions `isolines()` and `isobands()`, respectively. They return a list of isolines/isobands for each isolevel specified. Each isoline/isoband consists of vectors of x and y coordinates, as well as a vector of ids specifying which sets of coordinates should be connected. This format can be handed directly to `grid.polyline()`/`grid.path()` for drawing. However, we can also convert the output to spatial features and draw with ggplot2 (see below).
```{r basic-example}
library(isoband)
m <- matrix(c(0, 0, 0, 0, 0,
0, 1, 2, 1, 0,
0, 1, 2, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0), 5, 5, byrow = TRUE)
isolines(1:ncol(m), 1:nrow(m), m, 0.5)
isobands(1:ncol(m), 1:nrow(m), m, 0.5, 1.5)
```
The function `plot_iso()` is a convenience function for debugging and testing.
```{r basic-example-plot, fig.asp = 1, out.width = "50%"}
plot_iso(m, 0.5, 1.5)
```
The isolining and isobanding algorithms have no problem with larger datasets. Let’s calculate isolines and isobands for the volcano dataset, convert to sf, and plot with ggplot2.
```{r volcano, fig.asp = 1, out.width = "50%"}
library(ggplot2)
suppressWarnings(library(sf))
m <- volcano
b <- isobands((1:ncol(m))/(ncol(m)+1), (nrow(m):1)/(nrow(m)+1), m, 10*(9:19), 10*(10:20))
l <- isolines((1:ncol(m))/(ncol(m)+1), (nrow(m):1)/(nrow(m)+1), m, 10*(10:19))
bands <- iso_to_sfg(b)
data_bands <- st_sf(
level = 1:length(bands),
geometry = st_sfc(bands)
)
lines <- iso_to_sfg(l)
data_lines <- st_sf(
level = 2:(length(lines)+1),
geometry = st_sfc(lines)
)
ggplot() +
geom_sf(data = data_bands, aes(fill = level), color = NA, alpha = 0.7) +
geom_sf(data = data_lines, color = "black") +
scale_fill_viridis_c(guide = "none") +
coord_sf(expand = FALSE)
```