I'd like calculate (not plot) 2d spline paths in R. There's an old question on that topic that suggests xspline()
: Calculate a 2D spline curve in R
xspline()
somewhat works for my purpose, but has important limitations:
- I cannot customize the number of interpolation points
- I need to call
plot.new()
, even if I don't want it to draw anything - I only have a single parameter (shape) to customize the spline; I'd like to be able to try a few more different types, if possible
Reproducible example:
library(ggplot2)
# control points
x <- c(.1, .5, .7, .8)
y <- c(.9, .6, .5, .1)
plot.new() # necessary for xspline(); would be great if it could be avoided
# how do I set the number of interpolation points?
# how do I modify the exact path (beyond shape parameter)?
path <- xspline(x, y, shape = 1, draw = FALSE)
# plot path (black) and control points (blue) with ggplot
ggplot(data = NULL, aes(x, y)) +
geom_point(data = as.data.frame(path), size = 0.5) +
geom_point(data = data.frame(x, y), size = 2, color = "blue")
Created on 2021-08-14 by the reprex package (v2.0.0)
Are there any easily available alternatives to xspline()
?