interpol: Interpolate values to a grid using linear interpolation

Description Usage Arguments Details Value Functions Examples

View source: R/interpol.R

Description

These functions are both a thin wrappers around the approx() function from Base R's stats package. The primary difference is that these functions output a tibble instead of list with x and y components. The tibble / data frame output simplifies use in some work flows.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
interpol(.x, .y, .grid, .name = "ID", grow_grid = TRUE, ...)

interpol_res(
  .x,
  .y,
  .res,
  .name = "ID",
  grow_grid = TRUE,
  with_zero = TRUE,
  ...
)

Arguments

.x

Vector of (observed) x, or independent values

.y

Vector of (observed) y, or dependent values

.grid

Vector of locations for calculated interpolated values.

.name

An identifier added to the output dataframe, often useful when this function is used in loops or with map() , lapply() or their cousins.

grow_grid

Should the output grid be extended to whole integer values just above and just below the observed data (interpol_res)? Should interpolated values outside the source data be NA, or estimated as the value at the closest data extreme (interpol())? Useful for aligning data with whole number dates, depths, etc, which can make interpolated graphics look a bit better.

...

Further parameters to send to approx(). na.rm = TRUE is perhaps the most likely.

.res

Single numeric value representing output resolution in units compatible with the .x variable.

with_zero

Should the grid be expanded to include a value of zero? Useful for interpolation along depths, in the common situation where the shallowest observation is slightly below the surface, but a nice graphic should start at depth == 0.

Details

The functions uses simple linear interpolation to estimate values at (generally unmeasured) locations between observed points.

The parameter names .y, .x, are mnemonics, to remind you that the function will estimate the dependent variable .y at evenly spaced intervals values along the .x direction, based on the (.x, .y) pairs provided.

Ideas for this function and its use in profile graphics were inspired by Dewey Dunnington's fishandwhistle.net blog.

Value

A data frame with components id (arbitrary identifier, often useful when output from these functions are fed directly into loops or used in functional programming.), ind (independent variable), and dep (dependent variable).

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
data(dep_sonde)
dat <- dep_sonde[dep_sonde$month == 'Jul',]
grid <- seq(0, 14, 1)
interp <- interpol(dat$depth, dat$temp, .grid = grid)

ggplot(dat, aes(temp, depth)) +
  geom_point(size = 3, col = 'red') +
  geom_point(mapping = aes(dep, ind), data = interp, shape = 3) +
  scale_y_reverse()
data(dep_sonde)
dat <- dep_sonde[dep_sonde$month == 'Jul',]
interp <- interpol_res(dat$depth, dat$temp, .res = 0.5)

ggplot(dat, aes(temp, depth)) +
  geom_point(size = 3, col = 'red') +
  geom_point(mapping = aes(dep, ind), data = interp, shape = 3) +
  scale_y_reverse()

ccb60/tdggraph documentation built on Dec. 19, 2021, 1:58 p.m.