mapr is an R package that makes it easier to make maps in R
Given a set of locations, for example from a tagged marine animal, the mapr
function will load a global shapefile from the Natural Earth database using rworldmap
and manipulate it for plotting using ggplot2
and sf
Alternatively, given the same set of locations the meshr
function will help you to create a shapefile (or nested list of shapefiles) that can be used as a boundary when creating an INLA
mesh using inla.mesh.2d
To download the current development version from GitHub:
# install.packages("devtools")
devtools::install_github("jamesgrecian/mapr")
Here's an example of how to generate a map containing a coastline and some animal locations using the mapr
function alongside the sf
and ggplot2
libraries
# load libraries
require(tidyverse)
require(sf)
require(mapr)
# load example dataset
data(ellie)
# define an appropriate proj.4 projection
prj <- '+proj=laea +lat_0=-60 +lon_0=70 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
# use mapr to generate a shapefile
world_shp <- mapr(ellie, prj, buff = 1e6)
# output a plot using ggplot
p1 <- ggplot() +
geom_sf(aes(), data = world_shp) +
geom_sf(aes(), data = st_as_sf(ellie, coords = c('lon', 'lat'))
%>% st_set_crs('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'))
print(p1)
When using INLA to analyse animal movement data it is useful to base the mesh on the distribution of locations.
Here's an example of how to generate a boundary shapefile using meshr
that can then be passed to inla.mesh.2d
, we can plot the mesh using inlabru
, sf
and ggplot2
# load libraries
require(tidyverse)
require(sf)
require(mapr)
require(INLA)
require(inlabru)
# load example dataset
data(ellie)
# define an appropriate proj.4 projection
prj <- '+proj=laea +lat_0=-60 +lon_0=70 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
# use meshr to generate the boundary
b <- meshr(ellie, prj, buff = 5e5, keep = 0.05, Neumann = T)
# use the boundary to generate the INLA mesh and pass the same projection
mesh = inla.mesh.2d(boundary = b,
max.edge = c(250000, 1e+06),
cutoff = 25000,
max.n = 1000,
crs = CRS(prj))
# output a plot using ggplot
p2 <- ggplot() +
geom_sf(aes(), data = mapr(ellie, prj, buff = 1e6)) +
inlabru::gg(mesh) +
geom_sf(aes(), data = st_as_sf(ellie, coords = c("lon", "lat"))
%>% st_set_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
print(p2)