Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created November 7, 2023 20:03
Show Gist options
  • Save alexpghayes/980fad6f30d1d32c346acc981a61196e to your computer and use it in GitHub Desktop.
Save alexpghayes/980fad6f30d1d32c346acc981a61196e to your computer and use it in GitHub Desktop.
library(ggplot2)
data(mcycle, package = "MASS")
f_approxfun <- approxfun(mcycle$times, mcycle$accel)
f_splinefun <- splinefun(mcycle$times, mcycle$accel)
mcycle |>
ggplot(aes(times, accel)) +
geom_point() +
# doesn't work since approxfun() doesn't support derivatives out of the bag,
# could numDeriv probably
geom_function(aes(color = "approxfun"), fun = f_approxfun, args = list(deriv = 1)) +
geom_function(aes(color = "splinefun"), fun = f_splinefun, args = list(deriv = 1)) +
scale_color_brewer(palette = "Dark2") +
theme_bw()
# see https://www.andrewheiss.com/blog/2018/02/15/derivatives-r-fun/ for more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment