Greetings R Community.
I have a profiles of oxygen from an ice covered lake from fall, winter, and spring. I would like to make a 2-D contour plot with interpolated values between those that I have measured. I followed the excellent tutorial/blog posted by @paleolimbot, but my data are imperfect. First, since there is ice cover growth, the profiles do not always start at the surface (0m), so interpolating between depths needs to be done in a staggered way to account for the loss of samples in the upper water column.
library(tidyverse)
library(lubridate)
library(readxl)
truelove<-read_excel("truelove.xlsx")
print(truelove)
A tibble: 15 x 3
date depth oxygen_mL
1 1961-09-10 00:00:00 0 9.75
2 1961-09-10 00:00:00 2 9.69
3 1961-09-10 00:00:00 4 9.63
4 1961-09-10 00:00:00 6 9.56
5 1961-10-25 00:00:00 1 10.8
6 1961-10-25 00:00:00 2 10.5
7 1961-10-25 00:00:00 4 9.67
8 1961-10-25 00:00:00 6 6.62
9 1961-11-21 00:00:00 1 10.3
10 1961-11-21 00:00:00 2 10.0
11 1961-11-21 00:00:00 4 8.36
12 1961-11-21 00:00:00 6 6.39
13 1962-01-10 00:00:00 2 9.58
14 1962-01-10 00:00:00 4 8.31
15 1962-01-10 00:00:00 6 5.09
estimate_oxy_by_date <- function(target_date, target_depth) {
data_for_date <- truelove %>%
filter(date == target_date) %>%
arrange(depth)
approx(data_for_date$depth, data_for_date$oxygen_mL, xout = target_depth)$y
}
estimate_oxy_by_date(ymd("1962-05-07"), c(0, 2, 4, 5, 5.5, 6)) here the interpolation with depth works at each date samples in a profile were taken.
oxy_interp_depth<-crossing(tibble(date = unique(truelove$date)),
tibble(depth = seq(0, 6, length.out = 24))) %>%
group_by(date) %>%
mutate(oxygen_mL = estimate_oxy_by_date(date[1], depth)) #this also worked and I was able to plot interpolated profiles of oxygen
Now trying to interpolate between dates sampled
estimate_oxy_by_depth <- function(target_depth, target_date) {
data_for_depth <- oxy_interp_depth %>%
filter(depth == target_depth) %>%
arrange(date)
approx(data_for_depth$date, data_for_depth$oxygen_mL, xout = target_date)$y
}
estimate_oxy_by_date(target_depth = 4, target_date = seq(ymd("1961-10-25"), ymd("1962-11-21"), by = 1))
oxy_raster <- crossing(tibble(date = seq(ymd("1961-09-10"), ymd("1962-05-07"), by=2)),
tibble(depth = unique(oxy_interp_depth$depth))) %>%
group_by(depth) %>%
mutate(oxygen_mL = estimate_oxy_by_depth(depth[1], date))
This crossing() gave me the error:
Error in approx(data_for_depth$date, data_for_depth$oxygen_mL, xout = target_date) :
need at least two non-NA values to interpolate
Called from: approx(data_for_depth$date, data_for_depth$oxygen_mL, xout = target_date)First time posting in an R help, so please let me know what other information is helpful for you all.
I think this has to do with the fact that the profiles start at different depths through time. Just an idea though. Any help is greatly appreciated!
Thanks!