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
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)

Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With