[R] smoothed contour lines

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 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.




More information about the R-help mailing list