<-"https://github.com/MiriamLL/data_supporting_webpage/raw/refs/heads/main/Blog/2025/SST/cmems_mod_glo_phy_my_0.083deg-climatology_P1M-m_1729785319560.nc" SST_nc
Sea surface temperature
In this blog post, I walk you through the process of visualizing sea surface temperature in R. From downloading the dataset to reading it and creating a map using ggplot.
Intro
The Copernicus Marine Service (CMS), also known as the Copernicus Marine Environment Monitoring Service, is the marine component of the European Union’s Copernicus Programme.
It delivers free, regular, and systematic information on the state of the ocean, encompassing the Blue (physical), White (sea ice), and Green (biogeochemical) components, both globally and regionally.
Funded by the European Commission (EC) and implemented by Mercator Ocean International, the service is designed to support EU policies and international legal commitments related to Ocean Governance. It also addresses the global need for ocean knowledge and fosters the Blue Economy across all maritime sectors by providing cutting-edge ocean data and information at no cost.
This post includes:
- Download raster data
- Read and subset raster data
- Plot raster data
Data
Raster data is available at: Copernicus Marine Data Store
To access the products. You need to register to be able to download data
Sea surface temperature (SST) refers to the temperature of the uppermost layer of the ocean, typically measured at the surface of the sea.
For downloading the data: follow this instructions.
- For this exercise:
Download sea water potential temperaturethetao [°C]
Based on the coordinates 1 to 10 and 50 to 60
For the time frame of April and May 2018
From the depths 0 to 60 m
To download test data in nc format click here.
Read data
Select the directory where the information is stored, or as here, use the data directly form the repository.
Load the terra package for reading raster data.
library(terra)
The function rast helps to read raster data - replacing package raster.
<-rast(SST_nc) SST_file
Subset raster data
Change to data frame for data wrangling.
<- as.data.frame(SST_file, xy = TRUE) SST_dataframe
Load the package tidyverse.
library(tidyverse)
Use filter to subset the data to a specific geographical area.
<-SST_dataframe %>%
SST_sub filter(x > 2 & x < 10)%>%
filter(y > 52 & y < 57)
Obtain mean values per latitude and longitude
There are many columns with data per depth, as data was collected almost every meter.
ncol(SST_sub)
To keep columns with the depth area use the functions select and starts_with.
<-SST_sub %>%
SST_depthselect(starts_with("thetao"))
Check the depths were the data was collected.
<-colnames(SST_depth)
Depthshead(Depths)
Using functions from tidyverse such as rowwise, summarise the information per depth.
<-SST_depth %>%
SST_depth_perlocrowwise()%>%
mutate(mean_SST = mean(c_across(where(is.numeric)),na.rm=TRUE),
min_SST = min(c_across(where(is.numeric)),na.rm=TRUE),
max_SST = max(c_across(where(is.numeric))),na.rm=TRUE)%>%
relocate(mean_SST,min_SST,max_SST)
The function arrange from the package tidyverse allows to see the columns of interest first.
<-SST_depth[1,]
first_column<-first_column %>%
long_valuespivot_longer(
cols = 1:228,
names_to = "type",
values_to = "value"
)<-arrange(long_values, desc(value)) arrange_values
Now there is a value per latitude and longitude of the sea surface temperature, summarizing the first 60 m of the water column.
$SST<-SST_depth_perloc$mean_SST SST_sub
mean(SST_sub$SST)
Plot
Load the package ggplot2
library(ggplot2)
Plot your data using geom_raster
ggplot() +
geom_raster(data = SST_sub , aes(x = x, y = y, fill = SST)) +
theme_void()+
theme(legend.position='bottom')+
coord_sf(xlim = c(3,9), ylim = c(53,56))
To select different palettes explore ggplot2 book. Here are some of the palette options.
To change the color palette use scale_fill_distiller.
ggplot() +
geom_raster(data = SST_sub , aes(x = x, y = y, fill = SST)) +
scale_fill_distiller(palette = "YlOrBr")+
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'))
Add land shapefiles to complete the map. To add shapefiles load the package sf.
library(sf)
Use the function st_transform to convert to the same CRS.
For the exercise, use the shapefiles from the package GermanNorthSea
<-st_transform(GermanNorthSea::German_land, 4326) German_land
Add the land to the plot using geom_sf.
<-ggplot() +
Plot_sstgeom_tile(data = SST_sub , aes(x = x, y = y, fill = SST)) +
scale_fill_distiller(name="Sea surface temperature (°C) \nFrom 0 to 60 m depth\n(April and May montly composites)",
palette = "Spectral")+
geom_sf(data = German_land, colour = 'black', fill = '#d5bdaf')+
coord_sf(xlim = c(3,9), ylim = c(53,56),
label_axes = list(top = "E", left = "N", bottom = 'E', right='N'))+
theme_void()
Plot_sst
Change a little bit the design using arguments in the theme.
+
Plot_sst theme_void()+
xlab('Longitude')+ylab('Latitude')+
theme(legend.background = element_rect(colour = "transparent", fill = "transparent"),
legend.position = c(0.40,0.30),
panel.background = element_rect(fill = '#edede9'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1.5))+
NULL
Further reading
- Examples from Copernicus
- How to download Copernicus marine products
- Video Tutorials from Copernicus
- Comparing with other studies palettes: SST in the German North Sea from 2004
- Package GermanNorthSea
- Package terra
- Package sf