R Resources‎ > ‎

Creating a simple map

I was excited to figure out how to make nice, simple maps to show my study sites in R, so I will share it here. This uses a package called 'maps'. Here's a link to the documentation.

library(maps) #We use the package called 'maps'
#first create a set of coordinates for the points you want to plot.
coords=data.frame(c(-134.5,-122,-148.9),c(58,50,61))
colnames(coords)=c('long','lat')

#We then generate a map of two regions (Alaska and Canada) from the database called 'world'. There are a couple of other databases for mainland US and other world regions. I'm using the options here to cut off the Eastern part of the map (using xlim=...) and filling in the map with a light gray color.

map(database="world",regions=c("USA:alaska","canada"),xlim=c(-170,-90),col=gray(0.9), fill=TRUE)
map.axes()
#we now simply add 'points' to this map using the coordinates. Notice that longitude is the first element because measured on the xaxis, followed by the latitude. The rest are standard options for points(). Finally, we add dotted gray grid lines
points(coords$long, coords$lat, pch="*",cex=3,col="red")
abline(h=seq(50,80,10),col=gray(0.5),lty=2)
abline(v=seq(-160,-100,20),col=gray(0.5),lty=2)

... and there it is!

Comments