Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing maps without margins in R

Tags:

r

maps

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.

like image 427
Michael Dunn Avatar asked Mar 02 '11 14:03

Michael Dunn


People also ask

How do I reduce margins in R plot?

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).

How do I increase plotting region in R?

Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.


2 Answers

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.

like image 139
Joris Meys Avatar answered Sep 20 '22 13:09

Joris Meys


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.

like image 37
josiekre Avatar answered Sep 20 '22 13:09

josiekre