Monday, 15 August 2016

Create an inset map in R


I need to create a map of my sampling site, and the journal requested to make an inset map showing where in the world this map is, this is my map code.


library(maps)
library(GISTools)

map('state', fill = FALSE, xlim = c(-125, -114), ylim = c(32.2, 42.5), xlab = "lon", ylab = "lat")

map.axes(cex.axis=0.8)

points(-121.6945, 39.36708, bg = "black", pch = 21)

maps::map.scale(x=-124, y=34, ratio=FALSE, relwidth=0.3)
north.arrow(xb=-116, yb=41, len=0.22, lab="N")

I have tried to find a way to make the inset map but no luck so far.



Answer



You can use 'usr' argument inside par() to modify coordinates limits and add a small map. I tried to add a world map, but there is some bug inside maps package, limits aren't cropped by xlim and ylim when inmap is added.



map('state', fill = FALSE, xlim = c(-125, -114), ylim = c(32.2, 42.5), xlab = "lon", ylab = "lat")
map.axes(cex.axis=0.8)

points(-121.6945, 39.36708, bg = "black", pch = 21)

maps::map.scale(x=-124, y=34, ratio=FALSE, relwidth=0.3)
north.arrow(xb=-116, yb=41, len=0.22, lab="N")

# Inmap
par(usr=c(-216, -63, 22, 144))

rect(xleft =-126.2,ybottom = 23.8,xright = -65.5,ytop = 50.6,col = "white")
map("usa", xlim=c(-126.2,-65.5), ylim=c(23.8,50.6),add=T)
map("state", xlim=c(-126.2,-65.5), ylim=c(23.8,50.6),add=T, boundary = F, interior = T, lty=2)
map("state", region="california", fill=T, add=T)
points(-121.6945, 39.36708, bg = "white", pch = 21)

plot


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...