[R] Smooth line in graph
Greg Snow
Greg.Snow at intermountainmail.org
Thu Sep 20 18:11:07 CEST 2007
I don't know what SigmaPlot and Excel are doing for you, but I would guess that they are not doing cubic splines (as a general rule, when R and Excel differ, it is safest to assume that R is not the one doing something wrong)
Often differences between packages are due to differences in assumptions or model specifications. I don't know SigmaPlot to be able to say what it is doing and a quick search of the excel help for 'spline' was not enlightening.
Perhaps what you are looking for is not a cubic spline. Another option is an xspline. These are implemented in the grid package. Try:
library(grid)
library(lattice)
x<-c(-45,67,131,259,347)
y <- c(0.31, 0.45, 0.84, 0.43, 0.25)
s <- seq(-1,1, .25)
tmp.df <- data.frame(x=rep(x,9), y=rep(y,9), s=rep(s, each=5))
xyplot(y~x|factor(s), data=tmp.df, tmp.s=tmp.df$s, panel=function(x,y,subscripts,tmp.s,...){
grid.points(x,y)
tmp2.s <- tmp.s[subscripts]
print(tmp2.s)
grid.xspline(x,y, default.units='native', shape=tmp2.s)
})
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
> -----Original Message-----
> From: Nestor Fernandez [mailto:nestor at ual.es]
> Sent: Thursday, September 20, 2007 4:02 AM
> To: r-help at stat.math.ethz.ch
> Cc: Greg Snow
> Subject: Re: [R] Smooth line in graph
>
> Sorry, I answered too quickly.
> It worked with the "simplified" example I provided but not
> with non-regular intervals in x:
>
> x<-c(-45,67,131,259,347)
> y <- c(0.31, 0.45, 0.84, 0.43, 0.25)
>
> plot(x,y)
> lines(spline(x,y, method='n', n=250))
> #or:
> lines(predict(interpSpline(x, y)))
>
> Produce the same decrease between first two points and the
> shape is quite different to that produced by sigmaplot -I
> need comparable figures. Changing "method" and "n" arguments
> did not help.
>
> Sorry for bothering. Any other suggestion?
>
>
>
>
>
>
> Greg Snow escribió:
> > Try:
> >
> >
> >> lines(spline(x,y, method='n', n=250))
> >>
> >
> >
>
>
More information about the R-help
mailing list