Hi, I?m trying to get smooth curves connecting points in a plot using "spline" but I don?t get what I whant. Eg.: x<-1:5 y <- c(0.31, 0.45, 0.84, 0.43, 0.25) plot(x,y) lines(spline(x,y)) Creates a valley between the first and second points, then peaks at 3rd, and another valley between 4th and 5th. I?m trying to get a consistently growing curve up to the 3rth point and then a decrease like with SigmaPlot spline curves or with Excel. I tried with different spline arguments and also lowess and loess, with no success. Any ideas? Thanks.
how about: require(splines) x<-1:5 y <- c(0.31, 0.45, 0.84, 0.43, 0.25) yy <-predict(interpSpline(x, y)) plot(x, y) lines(yy) ---- Katharine Mullen mail: Department of Physics and Astronomy, Faculty of Sciences Vrije Universiteit Amsterdam, de Boelelaan 1081 1081 HV Amsterdam, The Netherlands room: T.1.06 tel: +31 205987870 fax: +31 205987992 e-mail: kate at nat.vu.nl homepage: http://www.nat.vu.nl/~kate/ On Wed, 19 Sep 2007, Nestor Fernandez wrote:> Hi, > > I?m trying to get smooth curves connecting points in a plot using > "spline" but I don?t get what I whant. > > Eg.: > x<-1:5 > y <- c(0.31, 0.45, 0.84, 0.43, 0.25) > plot(x,y) > lines(spline(x,y)) > > Creates a valley between the first and second points, then peaks at 3rd, > and another valley between 4th and 5th. I?m trying to get a consistently > growing curve up to the 3rth point and then a decrease like with > SigmaPlot spline curves or with Excel. > > I tried with different spline arguments and also lowess and loess, with > no success. Any ideas? > > Thanks. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Try:> lines(spline(x,y, method='n', n=250))-- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Nestor Fernandez > Sent: Wednesday, September 19, 2007 2:09 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Smooth line in graph > > Hi, > > I'm trying to get smooth curves connecting points in a plot > using "spline" but I don't get what I whant. > > Eg.: > x<-1:5 > y <- c(0.31, 0.45, 0.84, 0.43, 0.25) > plot(x,y) > lines(spline(x,y)) > > Creates a valley between the first and second points, then > peaks at 3rd, and another valley between 4th and 5th. I'm > trying to get a consistently growing curve up to the 3rth > point and then a decrease like with SigmaPlot spline curves > or with Excel. > > I tried with different spline arguments and also lowess and > loess, with no success. Any ideas? > > Thanks. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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)) > >> > > > > > >