Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Last active February 6, 2023 09:37
Show Gist options
  • Save TonyLadson/223cb62d06063bf13bc7262e53787654 to your computer and use it in GitHub Desktop.
Save TonyLadson/223cb62d06063bf13bc7262e53787654 to your computer and use it in GitHub Desktop.
R code for the blog
# Figure 1
# DO - date of sample
WQ %>%
ggplot(aes(x = date, y = do.mg.l)) + geom_point() +
labs(y = 'DO (mg/L)', x = 'Date') +
theme_grey(base_size = 16)
# Figure 2
# DO - temperature
WQ %>%
ggplot(aes(x = temp.c, y = do.mg.l)) + geom_point() +
labs(y = 'DO (mg/L)', x = quote('Temperature ' ^o * C)) +
theme_grey(base_size = 18)
# Figure 3
# Saturated DO as a function of temperature
# The Calc_DOsat100 function is available here
source('https://gist.github.com/TonyLadson/0c7e24d110da3bf3df7d5bf62304a5df')
ggplot(data.frame(x = c(0, 40)), aes(x)) +
stat_function(fun = Calc_DOsat100, geom = "line") +
theme_grey(base_size = 18) +
labs(x = quote('Temperature ' ^o * C), y = 'Saturated DO (mg/L)')
# Figure 4
# DO-temp with % saturation lines
# Suspect points highlighted
WQ %>%
ggplot(aes(x = temp.c, y = do.mg.l)) + geom_point() +
labs(y = 'DO (mg/L)', x = quote('Temperature ' ^o * C)) +
stat_function(fun = Calc_DOsat100, geom = "line", colour = 'blue', linetype = 'dashed') +
stat_function(fun = function(x) 2* Calc_DOsat100(x), geom = "line", colour = 'blue', linetype = 'dashed') +
stat_function(fun = function(x) 3* Calc_DOsat100(x), geom = "line", colour = 'blue', linetype = 'dashed') +
annotate(geom = 'text', x = 35, y = 5, label = '100%', colour = 'blue', hjust = 1) +
annotate(geom = 'text', x = 35, y = 12, label = '200%', colour = 'blue', hjust = 1) +
annotate(geom = 'text', x = 35, y = 19, label = '300%', colour = 'blue', hjust = 1) +
annotate(geom = 'text', x = 35, y = 28, label = '% saturation', colour = 'blue', hjust = 1) +
annotate(geom = 'rect', xmin = 6, xmax = 16, ymin = 75, ymax = 115, alpha = 0.0, colour = 'red') +
theme_grey(base_size = 18)
# Convert from percent saturation to concentration
# This
WQ %>%
filter(do.mg.l > 60) %>% # for testing, delete for deployment
select(do.mg.l, temp.c) %>% # for testing, delete for deployment
mutate(do.mg.l_fixed = if_else(do.mg.l > 60, do.mg.l*Calc_DOsat100(temp.c)/100, do.mg.l )) # convert from % saturation to concentration mg/L
# # A tibble: 13 x 3
# do.mg.l temp.c do.mg.l_fixed
# <dbl> <dbl> <dbl>
# 1 92.0 10.50 10.263729
# 2 94.0 10.90 10.389630
# 3 91.0 10.60 10.128491
# 4 98.0 11.40 10.707364
# 5 99.0 11.30 10.841546
# 6 86.0 9.00 9.941410
# 7 77.0 8.60 8.987252
# 8 105.0 11.45 11.458999
# 9 102.0 11.60 11.093355
# 10 96.0 10.60 10.685001
# 11 98.3 11.14 10.804696
# 12 111.4 14.80 11.282098
# 13 87.1 15.10 8.764121
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment