Plot bathymetry in R

Create a map with bathymetry information in R.

Miriam Lerma true
2025-01-10

Intro

Bathymetry gives us information on the water depth around an area.

This post includes:
- Download raster data
- Read and subset raster data
- Plot raster data

Download raster data

GEBCO: General Bathymetry Chart of the Oceans provides information from bathymetry in the ocean.

To download visit webpage

Go to:
- Download data for user-defined areas
- Use the application
- Add your coordinates -here I use 1 to 10 and 50 to 60-
- Add to basket and download

Load raster data

Once you had download the information, give the direction of the file. For example:

library(here)
here()
this_folder<-paste0(here(),"this_directory")
this_file<-paste0(this_folder,"this_file")

Load the terra package for reading raster data

The function rast helps to read raster data - replacing package raster

Bath_file<-rast(this_file)

Change to data frame

Bath_dataframe <- as.data.frame(Bath_file, xy = TRUE)

Alternatively, use the test information included in the package GermanNorthSea

##devtools::install_github("MiriamLL/GermanNorthSea")
library(GermanNorthSea)
Bath_dataframe<-GermanNorthSea::German_bath

Load the package tidyverse

Use filter to subset your data

Bath_dataframe_sub <-Bath_dataframe  %>%
  filter(x > 2 & x < 10)%>%
  filter(y > 52 & y < 57)%>%
  rename(Bathymetry=3) %>%
  filter(Bathymetry < 10)

Plot

Load the package ggplot2

Plot your data using geom_raster

ggplot() +
  geom_raster(data = Bath_dataframe_sub , aes(x = x, y = y, fill = Bathymetry)) +
  scale_fill_viridis_c(option = "mako")+
  theme_void()+
  theme(legend.position='bottom')+
  xlab('Longitude')+ylab('Latitude')+
  coord_sf(xlim = c(3,9), ylim = c(53,56),
                    label_axes = list(top = "E", left = "N", bottom = 'E', right='N'))

Load the package sf

Use the function st_transform to convert to the same CRS

German_land<-st_transform(GermanNorthSea::German_land, 4326)

Add the land to the plot using geom_sf

Plot_bath<-ggplot() +
  geom_raster(data = Bath_dataframe_sub , aes(x = x, y = y, fill = Bathymetry)) +
  geom_sf(data = German_land, colour = 'black', fill = '#ffffbe')+
  scale_fill_viridis_c(option = "mako")+
  theme_void()+
  theme(legend.position='bottom')+
  xlab('Longitude')+ylab('Latitude')+
  coord_sf(xlim = c(3,9), ylim = c(53,56),
                    label_axes = list(top = "E", left = "N", bottom = 'E', right='N'))
Plot_bath

More details about how to add features to a map here

Further reading