Skip to content

jamesgrecian/mapr

Repository files navigation

Project Status: Active – The project has reached a stable, usable state and is being actively developed.

mapr

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

Installation

To download the current development version from GitHub:

# install.packages("devtools")  
devtools::install_github("jamesgrecian/mapr")

An example map

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)

An example INLA mesh

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)

About

Manipulating and Projecting Maps in R

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages