r - 在 R 中插入路径/曲线

标签 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 -包我无法开始
  • asplineaspline akima-package 在处理任意路径时会做一些奇怪的事情:
    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

    最佳答案

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

    Spline graph

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

    关于r - 在 R 中插入路径/曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11356997/

    相关文章:

    swift - iOS 用其他颜色填充 UIBezierPath 的一半而不使用 CAGradientLayer

    r - 迭代更新字符串匹配模式

    r - 如何根据 list() 中的相应值替换 dataframe() 中的值?

    r - 在数据框中交换行和排序

    python - 神经网络在简单的线性插值任务中表现不佳

    graphics - 从 R 中的一组点在 map 上绘制平滑区域

    r - ipython notebook R cell magic作为python函数

    c++ - 使用 C++ 和 RGBA 像素 vector 的双线性调整大小

    Matlab:求微分方程给出的 y 值的根

    Javascript - 绘制曲线矩形的函数