KatyB KatyB - 3 months ago 11
R Question

include scale and coordinates in R map

I am drawing a map in R with the following code:

library(maps)
library(mapdata)

map('worldHires',c('UK'),
xlim=c(-10,-1),
ylim=c(56,59.5))
points(-5.65,56.7233,col=2,pch=18)


resulting in

enter image description here

How can I add a scale to the map as well as a box surrounding it with the latitude and longitude shown? Something similar to:

enter image description here

Answer

The maps package has also a function map.axes and map.scales.

library(maps)
library(mapdata)

map('worldHires',c('UK'), xlim=c(-10,-1),
    ylim=c(56,59.5))

points(-5.65,56.7233,col=2,pch=18)
map.axes()
map.scale()

gives you

enter image description here

Edit 1

If you look at the code of map.axes you see that it simply calling axis and box. One way would be to do that manually and adjust the labels.

library(maps)
library(mapdata)

m <- map('worldHires',c('UK'), xlim=c(-10,-1),
         ylim=c(56,59.5), mar=c(4, 4, 4, 4))

points(-5.65,56.7233,col=2,pch=18)

xat <- pretty(m$range[1:2])
xlab <- paste0(xat, " E")

yat <- pretty(m$range[3:4])
ylab <- paste0(yat, " N")

box()
axis(1, at=xat, labels=xlab)
axis(2, las=TRUE, at=yat, labels=ylab)
axis(3, at=xat, labels=xlab)
axis(4, las=TRUE, at=yat, labels=ylab)
map.scale(y=59)

This would give you the same picture with adjusted labels enter image description here

Edit 2:

Or use the raster::degreeLabels* function as suggested by @Pascal.

library(maps)
library(mapdata)
library(raster)

m <- map('worldHires',c('UK'), xlim=c(-10,-1),
         ylim=c(56,59.5), mar=c(4, 4, 4, 4))

points(-5.65,56.7233,col=2,pch=18)

xat <- pretty(m$range[1:2])
xlab <- parse(text=degreeLabelsEW(xat))

yat <- pretty(m$range[3:4])
ylab <- parse(text=degreeLabelsNS(yat))


box()
axis(1, at=xat, labels=xlab)
axis(2, las=TRUE, at=yat, labels=ylab)
axis(3, at=xat, labels=xlab)
axis(4, las=TRUE, at=yat, labels=ylab)

map.scale(y=59)

enter image description here