Hi all, I'm trying to draw a contour plot with rounded (smoothed) contour lines instead of sharp angles; when the grid consists of only a few points in x- and y- axis, the resulting contour is in facts rather ugly since very sharp angles may appear. I did not find any way to do it, by using either "contour" or "contourplot" (from the lattice package), I wonder if there exist a way for smoothing the angles, apart from artificially increasing the grid resolution, Thanks in advance Andrea
Here is one possible approach to get you started (this is not a final answer): x <- seq(-3,3) y <- seq(-3,3) z <- outer(x,y, function(x,y,...) x^2 + y^2 ) tmp <- contourLines(x,y,z) contour(x,y,z, lty=0) lapply(tmp, function(l) { x <- l$x y <- l$y if( length(x) > 2 ){ if( isTRUE( all.equal( c(x[1],y[1]), c(x[length(x)],y[length(y)])))) { xspline(x[-1],y[-1], -1, FALSE) } else { xspline(x, y, -1, TRUE) } } else { lines(x,y) # or whatever else should go here } } ) You can play with the settings to xspline to control the properties of the curves, also it will look better if you thin some of the points from contourLines (the points that are nearly identical cause the small loops). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.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 Andrea Storto > Sent: Tuesday, January 06, 2009 9:52 AM > To: r-help at r-project.org > Subject: [R] smoothed contour lines > > Hi all, > > I'm trying to draw a contour plot > with rounded (smoothed) contour lines instead of sharp angles; > when the grid consists of only a few points > in x- and y- axis, the resulting contour > is in facts rather ugly since very sharp angles may appear. > > I did not find any way to do it, > by using either "contour" or "contourplot" (from the lattice package), > I wonder if there exist a way for smoothing the angles, > apart from artificially increasing the grid resolution, > > Thanks in advance > > Andrea > > ______________________________________________ > 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.
> Greg Snow Greg.Snow at imail.org > Wed Jan 7 17:58:07 CET 2009 > > Here is one possible approach to get you started (this is not a finalanswer):> > x <- seq(-3,3) > y <- seq(-3,3) > > z <- outer(x,y, function(x,y,...) x^2 + y^2 ) > > tmp <- contourLines(x,y,z) > > contour(x,y,z, lty=0) > lapply(tmp, function(l) { > x <- l$x > y <- l$y > if( length(x) > 2 ){ > if( isTRUE( all.equal( c(x[1],y[1]),c(x[length(x)],y[length(y)])))) {> xspline(x[-1],y[-1], -1, FALSE) > } else { > xspline(x, y, -1, TRUE) > } > } else { > lines(x,y) # or whatever else should go here > } > } ) > > >You can play with the settings to xspline to control the properties >of the curves, also it will look better if you thin some of the points > from contourLines (the points that are nearly identical cause thesmall loops).> > Hope this helps, > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow at imail.org > 801.408.8111It often works better to fit a smooth surface to the data, evaluate that surface on a finer grid, and pass the result to contour. This ensures that contour lines don't cross one another and tends to avoid the spurious loops that you might get from smoothing the contour lines themselves. Thin plate splines (Tps from library("fields")) and loess (among others) can fit the surface. E.g., with your data try library("fields") xy<-as.matrix(expand.grid(x=x,y=y)) contour(predict.surface(Tps(as.matrix(expand.grid(x=x,y=y)),as.vector(z) ))) These surface fitters have arguments to control the smoothness/flatness of the surface. Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Andrea Storto > Sent: Tuesday, January 06, 2009 9:52 AM > To: r-help at r-project.org > Subject: [R] smoothed contour lines > > Hi all, > > I'm trying to draw a contour plot > with rounded (smoothed) contour lines instead of sharp angles; > when the grid consists of only a few points > in x- and y- axis, the resulting contour > is in facts rather ugly since very sharp angles may appear. > > I did not find any way to do it, > by using either "contour" or "contourplot" (from the lattice package), > I wonder if there exist a way for smoothing the angles, > apart from artificially increasing the grid resolution, > > Thanks in advance > > Andrea
On Tue, Jan 6, 2009 at 8:52 AM, Andrea Storto <andrea.storto at met.no> wrote:> Hi all, > > I'm trying to draw a contour plot > with rounded (smoothed) contour lines instead of sharp angles; > when the grid consists of only a few points > in x- and y- axis, the resulting contour > is in facts rather ugly since very sharp angles may appear. > > I did not find any way to do it, > by using either "contour" or "contourplot" (from the lattice package), > I wonder if there exist a way for smoothing the angles, > apart from artificially increasing the grid resolution,?filled.contour is something to try out. -Deepayan