我怎样才能使用spline()与ggplot? [英] How can I use spline() with ggplot?

查看:233
本文介绍了我怎样才能使用spline()与ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用样条曲线(y〜x)来拟合我的数据,但我可以找到的所有例子都使用样条曲线进行平滑处理,例如lm(y〜ns(x),df = _)。



我想特别使用 spline(),因为我使用它来进行分析



有没有简单的方法在ggplot中使用spline()?



我已经考虑过使用

  geom_smooth(aes(x =(spline(y〜x)$ x,y = spline(y〜x)$ y))

但我宁愿不必诉诸此。

谢谢!

解决方案

这就是你想要的吗?

  n < -  10 
d < - data.frame(x = 1:n,y = rnorm(n ))
ggplot(d,aes(x,y))+ geom_point()+
geom_line(data = data.frame(spline(d,n = n * 10)))


I would like to fit my data using spline(y~x) but all of the examples that I can find use a spline with smoothing, e.g. lm(y~ns(x), df=_).

I want to use spline() specifically because I am using this to do the analysis represented by the plot that I am making.

Is there a simple way to use spline() in ggplot?

I have considered the hackish approach of fitting a line using

geom_smooth(aes(x=(spline(y~x)$x, y=spline(y~x)$y))

but I would prefer not to have to resort to this.

Thanks!

解决方案

is this what you want?

n <- 10
d <- data.frame(x = 1:n, y = rnorm(n))
ggplot(d,aes(x,y)) + geom_point() + 
  geom_line(data=data.frame(spline(d, n=n*10)))

这篇关于我怎样才能使用spline()与ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆