Create a map with distance to coast information in R.
Distance to coast gives us information on the distance (in meters) from one point at sea to the nearest coast.
This post includes:
- Download raster data
- Read and subset raster data
- Plot raster data
To download:
- Access OceanColor NASA
- Select the interpolated 0.01-degree GeoTiff packed together with a brief description file.
- Unzip information.
To read from file.
Select the directory where the information is.
Use the package terra to use the function rast.
Then convert to data frame.
library(terra)
DistCoast_file<-rast(this_file)
DistCoast_dataframe <- as.data.frame(DistCoast_file, xy = TRUE)
beepr::beep(sound=1)
The file is quite large, so I recommend to subset the data to the area of interest.
Here I select the area close to the German North Sea.
To save as rda would make the reading a lot faster.
German_distancecoast<-DistCoast_dataframe_sub
save(German_distancecoast, file="German_distancecoast.rda")
From the package.
Alternatively, access the information from the package GermanNorthSea.
library(GermanNorthSea)
DistCoast_dataframe<-GermanNorthSea::German_distancecoast
To plot adding land.
Make sure is in the same CRS.
German_land<-st_transform(GermanNorthSea::German_land, 4326)
To exclude information on land.
Use ggplot to create your plot.
Plot_distance<-ggplot() +
geom_raster(data = DistCoast_dataframe_sub, aes(x = x, y = y, fill = Dist)) +
geom_sf(data = German_land, colour = 'black', fill = '#ffffbe')+
scale_fill_viridis_c(option = "rocket")+
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_distance+
guides(fill=guide_legend(title="Distance to coast"))