-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
168 lines (131 loc) · 5.55 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
# cache = TRUE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ggspatreg <img src="man/figures/logo.png" align="right" width="120" style="padding-left:10px;background-color:white;" />
<!-- badges: start -->
[![R-CMD-check](https://github.com/jamesotto852/ggspatreg/workflows/R-CMD-check/badge.svg)](https://github.com/jamesotto852/ggspatreg/actions)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version-ago/ggspatreg)](https://cran.r-project.org/package=ggspatreg)
[![Lifecycle:Experimental](https://img.shields.io/badge/Lifecycle-Experimental-339999)](<Redirect-URL>)
<!-- badges: end -->
**ggspatreg** extends [**ggplot2**](https://github.com/tidyverse/ggplot2), allowing for simple visualization of spatial regression models.
Included are functions `geom_krige()`, `geom_krige_contour()`, and `geom_krige_contour_lines()`
for plotting interpolation of spatial data.
## Installation
**ggspatreg** is being actively developed and is likely to undergo large changes in the future.
It is not available on CRAN, but you can install the development version from [GitHub](https://github.com/):
``` r
if (!requireNamespace("remotes")) install.packages("remotes")
remotes::install_github("jamesotto852/ggspatreg")
```
## Simple Examples
First, we simulate some spatially correlated data with `geoR::grf()`:
```{r sim_data, results = FALSE, warning = FALSE, message = FALSE}
library("tidyverse"); theme_set(theme_bw())
library("ggspatreg")
set.seed(1)
grid <- expand_grid(x = 0:100, y = 0:100)
df <- slice_sample(grid, n = 600)
df$z <- geoR::grf(grid = df, cov.model = "exp", cov.pars = c(5, 30), nugget = 1)$data
ggplot(df, aes(x, y, fill = z)) +
geom_point(shape = 21, size = 3) +
scale_fill_viridis_c() +
coord_fixed()
```
Now, we can use `geom_krige()` to plot the resulting interpolated kriging surface.
By default, an exponential covariance model is used.
This can be changed via the `model` argument.
```{r geom_krige, results = FALSE}
ggplot(df, aes(x, y)) +
geom_krige(aes(z = z)) +
geom_point(aes(fill = z), shape = 21, size = 3) +
scale_fill_viridis_c() +
coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = FALSE)
```
Also included are `geom_krige_contour()` and `geom_krige_contour_lines()`:
```{r geom_krige_contour, results = FALSE, fig.show = "hold", out.width = "50%"}
ggplot(df, aes(x, y, z = z)) +
geom_krige_contour(bins = 6, show.legend = FALSE) +
scale_fill_viridis_d() +
coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = FALSE)
ggplot(df, aes(x, y, z = z)) +
geom_krige_contour_lines(bins = 6, show.legend = FALSE) +
scale_color_viridis_d() +
coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = FALSE)
```
***
In **ggspatreg**, it is also possible to model spatially correlated data with trends.
Let's add a simple linear trend to the previously simulated data:
```{r sim_data_trend}
df <- mutate(df, z = z + 3/20 * x - 1/20 * y)
ggplot(df, aes(x, y, fill = z)) +
geom_point(shape = 21, size = 3) +
scale_fill_viridis_c() +
coord_fixed()
```
When `formula` is provided, `geom_krige()` models both the spatial dependence and the specified linear relationship.
```{r geom_krige_trend, results = FALSE}
ggplot(df, aes(x, y)) +
geom_krige(aes(z = z), formula = z ~ x + y) +
geom_point(aes(fill = z), shape = 21, size = 3) +
coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = FALSE) +
scale_fill_viridis_c()
```
Again, we can do the same thing with contours:
```{r geom_krige_contour_trend, results = FALSE, fig.show = "hold", out.width = "50%"}
ggplot(df, aes(x, y, z = z)) +
geom_krige_contour(bins = 8, formula = z ~ x + y, show.legend = FALSE) +
scale_fill_viridis_d() +
coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = FALSE)
ggplot(df, aes(x, y, z = z)) +
geom_krige_contour_lines(bins = 8, formula = z ~ x + y, show.legend = FALSE) +
scale_color_viridis_d() +
coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = FALSE)
```
## Latitude/Longitude Data
**ggspatreg** works well with latitude longitude data.
Below, we simulate spatial data for coordinates in Waco, TX.
```{r simulating_Waco, results = FALSE}
set.seed(1)
df <- tibble(
lon = runif(250, -97.3, -97),
lat = runif(250, 31.4, 31.7),
z = geoR::grf(grid = cbind(lon, lat), cov.model = "exp", cov.pars = c(3, .1), nugget = 1)$data
)
```
Below, we set up [**ggmap**](https://github.com/dkahle/ggmap) and visualize our sample points:
```{r ggmap_setup, message = FALSE, results = FALSE, dpi = 300}
library("ggmap")
theme_update(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()
)
waco_map <- get_map(location = c(-97.3, 31.4, -97, 31.7), color = "bw")
ggmap(waco_map) +
geom_point(data = df, aes(lon, lat, fill = z), shape = 21, size = 3) +
guides(color = guide_legend(), size = guide_legend()) +
scale_fill_viridis_c()
```
It is simple to include kriging layers on top of ggmap raster images, we need only adjust `alpha`:
```{r ggmap_krige_1, results = FALSE, warning = FALSE, dpi = 300}
ggmap(waco_map) +
geom_krige(data = df, aes(lon, lat, z = z), alpha = .4) +
geom_point(data = df, aes(lon, lat, fill = z), shape = 21, size = 3) +
scale_fill_viridis_c()
ggmap(waco_map) +
geom_krige_contour(data = df, aes(lon, lat, z = z), alpha = .4, bins = 7) +
scale_fill_viridis_d()
ggmap(waco_map) +
geom_krige_contour_lines(data = df, aes(lon, lat, z = z), bins = 7) +
scale_fill_viridis_d()
```