0

I am trying to add ocean color to a leaflet map without calling addTiles(). Effectively I want a simple map, generated by a shapefile, without all the extra data that comes from the addTiles() layer.

I can change the colors of areas marked in the shapefile by access characteristics in the @data slot. But how do I change colors for everything outside of that?

Here is an example:

#Load libraries
library('rgdal')
library('leaflet')

#Create save location
dir.create('DATA')

#Download and unzip file
download.file(url = 'http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip',
              destfile = 'DATA/world_shape_file.zip')
unzip(zipfile = 'DATA/world_shape_file.zip', 
      exdir = 'DATA')

#Create spdf
world_spdf <- readOGR('DATA/TM_WORLD_BORDERS_SIMPL-0.3.shp')

#plot spdf
leaflet() %>%
  addPolygons(data = world_spdf,
              weight = 0.5,
              color = 'grey', 
              fillColor = 'white')

Calling this generates an image of the ocean as grey. I want to be able to set this to a shade of blue. Seems like it should be simple but all solutions use addTiles() which adds all sorts of name data etc. to my map.

MorrisseyJ
  • 1,191
  • 12
  • 19
  • An idea is to build a spatialpolygon or a spatialpolygondataframe that stretches over the world boundaries and plot it under your polygon with world boundaries. – G. Cocca Jun 13 '22 at 12:08
  • Add `my_box <- rgeos::bbox2SP(n = 90, s = -89.53407, w = -180, e = 180, proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))` and add the object to your leaflet map – G. Cocca Jun 16 '22 at 15:34
  • @G. Cocca: This works perfectly. Thanks for the code. Note that this question has been closed. I think your answer is much simpler than that provided in the linked question here. If you want to post this answer there, i'll up vote it. – MorrisseyJ Jun 16 '22 at 20:18

0 Answers0