2012-04-03 205 views
3

我有这样的数据。有什么方法可以平滑我的情节?如何平滑曲线

cr <- colorRampPalette(col=c("red", "red", "red", "red"), bias=1) 
linecols <- cr(3) 
x<-c(-1000.000000,-900.000000,-800.000000,-700.000000,-600.000000,-500.000000,-400.000000,-300.000000,-200.000000,-100.000000,0.000000,100.000000,200.000000,300.000000,400.000000,500.000000,600.000000,700.000000,800.000000,900.000000,1000.000000) 
y<-c(0.809524,1.000000,1.333333,1.333333,3.285714,7.761905,13.619048,7.571429,14.809524,3.904762,1.857143,2.285714,4.857143,8.571429,2.000000,1.523810,2.714286,0.857143,1.285714,0.857143,1.380952) 
plot(x, y,type="l",main="Average",ylab="Average Profile",col=linecols[1],ylim=c(0.809524,14.809524),xaxt="s",yaxt="s",lwd=2) 

回答

9
lines(x, smooth(y)) 

?smooth

lines(supsmu(x, y)) 

请参阅'?supsmu'。

小心,平滑是恶魔的业务。

+0

看来Excel的平滑是ggplot或R平滑不同的微调。 Excel不会更改任何形状,但是R/ggplot可以。 – bogu 2012-04-09 03:33:23

+2

Excel使用什么算法?你可能会为此做什么真的没有尽头。你应该提供一个例子,如果你认为这是一个问题。 – mdsumner 2012-04-09 04:40:21

6

我会约平滑(互联网搜索的‘坏平滑数据’返回大量网页的),但我会提供了另一种解决方案第二@mdsumner的叮咛:

plot(lowess(x,y,f=1/3),type="l",col="red") 

更多见?lowess信息。

enter image description here

3

许多平整器是可用的。

这里是一个平滑函数:

trace.smooth<-function(trace, type="Savitsky-Golay", width=10){ 

    if(type=="lowess"){ 
    smooth.trace<-with(clean.trace, lowess(x=1:length(trace), 
            y=trace, 
            f=width/length(trace), 
            delta=width/2))$y 
    } 

    if(type=="moving-average"){ 
     moving_average<-function(width=10){ 
     moving.average<-rep(1,width)/width 
     return(moving.average) 
     } 

     moving.average<-moving_average(width) 

     smooth.trace<-filter(trace, moving.average) 

    } 

    if(type=="Savitsky-Golay"){ 
     # Savitsky-Golay smoothing function 
    savistsky_golay<-function(width=10){ 
     x<-1:width-width/2 
     y<-max(x^2)-x^2 
     sg<-y/sum(y) 
     return(sg) 
    } 

    sg<-savistsky_golay(width) 

     smooth.trace<-filter(trace, sg) 
    } 

    return(smooth.trace) 
} 

使用ggplot2

library(ggplot2) 

df<-data.frame(x=x, y=y) 

qplot(data=df, 
     x=x, 
     y=y, 
     geom=c("line", "point"))+ 
     geom_smooth(se=F) 

plot with smooth

可以将方法参数添加到geom_smooth的溶液(方法= “黄土”)

方法:smo无法使用的方法(功能),例如。 LM,GLM,GAM,黄土,RLM

您可以使用stat_smooth

+0

此代码无法运行。 clean.trace没有定义 – Paul 2015-04-24 16:29:41