yuliaUU
yuliaUU

Reputation: 1713

How to colour the ocean?

I am trying to make a map using basemap() function

library(ggplot2)
library(ggOceanMapsData)
library(ggOceanMaps)

dt <- expand.grid(lon = c(-129, -124), lat = c(49, 53))
basemap(data = dt,land.border.col = "grey40")

I found a way to colour continents, but not the ocean.

I tried doing theme(panel.background = element_rect(fill = "lightblue")), but it just overlays this layer on top of the graph, and I could not figure out how to transfer it at the back, so that the baseline map was visible.

Upvotes: 3

Views: 1201

Answers (2)

Mikko
Mikko

Reputation: 7755

You could also modify the ggplot2 background but then you'll lose the grid lines. A solution might be to hide them (second example). If you really want them, one can dig into the map_cmd code to manually create them.

library(ggOceanMaps)
#> Loading required package: ggplot2
#> Loading required package: ggspatial
#> Setting data download folder to a temporary folder
#> /var/folders/fm/xtn13rlj7d3gp8twy7hh9prxtlq_m4/T//RtmprYybGO. This
#> means that any downloaded map data need to be downloaded again when you
#> restart R. To avoid this problem, change the default path to a
#> permanent folder on your computer. Add following lines to your
#> .Rprofile file: {.ggOceanMapsenv <- new.env(); .ggOceanMapsenv$datapath
#> <- 'YourCustomPath'}. You can use usethis::edit_r_profile() to edit the
#> file.'~/Documents/ggOceanMapsLargeData'would make it in a writable
#> folder on most operating systems.

dt <- expand.grid(lon = c(-129, -124), lat = c(49, 53))

basemap(data = dt) + 
  theme(panel.background = element_rect(fill = "lightblue"),
        panel.ontop = FALSE) 
#> Using lon and lat as longitude and latitude columns, respectively.
#> projection transformed from EPSG:4326 to EPSG:3995

basemap(data = dt) + 
  theme(panel.background = element_rect(fill = "lightblue"),
        panel.ontop = FALSE,
        panel.grid = element_blank()) 
#> Using lon and lat as longitude and latitude columns, respectively.
#> projection transformed from EPSG:4326 to EPSG:3995

Created on 2021-06-24 by the reprex package (v2.0.0)

Upvotes: 2

mgrund
mgrund

Reputation: 1625

Not sure if it's possible to fill the oceans with a single color by any command. Here's a workaround in which the colors for the bathymetry are all set to a single one.

library(ggplot2)
library(ggOceanMapsData)
library(ggOceanMaps)

dt <- expand.grid(lon = c(-129, -124), lat = c(49, 53))

basemap(data = dt, land.border.col = "grey40", bathymetry = T, legends = F) + 
  scale_fill_manual(values = c(rep("lightblue", 7)))

Upvotes: 3

Related Questions