Water depth in 3D

For a soon-to-be submitted paper, I was called on to look at data for a deep-diving marine mammal. Depth normally influences their distribution, so I investigated depths in the study region of the Bay of Biscay, which borders the coast of France and Spain.

This code will bring in some depth data for an area, process this data in R, and plot it in 3D.

library(raster)
## Warning: package 'raster' was built under R version 4.0.5

## Warning: package 'sp' was built under R version 4.0.4
library(rayshader)
## Warning: package 'rayshader' was built under R version 4.0.5
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.5

## Warning: package 'ggplot2' was built under R version 4.0.5

Now lets bring in our depth data. I used freely downloadable data from EMODnet (https://www.emodnet-bathymetry.eu/). This depth data will be in the form of a raster, which we can then turn into a matrix, assign an area to either be land (over 0m depth), or water (under 0 m depth)

depth <- raster("depth.tif")
depth <- aggregate(depth, fact = 5, fun = mean)
land_rast <- reclassify(depth, cbind(-Inf, 0, NA), right=T)
land = raster_to_matrix(land_rast)
water_rast <- reclassify(depth, cbind(0, Inf,NA), right=F)
water = raster_to_matrix(water_rast)

Okay, we have our data. Now let’s plot it using plotly. We already loaded the package, so lets jump right in.

fig <- plot_ly(showscale = T)
fig <- fig %>% add_surface(z = ~water) %>% add_surface(z = ~land, opacity = 0.98, colorscale = list(c(0, 1), c("dark brown", "dark brown")))%>%
  layout(scene = list(xaxis = list(title = 'Longitude'))) %>% layout(scene = list(yaxis = list(title = 'Latitude')))%>%
  layout(scene = list(zaxis = list(title = 'Depth (m)')))
fig