Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ocean color in robinson projection map

Tags:

r

ggplot2

I am making a map of the world in a Robinson projection (i.e., oval) and would like to distinguish ocean from land by making the color of the ocean blue. How can I do this for my non-rectangle projection?

library("ggplot2")
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

world <- ne_countries(scale = "medium", returnclass = "sf")

# Creates map with land and ocean as color white
ggplot(data = world) +
  geom_sf(color = "gray60", fill = "white") +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) +
  theme_bw() +
  theme(panel.grid.major = element_line(color = 'gray75', linetype = "dashed", size = 0.25),
        panel.border = element_blank(),
        panel.background = element_blank())

# Creates map with a blue ocean however, the blue extends to a rectangle shape
# Would be ideal if the blue ocean fill was confined to the oval shape of the projection
ggplot(data = world) +
  geom_sf(color = "gray60", fill = "white") +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) +
  theme_bw() +
  theme(panel.grid.major = element_line(color = 'gray75', linetype = "dashed", size = 0.25),
        panel.border = element_blank(),
        panel.background = element_rect(fill = 'aliceblue'))
like image 880
tassones Avatar asked Sep 12 '25 08:09

tassones


1 Answers

Create the ocean as a polygon and plot it first.

ocean <- st_polygon(list(cbind(c(seq(-180, 179, len = 100), rep(180, 100), 
                        seq(179, -180, len = 100), rep(-180, 100)),
                      c(rep(-90, 100), seq(-89, 89, len = 100),
                        rep(90, 100), seq(89, -90, len = 100))))) |>
  st_sfc(crs = "WGS84") |>
  st_as_sf()

ggplot(data = world) +
  geom_sf(data = ocean, fill = "#8080ff80") +
  geom_sf(color = "gray60", fill = "white") +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 
           +datum=WGS84 +units=m +no_defs", expand = F) +
  theme_bw() +
  theme(panel.grid.major = element_line(color = 'gray75', 
                                        linetype = "dashed",
                                        linewidth = 0.25),
        panel.border = element_blank(),
        panel.background = element_rect(fill = 'white'))

enter image description here

like image 79
Allan Cameron Avatar answered Sep 15 '25 00:09

Allan Cameron