[R-sig-Geo] adding contours to a projected map

Roger Bivand Roger.Bivand at nhh.no
Wed Nov 9 09:39:34 CET 2005


On Tue, 8 Nov 2005, Jeanne Thibeault wrote:

> Hello,
> 
> I'm having a problem adding contours to a projected map. I was able to do it
> before I specified a projection. This is the code:
> 
> > Sum.80.83.li <-
> interp(SummerO18.80.84.df$longitude,SummerO18.80.84.df$latitude,
> + SummerO18.80.84.df$wt18o)
> > eurM <- map(database = "world", interior = FALSE, xlim = c(-10,50),
> + ylim = c(35,60), plot = FALSE)
> > map("world", interior = FALSE, xlim = c(-10,50), ylim = c(35,60),
> + projection = "bonne", par = 47, col = "slateblue1")
> > map.grid(eurM, labels = FALSE, col = "lightgray", lty = 2, nx = 2, ny = 2)
> > contour(Sum.80.83.li, add = TRUE)
> 
> Everything was coming out nicely until I tried to add the contours. No
> contours printed on the map. Any ideas on what is going wrong?

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:

> library(maps)
> eurobonne <- map("world", interior = FALSE, xlim = c(-10,50), 
+ ylim = c(35,60), projection = "bonne", par = 47, plot=FALSE)
> range(eurobonne$x, na.rm=TRUE)
[1] -0.3668910  0.3308747
> range(eurobonne$y, na.rm=TRUE)
[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.

> 
> Thanks,
> Jeanne
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list