View source: R/catSpatInterp.R
catSpatInterp | R Documentation |
Create a raster of probability of categorical values interpolated across a 2-dimensional space given a set of points where each is assigned to one of several classes.
catSpatInterp(
data,
x.col = "x",
y.col = "y",
group.col = "group",
num.grid = 100,
knn = 10,
hull.buffer = 0.1,
num.cores = 1,
num.batches = NULL
)
data |
matrix or data.frame containing points and grouping designation. |
x.col, y.col, group.col |
numbers or characters identifying which columns
in |
num.grid |
number of grid cells for k-nearest neighbor interpolation. |
knn |
number of nearest neighbors to consider for interpolation. |
hull.buffer |
percent increase of convex hull to use as spatial area to interpolate over. |
num.cores |
number of cores to distribute interpolations over. |
num.batches |
number of batches to divide grid cell interpolations into. |
A list containing a raster and points of buffered convex hull.
Eric Archer eric.archer@noaa.gov
Adapted from code originally presented in a blog post on Categorical Spatial Interpolation by Timo Grossenbacher https://timogrossenbacher.ch/2018/03/categorical-spatial-interpolation-with-r/
## Not run:
iris.mds <- stats::cmdscale(dist(iris[, 1:4]), k = 2)
mds.df <- setNames(
cbind(iris.mds, data.frame(iris$Species)),
c("dim1", "dim2", "Species")
)
result <- catSpatInterp(
mds.df, x.col = "dim1", y.col = "dim2", group.col = "Species",
num.grid = 300, knn = 20, hull.buffer = 0.05,
num.cores = 5, num.batches = NULL
)
library(ggplot2)
ggplot(mapping = aes(dim1, dim2)) +
geom_raster(
aes(fill = Species, alpha = prob),
data = result$raster
) +
geom_polygon(data = result$hull.poly, fill = NA, col = "black") +
geom_hline(yintercept = 0, col = "white") +
geom_vline(xintercept = 0, col = "white") +
geom_point(
aes(fill = Species),
data = mds.df,
col = "black",
shape = 21,
size = 4
) +
theme(
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
legend.position = "top",
panel.grid = element_blank(),
panel.background = element_blank()
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.