3

I'm using a geojson file to produce a map with leaflet on R. I would like to change the background colour to the white, or make the background transparent if it is possible (this is actually really desired one). I have seen this and this. I am able to change the border colour and filled colour but cannot change the colour of the outside of map > background colour.

wLeaf <- leaflet(states) %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.light",
    accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')))%>%
    addPolygons(
      weight = 2,
      opacity = 1,
      color = "#222",
      fillColor = "gray",
    )

How can I handle the colour or transparency issue of background for the map?

Thanks

Phil
  • 7,287
  • 3
  • 36
  • 66
fillo
  • 365
  • 1
  • 12
  • Have you tried to set you basemap totally transparent i.e. `opacity = 0` under providerTileOptions? – G. Cocca May 29 '22 at 15:24

1 Answers1

1

in case it helps, first create the background call

backg <- htmltools::tags$style(".leaflet-container { background: tomato; }" )

Then you can add this object as CSS format to your map

sts <- tigris::states(cb = TRUE) # you map
leaflet::leaflet(data = sts) %>% 
addPolygons(fillColor = "grey90", stroke = FALSE) %>%
htmlwidgets::prependContent(backg) #this applies the CSS format

All together:

library(dplyr)
backg <- htmltools::tags$style(".leaflet-container { background: tomato; }" )  
sts <- tigris::states(cb = TRUE)
leaflet::leaflet(data = sts) %>% 
addPolygons(fillColor = "grey90", stroke = FALSE) %>%
htmlwidgets::prependContent(backg)

Result with tomato background color

Cheers!