I'm trying to get rid of the margins of maps generated using the 'maps' package in R. I get some of the way there by setting par(mar=c(0,0,0,0))
and using the option border=0 in the map()
function. But compared to e.g. a scatterplot with mar=c(0,0,0,0)
there's still a lot of extra space. Here's some code to generate an example map, as well as a regular scatterplot 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 with margins notionally suppressed, space has been reserved for the axes.
Any ideas much appreciated, this has been annoying me for ages.
You can adjust the size of the margins by specifying a margin parameter using the syntax par(mar = c(bottom, left, top, right)) , where the arguments bottom , left … are the size of the margins. The default value for mar is c(5.1, 4.1, 4.1, 2.1).
Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.
In the map function, mar is reset again (and hence does not follow the general settings). You can set the margins within 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 a sidenote, if you change the general settings of par, you can do something like
oldpar <- par(mar=rep(0,4)
... some plotting ...
par(oldpar)
to set the parameters back to the original. This is especially useful if you write your own custom plot functions.
The map()
function does set the margins, so specifying them when you call the function will get the margins changed. However, mar
is ignored if you pass in a projection as well.
So if you do something like
map("county", fill = TRUE, resolution = 0, lty = 0,
projection = "polyconic",
myborder = 0, mar = c(0,0,0,0))
the margins will not change.
In order to correct this, you have 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 change so that any future call of maps()
with a projection
will still allow specification of mar
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With