【问题标题】:How can i plot an fda object using ggplot2?如何使用 ggplot2 绘制 fda 对象?
【发布时间】:2020-12-23 23:07:36
【问题描述】:

使用 fda 包我创建了名为“curve”的 fd 对象:

splinebasis = create.bspline.basis(rangeval = c(0,100), 
                                     nbasis = 23,         
                                     norder = 4) 
curve = smooth.basis(x, y, splinebasis)$fd

此时我可以通过命令轻松绘制我的 fd 对象:

plot(curve)

获得良好的结果。

我想做的是使用 ggplot2 包绘制对象,但不幸的是我不知道如何编写 ggplot2 s.t.它使用基和系数返回连续曲线*。

  • 我实际上是使用 eval.fd 完成的,但我希望使用 ggplot 绘制实际的 B 样条函数,而不是一些新生成的离散点。

【问题讨论】:

  • 您的代码不可重现:Error in smooth.basis(x, y, splinebasis) : object 'y' not found.

标签: r ggplot2 plot bspline


【解决方案1】:

这是使用fda 包中的predict 的简单解决方案。

library(fda)
set.seed(1)
x <- 0:100
y <- cumsum(rnorm(101))

splinebasis <- create.bspline.basis(rangeval = c(0,100), 
                                     nbasis = 23,         
                                     norder = 4) 
curve <- smooth.basis(x, y, splinebasis)

# Plot using base graphic engine
plot(curve$fd)

# Plot using ggplot2
library(ggplot2)   
xx <- seq(0,100,0.1)
df <- data.frame(x=xx, yhat = predict(curve, newdata=xx))
ggplot(data=df, aes(x=x, y=yhat)) +
  geom_line() +
  geom_hline(aes(yintercept=0), linetype=2) +
  labs(x="time", y="value") +
  theme_bw()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-13
    • 2020-08-02
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多