How to determine minimum marine distances in R

2 min readNov 25, 2019

Unlike measuring pairwise terrestrial distances, marine distances need to account for landmasses, which cannot be crossed. Marine distances are a crucial predictor in genetics to build isolation by distance models. These measure gene frequencies variation under increasing geographic distances.

When marine distances are plotted against pairwise genetic differentation levels, a linear relationship is expected. Here I provide a straightforward R script to determine minimum marine distances.

How it works?

A high-resolution polygon representing global landmasses is converted into an infinite resistance surface. Minimum distances between sites are computed with a shortest path algorithm considering the (infinite) resistance of landmasses and null resistance throughout the marine surface.

The outcome of the following code is a matrix of pairwise distances, a figure to visualize if sites are well represented in the surface area and a figure depicting an example of a minimum marine distance.


  1. R installation (available at The Comprehensive R Archive Network).
  2. High resolution polygon depicting the surface of the world (e.g., Global Self-consistent Hierarchical High-resolution Shorelines;
## Source the main function source("")## Read the landmass polygon
global.polygon <- "Global_CostLine_HD_Polygon.shp"
## Run the function
contour( global.polygon = global.polygon , file= "Data/sampling_sites.txt" , file.sep = ";" , file.dec = "." , file.strucutre = 1 , file.header = FALSE , resolution = 0.01 , buffer = c(1,1,1,1) , export.file = TRUE )
## file : the main file with the locations; should be text delimited
## global.polygon: the path of the polygon
## file.strucutre: the main file structure: 1 to “Name Lon Lat” or 2 to “Name Lat Lon”
## file.header: define if the text file has a header with the column names (TRUE or FALSE)
## resolution: the resolution of the study area and the buffer to use around the sites.
## buffer: the buffer can be a simple value or a vector such as c(xmin,xmax,ymin,ymax).
## export.file: file to export the results as a text delimited file (TRUE or FALSE)

Main citation

Assis, J., Castilho Coelho, N., Alberto, F., Valero, M., Raimondi, P., Reed, D., Serrão, E. A. (2013). High and Distinct Range-Edge Genetic Diversity despite Local Bottlenecks. PLoS ONE, 8(7), e68646.




Hi!! I’m Jorge Assis, a Data Scientist, Marine Ecologist, Climate Change Analyst, R and Python Developer based in Portugal []