Is this the way you did it? It seems pretty intuitive...
a=pi/180
x=1:360
plot(x,sin(a * x))
plot(x,a * cos(a * x))
plot(x,-a^2 * sin(a * x))
Is this the way you did it? It seems pretty intuitive...
a=pi/180
x=1:360
plot(x,sin(a * x))
plot(x,a * cos(a * x))
plot(x,-a^2 * sin(a * x))
My main problem was the derivative, not so much the plotting (or maybe it was 'plotting an arbitrary function'); but I looked it up and it seems I slightly misremembered what it was I wanted to do. I wanted to draw a cubic spline, not a sine. What I ended up doing was
spline_x <- 1:6
spline_y <- c(0, 0.5, 2, 2, 0.5, 0)
spl_fun <- splinefun(spline_x, spline_y)
p <- ggplot(data.frame(x=spline_x, y=spline_y), aes(x, y))
p <- p + stat_function(fun = spl_fun)
p <- p + stat_function(fun = spl_fun, arg = list(deriv = 2))
I still don't quite understand how that derivative works - ?list doesn't mention anything about 'deriv', and there's a function called 'deriv' but I'm not sure how that's being interpreted in the code above.Also it seems recent versions of ggplot2 have geom_xspline() which does what I need (I'm told) but that wasn't in the release version when I was doing it.
(FWIW the reason that there's no native support in ggplot2 for this sort of smoothing is that I think it's a really bad idea as it tends to distort the underlying data)
plot(sin) plot(D(sin))
(Programming from memory on my phone so might be slightly off)