【问题标题】:Interpolating a path/curve within R在 R 内插值路径/曲线
【发布时间】:2012-07-06 14:31:33
【问题描述】:

在 R 中,我想插入一条具有恒定距离的任意路径 插值点之间。

测试数据如下所示:

require("rgdal", quietly = TRUE)
require("ggplot2", quietly = TRUE)
r <- readOGR(".", "line", verbose = FALSE)
coords <- as.data.frame(r@lines[[1]]@Lines[[1]]@coords)
names(coords) <- c("x", "y")
print(coords)

x         y
-0.44409  0.551159
-1.06217  0.563326
-1.09867  0.310255
-1.09623 -0.273754
-0.67283 -0.392990
-0.03772 -0.273754
 0.63633 -0.015817
 0.86506  0.473291
 1.31037  0.998899
 1.43934  0.933198
 1.46854  0.461124
 1.39311  0.006083
 1.40284 -0.278621
 1.54397 -0.271321

p.orig <- ggplot(coords, aes(x = x, y = y)) + geom_path(colour = "red") + 
    geom_point(colour = "yellow")
print(p.orig)

我尝试了不同的方法,没有一个是真正令人满意的:

  • aspline (akima-package)
  • approx
  • bezierCurve
  • 使用 tourr-package 我无法开始

aspline

来自 akima-package 的aspline 在处理任意路径时会做一些奇怪的事情:

plotInt <- function(coords) print(p.orig + geom_path(aes(x = x, y = y), 
    data = coords) + geom_point(aes(x = x, y = y), data = coords))
N <- 50        # 50 points to interpolate

require("akima", quietly = TRUE)
xy.int.ak <- as.data.frame(with(coords, aspline(x = x, y = y, n = N)))
plotInt(xy.int.ak)

approx

xy.int.ax <- as.data.frame(with(coords, list(x = approx(x, n = N)$y, 
    y = approx(y, n = N)$y)))
plotInt(xy.int.ax)

乍一看,approx 看起来还不错;然而,用真实数据测试它给了我 插值点之间的距离问题。平滑的三次插值也是一件好事。

bezier

另一种方法是使用bezier-curves;我使用了以下 implementation

source("bez.R")
xy.int.bz <- as.data.frame(with(coords, bezierCurve(x, y, N)))
plotInt(xy.int.bz)

【问题讨论】:

    标签: r interpolation curve


    【解决方案1】:

    使用与approx 相同的方法的常规样条线怎么样?这适用于更大的数据吗?

    xy.int.sp <- as.data.frame(with(coords, list(x = spline(x)$y, 
                                                 y = spline(y)$y)))
    

    【讨论】:

    • 谢谢@sebastian-c,这基本上就是我想要的。现在我需要找到一些方法来过滤非常接近的点并将点添加到长段,谢谢。
    • 这最终是为了什么? R 中有一些用于跟踪数据的工具,看起来您可能会从中受益
    • @mdsummer,我必须插入一条河流
    【解决方案2】:

    考虑使用xsplinegrid.xspline(第一个用于基本图形,第二个用于网格):

    plot(x,y, type='b', col='red')
    xspline(x,y, shape=1)
    

    你可以调整 shape 参数来改变曲线,这个例子只是绘制 x 样条,但你也可以让函数返回一组你自己绘制的 xy 坐标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多