1

I have created maps using the ggplot function on R. I then wanted to add bathymetric lines. I so downloaded them from the NOAA portal using the code lines:

library(marmap)
bat <- getNOAA.bathy(-11.99792 ,-5.002083 ,35.00208,43.99792,res=4, keep=TRUE)
plot(bat, land=TRUE, n=100, lwd=0.03)
plot(bat, deep=-200, shallow=-200, step=0, lwd=0.5, drawlabel=TRUE, add=TRUE)
plot(bat, deep=-1000, shallow=-1000, step=0, lwd=0.3, drawlabel=TRUE, add=TRUE)

Until this moment the lines look homogeneous and not discontinued. I then run these two lines:

bathy_df <- inlmisc::Grid2Polygons(as.SpatialGridDataFrame(bat), 
                               level=TRUE, pretty=TRUE)
bathy_map <- fortify(bathy_df)

But then, when I add bathy_map to my ggplot, the lines of the bathymetry appear as you can see in the attached picture: not homogeneous at all and discontinue. This is the script of my ggplot:

D <- ggplot() + 
  geom_raster(data = Fig_D, aes(x = x, y = y, fill = value)) +
  facet_wrap(~ variable) +
  coord_equal() +
  scale_fill_viridis_c(option = "turbo",  direction = 1, limits=c(0, 160)) +
  geom_polygon(data=bathy_map, 
           aes(x=long,y=lat,group=group), inherit.aes=F, 
           colour='black', fill=NA, lwd=0.5)+
  geom_polygon(data = coast_map,
               aes(x=long,y=lat,group=group), inherit.aes=F, 
               colour='black', fill='gray', lwd=0.5) +
  theme_void(base_size = 12) +
  theme(strip.text.x = element_text(size = 10, vjust = 1)) +
  theme(legend.position = "right", legend.justification = "left") +
  labs(fill='SPL')

(the first geom_polygon is the one referring to the bathymetric lines)

Do you know how I can make these lines look more homogeneous? And also, do you know how I can show the depth of some of the most important bathymetric lines? Thank you.

Maps created with ggplot showing bathymetric lines

  • Welcome to Stackoverflow. Why `geom_polygon` vs `geom_contour` for your bathy lines, just very generally? [Bathy maps-ggplot()](https://hansenjohnson.org/post/bathymetric-maps-in-r/), and perhaps mention `library(marmap)` in your post. The `contours` approach allows you to 'declare' your most important bathymetric lines. – Chris Sep 02 '22 at 12:43
  • Thank you very much! I tried with geom_contour but it didn't work out... I added this code line (instead of the geom_polygon one): geom_contour(data = bat, aes(x=long,y=lat,group=group), inherit.aes=F, colour='black', fill=NA, lwd=0.3) But it doesn't give me the plot... You are right, I will mention 'library(marmap)' in my post. – Giulia Spadoni Sep 02 '22 at 16:24

1 Answers1

2

When you want to draw maps with ggplot2, always think library(sf)! ;-)

Here is an example:

# Load useful packages
library(sf)
library(marmap)
library(tidyverse)
library(rnaturalearth)

# Get bathymetric data
bat <- getNOAA.bathy(-12, -5, 35, 44, res = 4, keep = TRUE)
bat_xyz <- as.xyz(bat)

# Import country data
country <- ne_countries(scale = "medium", returnclass = "sf")

# Plot using ggplot and sf
ggplot() + 
  geom_sf(data = country) +
  geom_tile(data = bat_xyz, aes(x = V1, y = V2, fill = V3)) +
  geom_contour(data = bat_xyz, 
               aes(x = V1, y = V2, z = V3),
               binwidth = 100, color = "grey85", size = 0.1) +
  geom_contour(data = bat_xyz, 
               aes(x = V1, y = V2, z = V3),
               breaks = -200, color = "grey85", size = 0.5) +
  geom_sf(data = country) +
  coord_sf(xlim = c(-12, -5), 
           ylim = c(35, 44)) +
  labs(x = "Longitude", y = "Latitude", fill = "Depth (m)") +
  theme_minimal()

And here is the result.

enter image description here

You don't need to use inlmisc::Grid2Polygons() nor ggplot2::fortify(). Simply use marmap::as.xyz() to transform your bathymetric data into a long data.frame, and use either (or both), of geom_tile() and geom_contour().

Of course, you can add several layers of geom_contour(). Here, I've added one with one isobath every 100 meters, and a darker one at -200m.

As for labelling some (or all) of the isobaths, have a look at metR::geom_label_contour()

Benoit
  • 1,154
  • 8
  • 11
  • That's a beautiful map, and best to have a pkg author address issues. I'm running into issue #27, probably related to Labor Day weekend here, so will pick it up next week. – Chris Sep 02 '22 at 20:12
  • Nice plot!!. I was wondering if instead of passing the second `geom_sf(data = country)`, will be able to plot positive (above 0 meters) lines to see "mountains" or land features more than a flat polygon, using some topocolours, for example...? – Antonio Canepa Aug 31 '23 at 08:57
  • Yes, it's possible. Take a look at `?marmap::etopo.colors()` for examples. – Benoit Sep 01 '23 at 09:27