Description Usage Arguments Details Examples
View source: R/interpolation.R
Inverse distance squared interpolation
1 | interpolate_IDS(precip_df, out_xy, elev_gradient = 0)
|
precip_df |
Must contain precip, x, y columns (elev optional) |
out_xy |
Coordinates (x, y) at which to interpolate (elev optional) |
elev_gradient |
Linear elevation gradient (see details) |
For elevation gradient, elev_gradient
should be specified in units
of precipitation over elevation (i.e., if precipitation is in mm and
elevation in m, elev_gradient
should be [mm/m]). Additionally,
precip_df
and out_xy
must contain a column named elev
.
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 | # Generate topography with sin functions
get_elev <- function(x, y) 500*sin(x / 750) + 500*sin(y / 1000) + 1000
# Generate random precipitation observations
set.seed(100)
N_obs<- 10
precip_df <- data.frame(precip = rnorm(N_obs, 750, 100),
x = runif(N_obs, 0, 5000),
y = runif(N_obs, 0, 5000))
precip_df$elev <- get_elev(precip_df$x, precip_df$y)
out_xy <- expand.grid(x = seq(0, 5000, length.out = 9),
y = seq(0, 5000, length.out = 9))
out_xy$elev <- get_elev(out_xy$x, out_xy$y)
# Plot elevation
library(ggplot2)
ggplot() +
geom_raster(data = out_xy, aes(x, y, fill = elev)) + coord_equal()
# Interpolate precipitation
out_xy$precip_interp1 <- interpolate_IDS(precip_df, out_xy, elev_gradient = 0)
out_xy$precip_interp2 <- interpolate_IDS(precip_df, out_xy, elev_gradient = 0.1)
# Plot the data
ggplot() +
geom_raster(data = out_xy, aes(x, y, fill = precip_interp2)) +
geom_point(data = precip_df, aes(x, y, fill = precip), shape = 21, size = 3) +
scale_fill_viridis_c() +
coord_equal()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.