Drawing borderless maps in R

I am trying to get rid of the map fields generated by the 'maps' package in R. I get some of them by setting par(mar=c(0,0,0,0)) and using the border = 0 parameter in the map() function , But compared with, for example, the scattering diagram with mar=c(0,0,0,0) is still a lot of extra space. Here is some code to create an example map, as well as a regular scatter plot for comparison.

 library(maps) x <- sample(360, 10)-180 y <- sample(160, 10)-80 x.boundary <- c(-180, 180, 0, 0) y.boundary <- c(0, 0, -80, 80) pdf("map.tmp.pdf", width=9, height=4) par(mar=rep(0,4)) map("world", border=0, ylim=c(-80, 80), fill=TRUE, bg="gray", col="white") points(x, y, pch=19, col="blue") points(x.boundary, y.boundary, pch=19, col="red") # map.axes() dev.off() pdf("scatter.tmp.pdf", width=9, height=4) par(mar=rep(0,4)) plot(x, y, xlim=c(-180, 180), ylim=c(-80, 80), pch=19, col="blue") points(x.boundary, y.boundary, pch=19, col="red") dev.off() 

If you uncomment the map.axes() function, you can see that even if marginal values ​​were suppressed conditionally, space is reserved for the axes.

Any ideas that are highly valued have annoyed me for ages.

+7
source share
2 answers

In the display function, mar again reset (and therefore does not match the general settings). You can set the fields in the map function (see ?map ). This gives what you want:

 map("world", border=0, ylim=c(-80, 80), fill=TRUE, bg="gray", col="white",mar=rep(0,4)) 

on the side, if you change the general settings of the par parameter, you can do something like

 oldpar <- par(mar=rep(0,4) ... some plotting ... par(oldpar) 

to return the settings to the original. This is especially useful if you are writing your own custom chart functions.

+9
source

The map() function sets the fields, so specifying them when calling the function will cause the fields to change. However, mar ignored if you also pass projection.

So, if you do something like

 map("county", fill = TRUE, resolution = 0, lty = 0, projection = "polyconic", myborder = 0, mar = c(0,0,0,0)) 

fields will not change.

To fix this, you need to change the maps() function by adding one line of code.

Old function:

 ... if (coordtype != "spherical" || doproj) { plot.window(xrange, yrange, asp = 1/aspect[1]) } else { ... 

Change to:

 ... if (coordtype != "spherical" || doproj) { par(mar = mar) plot.window(xrange, yrange, asp = 1/aspect[1]) } else { ... 

I downloaded the source from CRAN and rebuilt the library for myself using RStudio after I made the changes so that any future call to maps() with projection would still allow the mar specification.

+2
source

All Articles