Skip to content

An R package for visualizing spatial regression methods with ggplot2

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

jamesotto852/ggspatreg

Repository files navigation

ggspatreg

R-CMD-check CRAN_Status_Badge Lifecycle:Experimental

ggspatreg extends 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:

if (!requireNamespace("remotes")) install.packages("remotes")
remotes::install_github("jamesotto852/ggspatreg")

Simple Examples

First, we simulate some spatially correlated data with geoR::grf():

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.

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():

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:

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.

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:

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.

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 and visualize our sample points:

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:

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()

About

An R package for visualizing spatial regression methods with ggplot2

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages