4 ms·
There are many things wrong with R but basic plotting functions are one of its strengths. Is this the way you did it? It seems pretty intuitive... a=pi/180 x
by ldp01 10y ago
There are many things wrong with R but basic plotting functions are one of its strengths.
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))
- hadley 10y agoOr plot(sin) plot(D(sin)) (Programming from memory on my phone so might be slightly off)
- roel_v 10y agoBut that just draws 3 separate plots. 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.
- hadley 10y agoIt took me 5 minutes to figure out how that actually did work! That is rather esoteric code! (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)