Created
November 7, 2023 20:03
-
-
Save alexpghayes/980fad6f30d1d32c346acc981a61196e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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