插值R中的路径/曲线

时间:2012-07-06 06:32:26

标签: r interpolation curve

在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)

http://i.imgur.com/0tusA.png

我尝试了不同的方法,但没有一种方法令人满意:

  • aspline(akima-package)
  • approx
  • bezierCurve
  • 使用tourr - 包我无法上手

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)

http://i.imgur.com/GVoES.png

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)

http://i.imgur.com/pqvTy.png

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

bezier

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

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

http://i.imgur.com/aCFPG.png

2 个答案:

答案 0 :(得分:8)

常规样条曲线如何使用与approx相同的方法?这会对更大的数据起作用吗?

Spline graph

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

答案 1 :(得分:6)

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

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

enter image description here

您可以调整形状参数来更改曲线,此示例仅绘制x样条曲线,但您也可以让函数返回一组您自己绘制的xy坐标。

相关问题