Skip to content

smoothed contour lines

3 messages · Andrea Storto, Greg Snow, Deepayan Sarkar

#
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
1 day later
#
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,
#
On Tue, Jan 6, 2009 at 8:52 AM, Andrea Storto <andrea.storto at met.no> wrote:
?filled.contour is something to try out.

-Deepayan