| Title: | Plot Data on Oceanographic Maps using 'ggplot2' |
|---|---|
| Description: | Allows plotting data on bathymetric maps using 'ggplot2'. Plotting oceanographic spatial data is made as simple as feasible, but also flexible for custom modifications. Data that contain geographic information from anywhere around the globe can be plotted on maps generated by the basemap() or qmap() functions using 'ggplot2' layers separated by the '+' operator. The package uses spatial shape- ('sf') and raster ('stars') files, geospatial packages for R to manipulate, and the 'ggplot2' package to plot these files. The package ships with low-resolution spatial data files and higher resolution files for detailed maps are stored in the 'ggOceanMapsLargeData' repository on GitHub and downloaded automatically when needed. |
| Authors: | Mikko Vihtakari [aut, cre] (affiliation: Institute of Marine Research, ORCID: <https://orcid.org/0000-0003-0371-4319>), Roger Bivand [ctb], Hadley Wickham [ctb] |
| Maintainer: | Mikko Vihtakari <[email protected]> |
| License: | GPL-3 |
| Version: | 3.0.0 |
| Built: | 2026-06-22 13:54:52 UTC |
| Source: | https://github.com/mikkovihtakari/ggoceanmaps |
Creates a ggplot2 basemap for further plotting of data.
basemap( x = NULL, limits = NULL, data = NULL, shapefiles = NULL, crs = NULL, bathymetry = FALSE, glaciers = FALSE, rotate = FALSE, legends = TRUE, legend.position = "right", lon.interval = NULL, lat.interval = NULL, bathy.style = NULL, downsample = 0, bathy.border.col = NA, bathy.size = 0.1, bathy.alpha = 1, land.col = "grey60", land.border.col = "black", land.size = 0.1, gla.col = "grey95", gla.border.col = "black", gla.size = 0.1, grid.col = "grey70", grid.size = 0.1, base_size = 11, projection.grid = FALSE, expand.factor = 1, verbose = FALSE )basemap( x = NULL, limits = NULL, data = NULL, shapefiles = NULL, crs = NULL, bathymetry = FALSE, glaciers = FALSE, rotate = FALSE, legends = TRUE, legend.position = "right", lon.interval = NULL, lat.interval = NULL, bathy.style = NULL, downsample = 0, bathy.border.col = NA, bathy.size = 0.1, bathy.alpha = 1, land.col = "grey60", land.border.col = "black", land.size = 0.1, gla.col = "grey95", gla.border.col = "black", gla.size = 0.1, grid.col = "grey70", grid.size = 0.1, base_size = 11, projection.grid = FALSE, expand.factor = 1, verbose = FALSE )
x |
The limit type ( |
limits |
Map limits. One of the following:
Can be omitted if |
data |
A data frame, sp, or sf shape containing longitude and latitude coordinates. If a data frame, the coordinates have to be given in decimal degrees. The limits are extracted from these coordinates and produce a rectangular map. Suited for situations where a certain dataset is plotted on a map. The function attempts to guess the correct columns and it is advised to use intuitive column names for longitude (such as "lon", "long", or "longitude") and latitude ("lat", "latitude") columns. Can be omitted if |
shapefiles |
Either a list containing shapefile information or a character argument referring to a name of pre-made shapefiles in |
crs |
Coordinate reference system (CRS) for the map. If |
bathymetry |
Logical indicating whether bathymetry should be added to the map. Functions together with |
glaciers |
Logical indicating whether glaciers and ice sheets should be added to the map. |
rotate |
Logical indicating whether the projected maps should be rotated to point towards the pole relative to the mid-longitude limit. |
legends |
Logical indicating whether the legend for bathymetry should be shown. |
legend.position |
The position for ggplot2 legend. See the argument with the same name in theme. |
lon.interval, lat.interval
|
Numeric value specifying the interval of longitude and latitude grids. |
bathy.style |
Character (plots bathymetry; list of alternatives in Details) or |
downsample |
Integer defining the downsampling rate for raster bathymetries. A value of 0 (default) does not downsample, 1 skips every second row, 2 every second and third. See |
bathy.alpha |
Transparency parameter for the bathymetry fill color. See scale_alpha. |
land.col, gla.col, grid.col
|
Character code specifying the color of land, glaciers, and grid lines, respectively. Use |
land.border.col, gla.border.col, bathy.border.col
|
Character code specifying the color of the border line for land, glacier, and bathymetry shapes. |
land.size, gla.size, bathy.size, grid.size
|
Numeric value specifying the width of the border line land, glacier and bathymetry shapes as well as the grid lines, respectively. Use the |
base_size |
Base size parameter for ggplot. See ggtheme. |
projection.grid |
Logical indicating whether the coordinate grid should show projected coordinates instead of decimal degree values. Useful to define limits for large maps in polar regions. |
expand.factor |
Expansion factor for map limits. Can be used to zoom in (decrease the value under 1) and out (increase the value over 1) automatically ( |
verbose |
Logical indicating whether information about the projection and guessed column names should be returned as messages. Set to |
The function uses ggplot2, sf, stars and spatial files to plot maps of the world's oceans.
Limits
If the limits are in decimal degrees, the longitude limits ([1:2]) specify the start and end segments of corresponding angular lines that should reside inside the map area. The longitude limits are defined counter-clockwise. The latitude limits [3:4] define the parallels that should reside inside the limited region given the longitude segments. Note that the actual limited region becomes wider than the polygon defined by the coordinates (shown in Examples). Using data to limit the map, making the points barely fit into the map. The expand.factor argument can be used to adjust the space between map borders and points. If the limits are given as projected coordinates or as decimal degrees for maps with -60 < latitude < 60, limit elements represent lines encompassing the map area in cartesian space.
Projections
If the shapefiles are not specified, the function uses either the limits or data arguments to decide which projection to use. Up-to-date conditions are defined in define_shapefiles and shapefile_list functions. At the time of writing, the function uses three different projections (given as EPSG codes)
3995 WGS 84 / Arctic Polar Stereographic. Called "ArcticStereographic". For max latitude (limits[4]) >= 60 (if min latitude (limits[3]) >= 30), and single integer latitudes >= 30 and <= 89.
3031 WGS 84 / Antarctic Polar Stereographic. Called "AntarcticStereographic". For max latitude (limits[4]) <= -60 (if min latitude (limits[3]) <= -30), and single integer latitudes <= -30 and >= -89.
4326 WGS 84 / World Geodetic System 1984, used in GPS. Called "DecimalDegree". For min latitude (limits[3]) < 30 or > -30, max latitude (limits[4]) < 60 or > -60, and single integer latitudes < 30 and > -30.
The rotate argument changes the pre-defined projection such that mid-longitude point in the map points northward.
The crs argument can be used to define the projection, which can be useful when plotting, for instance, model data that are difficult to transform into another projection.
Bathymetry
Bathymetry can be plotted by simply specifying bathymetry = TRUE or bathy.style. The former uses the low-resolution raster shipped with ggOceanMaps; the latter selects one of the styles listed below. See the dedicated bathymetry vignette for a full walk-through with examples.
The bathy.style string follows the pattern geometry_palette, where geometry encodes both the underlying data type (raster grid, polygon contours or contour lines) and where the data come from (shipped, downloaded, user-supplied or web service), and palette is either blues or grays. All styles have a short abbreviation. Substitute g for b at the end of the abbreviation to switch from blues to grays (e.g. rbb → rbg).
| Alias | Abbr. | Geometry | Data source | Needs | Notes |
raster_binned_blues |
rbb |
raster, binned | shipped low-res | nothing | Default. Fast, works offline. |
raster_continuous_blues |
rcb |
raster, continuous | ggOceanMapsLargeData | ggOceanMaps.datapath |
Higher detail. Recommended once cached. |
raster_user_blues |
rub |
raster, continuous | user-supplied raster | ggOceanMaps.userpath |
GEBCO, ETOPO, IBCAO NetCDF / GeoTIFF. |
wcs_emodnet_blues |
wemb |
raster, continuous | EMODnet WCS (European waters) | internet (tiles cached) | ~115 m. DD limits only. See wcs_bathymetry. |
wcs_etopo_blues |
wceb |
raster, continuous | NOAA NCEI ETOPO1 WCS (global) | internet (tiles cached) | ~1.85 km. DD limits only. See wcs_bathymetry. |
poly_blues |
pb |
polygon contours | ggOceanMapsLargeData | ggOceanMaps.datapath |
Pre-2.0 default. Filled depth bands. |
contour_blues |
cb |
contour lines | ggOceanMapsLargeData | ggOceanMaps.datapath |
Lines only, no fill. |
contour_gray |
cg |
contour lines | ggOceanMapsLargeData | ggOceanMaps.datapath |
Gray-only variant (no cb → cg alias needed).
|
Add the suffix _grays (full alias) or change the last letter b → g (abbreviation) to get the greyscale variant of any style with a _blues version, e.g. raster_binned_grays (rbg), wcs_emodnet_grays (wemg), poly_grays (pg).
If bathy.style is NULL (the default), it is read from getOption("ggOceanMaps.bathy.style") and falls back to "raster_binned_blues". Override globally with options(ggOceanMaps.bathy.style = "...") in .Rprofile (e.g. "poly_blues" to mimic pre-2.0 ggOceanMaps).
Pre-made shapefiles
If the limits are not defined as decimal degrees (any longitude outside the range [-180, 180] or latitude [-90, 90]), the function will ask to specify shapefiles. The shapefiles can be defined by partially matching the names of the pre-made shapefiles in shapefile_list (e.g. "Ar" would be enough for "ArcticStereographic") or by specifying custom shapefiles.
Custom shapefiles
Custom shapefiles have to be a named list containing at least the following elements:
land Object name of the spatial polygon containing land. Required.
glacier Object name of the spatial polygon containing glaciers. Not required if glaciers are not needed.
bathy Object name of the spatial polygon or raster containing bathymetry data. Not required if bathymetry is not needed.
See Examples.
Line width and font size
The line size aesthetics in ggplot2 generates approximately 2.13 wider lines measured in pt than the given values. If you want a specific line width in pt, use the internal function LS to convert the desired line width to the ggplot2 equivalent. A similar function is also available for font sizes (FS).
Returns a ggplot map, which can be assigned to an object and modified as any ggplot object.
Mikko Vihtakari
Note that if you use this function to generate maps for a publication, it is advised to cite the underlying data. The spatial data used by this function have been acquired from the following sources:
Land polygons. Natural Earth Data 1:10m Physical Vectors with the Land and Minor Island datasets combined. Distributed under the CC Public Domain license (terms of use).
Glacier polygons. Natural Earth Data 1:10m Physical Vectors with the Glaciated Areas and Antarctic Ice Shelves datasets combined. Distributed under the CC Public Domain license (terms of use)
Bathymetry. NOAA National Centers for Environmental Information. 2022: ETOPO 2022 15 Arc-Second Global Relief Model. NOAA National Centers for Environmental Information. doi:10.25921/fd45-gt74. Distributed under the U.S. Government Work license.
Other basemap functions:
qmap(),
shapefile_list(),
transform_coord()
# The easiest way to produce a map is to use the limits # argument and decimal degrees: basemap(limits = 60) # synonym to basemap(60) # Bathymetry can be added using the respective argument: basemap(limits = -60, bathymetry = TRUE) ## Not run: # Glaciers require a download in the new version: basemap(limits = -60, glaciers = TRUE, shapefiles = "Arctic") ## End(Not run) # The easiest way to add data on the maps is to use the ggspatial functions: dt <- data.frame(lon = c(-150, 150), lat = c(60, 90)) if(requireNamespace("ggspatial", quietly = TRUE)) { basemap(data = dt, bathymetry = TRUE) + ggspatial::geom_spatial_point(data = dt, aes(x = lon, y = lat), color = "red") } ## Not run: # Note that writing out data = dt is required because there are multiple # underlying ggplot layers plotted already: basemap(data = dt) + ggspatial::geom_spatial_point(dt, aes(x = lon, y = lat), color = "red") #> Error: `mapping` must be created by `aes()` ## End(Not run) # If you want to use native ggplot commands, you need to transform your data # to the projection used by the map: dt <- transform_coord(dt, bind = TRUE) basemap(data = dt) + geom_point(data = dt, aes(x = lon.proj, y = lat.proj), color = "red") # The limits argument of length 4 plots a map anywhere in the world: basemap(limits = c(100, 160, -20, 30), bathymetry = TRUE) # The limits are further expanded when using the data argument: dt <- data.frame(lon = c(-160, 160, 160, -160), lat = c(80, 80, 60, 60)) if(requireNamespace("ggspatial", quietly = TRUE)) { basemap(data = dt) + ggspatial::geom_spatial_polygon(data = dt, aes(x = lon, y = lat), fill = NA, color = "red") # Rotate: basemap(data = dt, rotate = TRUE) + ggspatial::geom_spatial_polygon(data = dt, aes(x = lon, y = lat), fill = NA, color = "red") } # Alternative: basemap(data = dt, rotate = TRUE) + geom_polygon(data = transform_coord(dt, rotate = TRUE), aes(x = lon, y = lat), fill = NA, color = "red") ## To find UTM coordinates to limit a polar map: basemap(limits = 60, projection.grid = TRUE) ## Not run: # (Arctic shapes require a download in 2.0) basemap(limits = c(2.5e4, -2.5e6, 2e6, -2.5e5), shapefiles = "Arctic") # Using custom shapefiles (requires download): data(bs_shapes, package = "ggOceanMapsData") basemap(shapefiles = list(land = bs_land))#' # Premade shapefiles from ggOceanMapsLargeData (requires download): basemap("BarentsSea", bathymetry = TRUE) ## End(Not run) # grid.col = NA removes grid lines, rotate = TRUE rotates northwards: basemap(limits = c(-180, -140, 50, 70), grid.col = NA, rotate = TRUE) # Add axis labels basemap(limits = c(-140, -105, 20, 40), bathymetry = TRUE) + labs(y = "Latitude", x = "Longitude") # Remove axis text basemap(limits = c(0, 60, 68, 80), rotate = TRUE) + theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank() )# The easiest way to produce a map is to use the limits # argument and decimal degrees: basemap(limits = 60) # synonym to basemap(60) # Bathymetry can be added using the respective argument: basemap(limits = -60, bathymetry = TRUE) ## Not run: # Glaciers require a download in the new version: basemap(limits = -60, glaciers = TRUE, shapefiles = "Arctic") ## End(Not run) # The easiest way to add data on the maps is to use the ggspatial functions: dt <- data.frame(lon = c(-150, 150), lat = c(60, 90)) if(requireNamespace("ggspatial", quietly = TRUE)) { basemap(data = dt, bathymetry = TRUE) + ggspatial::geom_spatial_point(data = dt, aes(x = lon, y = lat), color = "red") } ## Not run: # Note that writing out data = dt is required because there are multiple # underlying ggplot layers plotted already: basemap(data = dt) + ggspatial::geom_spatial_point(dt, aes(x = lon, y = lat), color = "red") #> Error: `mapping` must be created by `aes()` ## End(Not run) # If you want to use native ggplot commands, you need to transform your data # to the projection used by the map: dt <- transform_coord(dt, bind = TRUE) basemap(data = dt) + geom_point(data = dt, aes(x = lon.proj, y = lat.proj), color = "red") # The limits argument of length 4 plots a map anywhere in the world: basemap(limits = c(100, 160, -20, 30), bathymetry = TRUE) # The limits are further expanded when using the data argument: dt <- data.frame(lon = c(-160, 160, 160, -160), lat = c(80, 80, 60, 60)) if(requireNamespace("ggspatial", quietly = TRUE)) { basemap(data = dt) + ggspatial::geom_spatial_polygon(data = dt, aes(x = lon, y = lat), fill = NA, color = "red") # Rotate: basemap(data = dt, rotate = TRUE) + ggspatial::geom_spatial_polygon(data = dt, aes(x = lon, y = lat), fill = NA, color = "red") } # Alternative: basemap(data = dt, rotate = TRUE) + geom_polygon(data = transform_coord(dt, rotate = TRUE), aes(x = lon, y = lat), fill = NA, color = "red") ## To find UTM coordinates to limit a polar map: basemap(limits = 60, projection.grid = TRUE) ## Not run: # (Arctic shapes require a download in 2.0) basemap(limits = c(2.5e4, -2.5e6, 2e6, -2.5e5), shapefiles = "Arctic") # Using custom shapefiles (requires download): data(bs_shapes, package = "ggOceanMapsData") basemap(shapefiles = list(land = bs_land))#' # Premade shapefiles from ggOceanMapsLargeData (requires download): basemap("BarentsSea", bathymetry = TRUE) ## End(Not run) # grid.col = NA removes grid lines, rotate = TRUE rotates northwards: basemap(limits = c(-180, -140, 50, 70), grid.col = NA, rotate = TRUE) # Add axis labels basemap(limits = c(-140, -105, 20, 40), bathymetry = TRUE) + labs(y = "Latitude", x = "Longitude") # Remove axis text basemap(limits = c(0, 60, 68, 80), rotate = TRUE) + theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank() )
Decimal degree land shapes
dd_landdd_land
Simple feature collection land shapes in decimal degrees (EPSG:4326). Obtained from Natural Earth Data (10m vectors). Includes the islands dataset.
Other mapfiles:
dd_rbathy
Decimal degree bathymetry
dd_rbathydd_rbathy
Raster bathymetry in decimal degrees (EPSG:4326). Downsampled from ETOPO 60 arc-second grid.
NOAA National Centers for Environmental Information. 2022: ETOPO 2022 15 Arc-Second Global Relief Model. NOAA National Centers for Environmental Information. doi:10.25921/fd45-gt74
Other mapfiles:
dd_land
Calculates the closest distance to land for coordinates in a data frame
dist2land( data, lon = NULL, lat = NULL, shapefile = "DecimalDegree", proj.in = 4326, bind = TRUE, dist.col = "ldist", binary = FALSE, verbose = TRUE )dist2land( data, lon = NULL, lat = NULL, shapefile = "DecimalDegree", proj.in = 4326, bind = TRUE, dist.col = "ldist", binary = FALSE, verbose = TRUE )
data |
Data frame or sf object containing geographic coordinates. |
lon, lat
|
Either the names of the longitude and latitude columns in |
shapefile |
Land shape to which distances should be calculated. Either a character argument referring to a name of pre-made shapefiles in |
proj.in |
|
bind |
Logical indicating whether |
dist.col |
The name of the distance column, if |
binary |
Logical indicating whether binary (TRUE = the position is in the ocean, FALSE = the position is on land) should be returned instead of distances. Speeds up the function considerably. |
verbose |
Logical indicating whether information about the process should be returned as messages. Set to |
The function calculates great circle spherical distances using the st_distance function by default. The function can be slow for large datasets. If you only want to use the function to remove (wrong) observations reported on land, set the binary argument to TRUE. This speeds up the calculations by a factor of ten.
Returns a vector if bind = FALSE, otherwise a data frame. The distances are given in a new column defined by the dist.col argument. The distances are kilometers if binary = FALSE, otherwise logical (TRUE = the position is in the ocean, FALSE = the position is on land).
Mikko Vihtakari
# Simple example: dt <- data.frame(lon = seq(-20, 80, length.out = 41), lat = 50:90) dt <- dist2land(dt, verbose = FALSE) qmap(dt, color = ldist) + scale_color_viridis_c() # Datasets covering the entire Earth seem to work now, except 0,0 lon/lat point lon = deg_to_dd(seq(0,360,30)); lat = c(80,50,20,0,-20,-50,-80) dt <- data.frame( lon = rep(lon, length(lat)), lat = rep(lat, each = length(lon))) qmap(dist2land(dt, verbose = FALSE), color = ldist) + scale_color_viridis_c() ## Not run: dt <- data.frame( lon = deg_to_dd(seq(0,360,length.out = 1e3)), lat = rep(60, 1000)) # The distance calculation is slow for large datasets system.time(dist2land(dt)) # user system elapsed # 12.677 0.146 12.849 # binary = TRUE speeds the function up system.time(dist2land(dt, binary = TRUE)) # user system elapsed # 1.239 0.120 1.369 ## End(Not run)# Simple example: dt <- data.frame(lon = seq(-20, 80, length.out = 41), lat = 50:90) dt <- dist2land(dt, verbose = FALSE) qmap(dt, color = ldist) + scale_color_viridis_c() # Datasets covering the entire Earth seem to work now, except 0,0 lon/lat point lon = deg_to_dd(seq(0,360,30)); lat = c(80,50,20,0,-20,-50,-80) dt <- data.frame( lon = rep(lon, length(lat)), lat = rep(lat, each = length(lon))) qmap(dist2land(dt, verbose = FALSE), color = ldist) + scale_color_viridis_c() ## Not run: dt <- data.frame( lon = deg_to_dd(seq(0,360,length.out = 1e3)), lat = rep(60, 1000)) # The distance calculation is slow for large datasets system.time(dist2land(dt)) # user system elapsed # 12.677 0.146 12.849 # binary = TRUE speeds the function up system.time(dist2land(dt, binary = TRUE)) # user system elapsed # 1.239 0.120 1.369 ## End(Not run)
Major fisheries areas (hovedomraade) of Norway
fdir_main_areasfdir_main_areas
sf object containing major fishing zones defined by the Norwegian Directorate of Fisheries. Contains also Northwest Atlantic Fisheries Organization's divisions where Norwegian vessels tend to fish.
Norwegian Directorate of Fisheries and Northwest Atlantic Fisheries Organization
Other datasets:
fdir_sub_areas,
ices_areas
if(requireNamespace("ggspatial")) { basemap(fdir_main_areas) + ggspatial::annotation_spatial(fdir_main_areas, fill = NA) }if(requireNamespace("ggspatial")) { basemap(fdir_main_areas) + ggspatial::annotation_spatial(fdir_main_areas, fill = NA) }
Norwegian sub-areas (lokasjon) for commercial fishing
fdir_sub_areasfdir_sub_areas
sf object containing major fishing zones defined by the Norwegian Directorate of Fisheries.
Norwegian Directorate of Fisheries
Other datasets:
fdir_main_areas,
ices_areas
if(requireNamespace("ggspatial")) { basemap(fdir_sub_areas) + ggspatial::annotation_spatial(fdir_sub_areas, fill = NA) }if(requireNamespace("ggspatial")) { basemap(fdir_sub_areas) + ggspatial::annotation_spatial(fdir_sub_areas, fill = NA) }
Opens and formats Geonorge bathymetry shapefiles ready for plotting in ggOceanMaps
geonorge_bathymetry(filepath, layer = NULL, verbose = FALSE)geonorge_bathymetry(filepath, layer = NULL, verbose = FALSE)
filepath |
Character string defining the path to the |
layer |
Character string defining the layer containing depth information. If |
verbose |
Logical indicating whether information the reading process should be returned. |
You can download the bathymetry polygon shapefiles from Geonorge. Download the file in GLM format.
An sf object containing the depth polygons. Uses same projection than bathy (see CRS).
Mikko Vihtakari
Other create shapefiles:
clip_shapefile(),
raster_bathymetry(),
vector_bathymetry(),
vector_land(),
wcs_bathymetry()
Extracts depth from basemap bathymetry raster dataset for coordinates in a data frame
get_depth( data, bathy.style = "raster_continuous", lon = NULL, lat = NULL, shapefile = "DecimalDegree", proj.in = 4326, bind = TRUE, depth.col = "depth", verbose = FALSE )get_depth( data, bathy.style = "raster_continuous", lon = NULL, lat = NULL, shapefile = "DecimalDegree", proj.in = 4326, bind = TRUE, depth.col = "depth", verbose = FALSE )
data |
Data frame or sf object containing geographic coordinates. |
bathy.style |
Character defining the basemap bathymetry raster which should be used for the depth extraction. Valid alternatives: |
lon, lat
|
Either the names of the longitude and latitude columns in |
shapefile |
Land shape to which distances should be calculated. Either a character argument referring to a name of pre-made shapefiles in |
proj.in |
|
bind |
Logical indicating whether |
depth.col |
The name of the depth column, if |
verbose |
Logical indicating whether information about the process should be returned as messages. Set to |
Uses the st_extract function to extract values from basemap bathymetry raster grids. Does not work for vector bathymetries.
Returns a vector if bind = FALSE, otherwise a data frame. The depths are given in a new column defined by the dist.col argument. The distances are kilometers. NA distance means that the position is on land.
Mikko Vihtakari
## Not run: dt <- data.frame(lon = seq(-20, 80, length.out = 41), lat = 50:90) dt <- get_depth(dt) qmap(dt, color = depth) + scale_color_viridis_c() ## End(Not run)## Not run: dt <- data.frame(lon = seq(-20, 80, length.out = 41), lat = 50:90) dt <- get_depth(dt) qmap(dt, color = depth) + scale_color_viridis_c() ## End(Not run)
ICES Advisory Areas
ices_areasices_areas
sf object containing ICES Advisory Areas.
International Council for the Exploration of the Sea
Other datasets:
fdir_main_areas,
fdir_sub_areas
if(requireNamespace("ggspatial")) { basemap(ices_areas) + ggspatial::annotation_spatial(ices_areas, fill = NA) }if(requireNamespace("ggspatial")) { basemap(ices_areas) + ggspatial::annotation_spatial(ices_areas, fill = NA) }
qmap is a shortcut similar to ggplot2's qplot designed to quickly plot data with a limited range of options.
qmap( data, ..., x = NULL, y = NULL, geom = "point", limits = NULL, shapefiles = NULL, crs = NULL, bathymetry = FALSE, glaciers = FALSE, rotate = FALSE, legends = TRUE, legend.position = "right", lon.interval = NULL, lat.interval = NULL, bathy.style = NULL, downsample = 0, bathy.border.col = NA, bathy.size = 0.1, bathy.alpha = 1, land.col = "grey60", land.border.col = "black", land.size = 0.1, gla.col = "grey95", gla.border.col = "black", gla.size = 0.1, grid.col = "grey70", grid.size = 0.1, base_size = 11, projection.grid = FALSE, expand.factor = 1.1, verbose = FALSE )qmap( data, ..., x = NULL, y = NULL, geom = "point", limits = NULL, shapefiles = NULL, crs = NULL, bathymetry = FALSE, glaciers = FALSE, rotate = FALSE, legends = TRUE, legend.position = "right", lon.interval = NULL, lat.interval = NULL, bathy.style = NULL, downsample = 0, bathy.border.col = NA, bathy.size = 0.1, bathy.alpha = 1, land.col = "grey60", land.border.col = "black", land.size = 0.1, gla.col = "grey95", gla.border.col = "black", gla.size = 0.1, grid.col = "grey70", grid.size = 0.1, base_size = 11, projection.grid = FALSE, expand.factor = 1.1, verbose = FALSE )
data |
Data frame to use. |
x, y, ...
|
Aesthetics passed into each layer. Longitude and latitude columns are automatically recognized using the |
geom |
Character argument specifying geom(s) to draw. Defaults to "point". Other alternatives are "text" and "label". The "text" option can also be triggered by simply mapping a variable to |
limits |
Map limits. One of the following:
Can be omitted if |
shapefiles |
Either a list containing shapefile information or a character argument referring to a name of pre-made shapefiles in |
crs |
Coordinate reference system (CRS) for the map. If |
bathymetry |
Logical indicating whether bathymetry should be added to the map. Functions together with |
glaciers |
Logical indicating whether glaciers and ice sheets should be added to the map. |
rotate |
Logical indicating whether the projected maps should be rotated to point towards the pole relative to the mid-longitude limit. |
legends |
Logical indicating whether the legend for bathymetry should be shown. |
legend.position |
The position for ggplot2 legend. See the argument with the same name in theme. |
lon.interval, lat.interval
|
Numeric value specifying the interval of longitude and latitude grids. |
bathy.style |
Character (plots bathymetry; list of alternatives in Details) or |
downsample |
Integer defining the downsampling rate for raster bathymetries. A value of 0 (default) does not downsample, 1 skips every second row, 2 every second and third. See |
bathy.alpha |
Transparency parameter for the bathymetry fill color. See scale_alpha. |
land.col, gla.col, grid.col
|
Character code specifying the color of land, glaciers, and grid lines, respectively. Use |
land.border.col, gla.border.col, bathy.border.col
|
Character code specifying the color of the border line for land, glacier, and bathymetry shapes. |
land.size, gla.size, bathy.size, grid.size
|
Numeric value specifying the width of the border line land, glacier and bathymetry shapes as well as the grid lines, respectively. Use the |
base_size |
Base size parameter for ggplot. See ggtheme. |
projection.grid |
Logical indicating whether the coordinate grid should show projected coordinates instead of decimal degree values. Useful to define limits for large maps in polar regions. |
expand.factor |
Expansion factor for map limits. Can be used to zoom in (decrease the value under 1) and out (increase the value over 1) automatically ( |
verbose |
Logical indicating whether information about the projection and guessed column names should be returned as messages. Set to |
Returns a ggplot map, which can be assigned to an object and modified as any ggplot object.
Mikko Vihtakari
Other basemap functions:
basemap(),
shapefile_list(),
transform_coord()
dt <- data.frame(lon = c(-100, -80, -60), lat = c(10, 25, 40), var = c("a", "a", "b")) # Quickly see position of data qmap(dt) # Set color qmap(dt, color = I("blue")) # Map color to a variable qmap(dt, color = var) # Map text to a variable qmap(dt, label = var) # All basemap arguments work in qmap() dt <- data.frame(lon = c(-80, -80, -50, -50), lat = c(65, 80, 80, 65)) qmap(dt, rotate = TRUE)dt <- data.frame(lon = c(-100, -80, -60), lat = c(10, 25, 40), var = c("a", "a", "b")) # Quickly see position of data qmap(dt) # Set color qmap(dt, color = I("blue")) # Map color to a variable qmap(dt, color = var) # Map text to a variable qmap(dt, label = var) # All basemap arguments work in qmap() dt <- data.frame(lon = c(-80, -80, -50, -50), lat = c(65, 80, 80, 65)) qmap(dt, rotate = TRUE)
Simplifies bathymetry raster ready for the vector_bathymetry function. Warning: processing may take a long time if the bathymetry raster is large.
raster_bathymetry( bathy, depths, proj.out = NULL, proj.bathy = NULL, boundary = NULL, warp = FALSE, estimate.land = FALSE, downsample = NULL, verbose = TRUE )raster_bathymetry( bathy, depths, proj.out = NULL, proj.bathy = NULL, boundary = NULL, warp = FALSE, estimate.land = FALSE, downsample = NULL, verbose = TRUE )
bathy |
A stars object or a string giving the path to a bathymetry NetCDF or grd file |
depths |
Numeric vector giving the cut points for depth contours (see |
proj.out |
A character string specifying the coordinate reference system (CRS) argument for the output. See |
proj.bathy |
A character string specifying the |
boundary |
A st_polygon object, text string defining the file path to a spatial polygon, bounding box, or a numeric vector of length 4 giving the boundaries for which |
warp |
Logical indicating whether the resulting grid should be resampled to a new CRS if |
estimate.land |
Logical indicating whether to include land to the output. Can be used in the following |
downsample |
An integer defining how many rows in |
verbose |
Logical indicating whether information about progress and guessed projection should be returned. Set to |
You can use GEBCO, IBCAO, ETOPO bathymetry grids downloaded from respective sources as the bathy argument. The bathymetry grids read from files must be in any format read by read_stars. Alternatively use the marmap::getNOAA.bathy function to download ETOPO1 bathymetry and convert it to a raster object using the marmap::as.raster function.
Note that the size of the output is heavily influenced by the number of depth contours (depths) as well as the resolution of bathy and choice of downsample. To make the vector_bathymetry function and consequent plotting faster, limiting the details of the bathymetry raster may be desirable.
A list with a stars object the containing projected bathymetry defined by the proj.out argument and a data frame of depth intervals.
Mikko Vihtakari
GEBCO Compilation Group (2025) GEBCO 2025 (doi:10.5285/37c52e96-24ea-67ce-e063-7086abc05f29). URL: https://www.gebco.net/data-products-gridded-bathymetry-data/gebco2025-grid. NOAA National Centers for Environmental Information. 2022: ETOPO 2022 15 Arc-Second Global Relief Model. NOAA National Centers for Environmental Information. doi:10.25921/fd45-gt74.
Other create shapefiles:
clip_shapefile(),
geonorge_bathymetry(),
vector_bathymetry(),
vector_land(),
wcs_bathymetry()
basemap land, glacier and grid layers on top of other ggplot layersMoves existing land, glacier and grid layers on top of other layers. Useful for hiding region polygons under land.
reorder_layers(p)reorder_layers(p)
p |
ggplot object from the |
This function has not been tested properly yet and is likely to contain bugs.
Returns a ggplot object with land, glacier and grid layers on top.
Mikko Vihtakari
Other customize shapefiles:
auto_limits(),
theme_map()
if(requireNamespace("ggspatial", quietly = TRUE)) { data("ices_areas") p <- basemap(c(-20, 15, 50, 70)) + ggspatial::annotation_spatial(ices_areas, aes(fill = Area_Full), show.legend = FALSE) # Polygons on top of land p # Move land on top reorder_layers(p) }if(requireNamespace("ggspatial", quietly = TRUE)) { data("ices_areas") p <- basemap(c(-20, 15, 50, 70)) + ggspatial::annotation_spatial(ices_areas, aes(fill = Area_Full), show.legend = FALSE) # Polygons on top of land p # Move land on top reorder_layers(p) }
basemap
Lists available pre-made shapefiles for plotting in the basemap function. Gives also instructions how to make custom ones.
shapefile_list(name, get.data = FALSE)shapefile_list(name, get.data = FALSE)
name |
A character argument giving the name of a pre-made shapefile. Will be partially matched. Use "all" to list all available ones. |
get.data |
Logical indicating whether spatial data should be returned instead of names of spatial data objects. |
Custom shapefiles for basemap should be defined as lists with (at least) following names (everything should be provided as characters):
land Name of the object containing land polygons. Required.
glacier Name of the object containing glacier polygons. Use NULL if glaciers are not needed.
bathy Name of the object containing land polygons. Use NULL if bathymetry is not needed.
All linked spatial data objects must be in same projection. High-resolution pre-made data are still under development and may not be available. Pre-made shapefiles contain additional elements that are used in the basemap function, but not required for custom shapefile datasets.
Returns a data frame of provided pre-made shapefiles, if name = "all". Returns a shapefile list containing the information for a particular map otherwise.
Mikko Vihtakari
Other basemap functions:
basemap(),
qmap(),
transform_coord()
old_options <- options( ggOceanMaps.datapath = "path/to/ggOceanMapsLargeData", ggOceanMaps.userpath = "path/to/your/bathymetry.nc" ) shapefile_list("all") shapefile_list("Arctic") # partial matching options(old_options)old_options <- options( ggOceanMaps.datapath = "path/to/ggOceanMapsLargeData", ggOceanMaps.userpath = "path/to/your/bathymetry.nc" ) shapefile_list("all") shapefile_list("Arctic") # partial matching options(old_options)
A ggplot2 theme for maps.
theme_map(..., grid.col = NULL, grid.size = NULL)theme_map(..., grid.col = NULL, grid.size = NULL)
... |
additional arguments passed to |
grid.col |
Character code specifying the color of grid lines. Use |
grid.size |
Numeric value specifying the width of grid lines. |
A ggplot2 theme layer.
Other customize shapefiles:
auto_limits(),
reorder_layers()
Transforms spatial coordinates from original projection (decimal degrees assumed) to another projection.
transform_coord( x = NULL, lon = NULL, lat = NULL, new.names = "auto", rotate = FALSE, proj.in = 4326, proj.out = NULL, verbose = FALSE, bind = FALSE, na = "ignore" )transform_coord( x = NULL, lon = NULL, lat = NULL, new.names = "auto", rotate = FALSE, proj.in = 4326, proj.out = NULL, verbose = FALSE, bind = FALSE, na = "ignore" )
x |
Data frame to be transformed. Can be omitted if numeric vectors are assigned to |
lon, lat
|
Either a name of the longitude and latitude columns in |
new.names |
Character vector of length 2 specifying the names of transformed longitude and latitude columns, respectively. Alternatively |
rotate |
Logical indicating whether the projected maps should be rotated to point towards the pole relative to the mid-longitude limit. |
proj.in |
The original |
proj.out |
Character. Either |
verbose |
Logical indicating whether information about the projection should be returned as message. Set to |
bind |
logical. Should only transformed coordinates be returned ( |
na |
character specifying the NA action for missing coordinates. The "ignore" option ignores the coordinates and returns NAs to transformed coordinates. The "remove" option removes missing values from |
If x is specified, the function guesses longitude and latitude columns from x by default.
Returns a data frame with transformed spatial coordinates.
Mikko Vihtakari
Other basemap functions:
basemap(),
qmap(),
shapefile_list()
# Coordinates are automatically transformed to the pre-made shapefile # projections: x <- data.frame(lon = c(-150, 150), lat = c(60, 90)) transform_coord(x) transform_coord(x, bind = TRUE) x <- data.frame(lon = c(-150, 150), lat = c(20, 50)) transform_coord(x, bind = TRUE) # no transformation required.# Coordinates are automatically transformed to the pre-made shapefile # projections: x <- data.frame(lon = c(-150, 150), lat = c(60, 90)) transform_coord(x) transform_coord(x, bind = TRUE) x <- data.frame(lon = c(-150, 150), lat = c(20, 50)) transform_coord(x, bind = TRUE) # no transformation required.
Vectorizes bathymetry rasters. Designed to be used for the output of raster_bathymetry function. Warning: processing may take a long time if the bathymetry raster is large.
vector_bathymetry( bathy, drop.crumbs = NULL, remove.holes = NULL, smooth = FALSE )vector_bathymetry( bathy, drop.crumbs = NULL, remove.holes = NULL, smooth = FALSE )
bathy |
bathyRaster object from the |
drop.crumbs |
Single numeric value specifying a threshold (area in km2) for disconnected polygons which should be removed. Set to |
remove.holes |
Single numeric value specifying a threshold (area in km2) for holes which should be removed. Set to |
smooth |
Logical indicating whether the pixelated contours should be smoothed. Uses the smooth_ksmooth function. |
The drop.crumbs and remove.holes arguments can be used to make the resulting object smaller in file size. The smooth argument can be used to remove the pixelated contours, but often increases file size. Note also that using this option will bias the contours with respect to real world.
An sf object containing the depth polygons. Uses same projection than bathy (see CRS).
Mikko Vihtakari
Other create shapefiles:
clip_shapefile(),
geonorge_bathymetry(),
raster_bathymetry(),
vector_land(),
wcs_bathymetry()
Extracts the land area from a bathyRaster object produced
by raster_bathymetry and returns it as an
sf polygon layer suitable for use in the
shapefiles argument of basemap. Warning: processing
may take a long time if the bathymetry raster is large.
vector_land(bathy, drop.crumbs = NULL, remove.holes = NULL, smooth = FALSE)vector_land(bathy, drop.crumbs = NULL, remove.holes = NULL, smooth = FALSE)
bathy |
A |
drop.crumbs |
Single numeric value specifying a threshold (area in km2) for disconnected polygons which should be removed. Set to |
remove.holes |
Single numeric value specifying a threshold (area in km2) for holes which should be removed. Set to |
smooth |
Logical indicating whether the pixelated contours should be smoothed. Uses the smooth_ksmooth function. |
The drop.crumbs and remove.holes arguments can be used
to reduce the resulting file size. The smooth argument removes the
pixelated contours but increases file size and biases the polygon with
respect to the underlying raster.
Use vector_land() together with vector_bathymetry to
build a matched land + bathymetry pair from a single source raster (e.g.
GEBCO, ETOPO, IBCAO) — see the example.
An sf object containing the land polygons in
the same projection as bathy$raster.
Mikko Vihtakari
raster_bathymetry, vector_bathymetry
Other create shapefiles:
clip_shapefile(),
geonorge_bathymetry(),
raster_bathymetry(),
vector_bathymetry(),
wcs_bathymetry()
Fetches gridded bathymetry data on demand from an OGC Web
Coverage Service (WCS) and returns it as a bathyRaster object
compatible with basemap and vector_bathymetry.
Two sources are currently supported: "emodnet" (~115 m European
waters) and "etopo" (1 arc-minute / ~1.85 km global).
wcs_bathymetry( limits, source = "emodnet", coverage = NULL, cache_dir = NULL, force = FALSE, max_area_deg2 = NULL, tile_size_deg = NULL, downsample = 0, timeout = 60, verbose = TRUE )wcs_bathymetry( limits, source = "emodnet", coverage = NULL, cache_dir = NULL, force = FALSE, max_area_deg2 = NULL, tile_size_deg = NULL, downsample = 0, timeout = 60, verbose = TRUE )
limits |
Numeric vector of length 4 giving the bounding box in decimal
degrees as |
source |
Character. The WCS source to query. One of |
coverage |
Character. Override the default coverage for the source
(e.g. |
cache_dir |
Character. Directory in which downloaded GeoTIFFs are
cached. Defaults to |
force |
Logical. If |
max_area_deg2 |
Numeric. Maximum bounding-box area (in degree-squared)
allowed before the function errors. Guards against accidentally
downloading a very large area. |
tile_size_deg |
Numeric. Edge length (in degrees) of the largest
single-request tile. |
downsample |
Non-negative integer. Number of grid cells to skip when
requesting the raster. |
timeout |
Positive numeric. Minimum HTTP timeout in seconds. A larger
existing global |
verbose |
Logical. Print download progress and informational messages. |
EMODnet serves the European-waters bathymetric DTM at ~115 m native resolution (~0.00104 deg) in EPSG:4326 GeoTIFF format. The 1x1 deg tile around the North Sea is ~4 MB; a 5x5 deg tile would be ~100 MB and hit the server's ~98 MB read cap. Coverage is European regional seas only (~-36 to 43 lon, ~15 to 90 lat).
ETOPO (ETOPO1 Ice Surface, served by NOAA NCEI) is a global
1 arc-minute (~1.85 km) topo-bathy grid in EPSG:4326. Useful when EMODnet
has no coverage. NCEI returns the GeoTIFF inside a multipart/related MIME
envelope; wcs_bathymetry() extracts the binary part transparently.
To handle bounding boxes larger than the source's single-request cap,
wcs_bathymetry() splits the request into tiles of at most
tile_size_deg per axis, caches each tile, and mosaicks them via a
GDAL virtual raster. Each tile is cached independently so subsequent
overlapping requests reuse what's already on disk.
The returned object is a bathyRaster (same class returned by
raster_bathymetry with depths = NULL) and can be
slotted directly into basemap(shapefiles = list(bathy = ...)) or
passed to vector_bathymetry.
Citation requirements: EMODnet bathymetry is published under CC-BY and must be cited if used in figures (https://emodnet.ec.europa.eu/en/bathymetry). ETOPO1 also requires citation (Amante & Eakins 2009, NOAA NGDC; see https://www.ncei.noaa.gov/products/etopo-global-relief-model).
A bathyRaster object: a list with elements raster
(a read_stars object with positive depth values) and
depth.invervals (a length-2 numeric range).
Mikko Vihtakari
Other create shapefiles:
clip_shapefile(),
geonorge_bathymetry(),
raster_bathymetry(),
vector_bathymetry(),
vector_land()
## Not run: # European waters, high resolution bathy <- wcs_bathymetry(c(2, 3, 54, 55), source = "emodnet") basemap(c(2, 3, 54, 55), shapefiles = list(land = dd_land, glacier = NULL, bathy = bathy$raster), bathymetry = TRUE) # Global coverage (Hawaii -- outside EMODnet) bathy <- wcs_bathymetry(c(-160, -154, 18, 23), source = "etopo") basemap(c(-160, -154, 18, 23), shapefiles = list(land = dd_land, glacier = NULL, bathy = bathy$raster), bathymetry = TRUE) ## End(Not run)## Not run: # European waters, high resolution bathy <- wcs_bathymetry(c(2, 3, 54, 55), source = "emodnet") basemap(c(2, 3, 54, 55), shapefiles = list(land = dd_land, glacier = NULL, bathy = bathy$raster), bathymetry = TRUE) # Global coverage (Hawaii -- outside EMODnet) bathy <- wcs_bathymetry(c(-160, -154, 18, 23), source = "etopo") basemap(c(-160, -154, 18, 23), shapefiles = list(land = dd_land, glacier = NULL, bathy = bathy$raster), bathymetry = TRUE) ## End(Not run)