Skip to content
Prev 659 / 29559 Next

adding contours to a projected map

On Tue, 8 Nov 2005, Jeanne Thibeault wrote:

            
contour() is a base graphics generic function, and has no idea where to 
plot, so plots in the input coordinate system xlim = c(-10,50), ylim = 
c(35,60). When map() projects, the output coordinate system is changed:
+ ylim = c(35,60), projection = "bonne", par = 47, plot=FALSE)
[1] -0.3668910  0.3308747
[1] -1.1721820 -0.6617041

so contour() misses the output plot region (y overshoots).

With a little work, you can use contourLines():

res <- contourLines(Sum.80.83.li)
contours_x <- unlist(sapply(res, function(x) c(x$x, NA)))
contours_y <- unlist(sapply(res, function(x) c(x$y, NA)))
contours <- list(x=contours_x, y=contours_y)

gets you contours you can give as an argument to mapproject(), but you'll 
need to handle label positions and values yourself based on res - look at 
str(res[[1]]) to get a view of what the raw contours look like.

plot(mapproject(contours, ...), type="l")

does the plotting.