[R] Get contour of a map

Duncan Murdoch murdoch.duncan at gmail.com
Mon May 23 21:00:09 CEST 2011


On 11-05-23 5:55 AM, Pierre Bruyer wrote:
> Hello everybody,
>
> I search a function which returns the contour of map with levels like contourLines, but I would like this function return the border of the map too, because the function contourLines cannot consider the corner of the map and it is not adapted to fill polygon after that.


I think writing that would be hard, but you can fake it.  Find the range 
of z first to work out the contour levels you want, then put very large 
values around the outside of the z matrix.  (You can't use Inf, but big 
finite values will work.)  For example:

x <- 1:10
y <- 1:20
z <- outer(x, y, function(x,y) sin(x)+cos(x*y/10))

range(z) # about -2 to 2, so 10000 is much bigger

z <- rbind(10000, z, 10000)
z <- cbind(10000, z, 10000)

x <- c(0.99, x, 10.01)
y <- c(0.99, y, 20.01)

Now contour plots of x,y,z with contour levels in the range of -2 to 2 
will show closed contours, and contourLines() will return paths around 
them.  They aren't exactly right (because 10000 is not infinity, and 
0.01 is not zero), but should be close enough for graphics purposes.

You'll have to write your own function to handle the general case, but 
it shouldn't be too hard.

Duncan Murdoch



More information about the R-help mailing list