Plot a world map with a specified grid with R
-
26-12-2019 - |
Question
I am trying to use wrld_simpl from maptools package in order to plot a piece of world map with a longitude/latitude grid.
For example, I have a netCDF file with longitude and latitude, I create a matrix with all the points I can have on the grid:
lat <- get.var.ncdf(nc,"lat")
long <- get.var.ncdf(nc,"lon")
pts <- SpatialPoints(expand.grid(long,lat), CRS(proj4string(wrld_simpl)))
Now I want to plot the countries whose coordinates are on my grid.
I don't know how to do with wrld_simpl attributes!
Solution
Did you look at maps
mapproject
packages ? You can draw any coast line very quickly with the desired resolution and it comes with projections and grid. You can apply projections to one of your object (expand.grid(long,lat)
). Example in Antartica:
library("mapproj")
m <- map( "world", "Antarctica", plot=FALSE)
map("world", "Antarctica", proj="conic", param=-90, fill=TRUE, col="gray")
map.grid(m, nx=4, ny=6, col="black")
front.lim <- data.frame(x=seq(-180, 180, length.out=1000),
y=rep(-62, 1000))
front.lim <- mapproject(front.lim$x, front.lim$y, projection="conic", parameters=-90)
lines(front.lim$x, front.lim$y, col="red", lwd=2)
If you have already plotted the ncdf matrix, you can simply add map(add=TRUE)
. It will read the x and y limits and draw the borders in this area.
plot(c(-10, 45), c(30, 60), type="n", xlab="", ylab="") # image(ncdf)
map(add=TRUE, fill=TRUE, col="gray")
abline(v=seq(-10, 45, 7.5), lty=2)
abline(h=seq(-30, 60, 7.5), lty=2)