0

I am relatively new in r and creating leaflet plot for which I need a white background instead of grey.

I came across this SO post for same: blank, white background for leaflet map & tried:

Plot code:

daily_leaflet_plt <- ind_states %>% leaflet(options = leafletOptions(zoomControl = FALSE,
                                                dragging = FALSE,
                                                minZoom = 3,
                                                style = list(
                                                  "background-color" = "white"
                                                )
                                                )) %>%
          
          addPolygons(
                      label = label_daily,
                      labelOptions = labelOptions(
                        opacity = .6,
                        style = list(
                            "color" = "white",
                            "background-color" = "black",
                            "font-size" = "15px",
                            "border-color" = "rgba(0,0,0,0.5)"
                        )
                      ),
                      stroke = TRUE,
                      color = "white",
                      dashArray = "3",
                      weight = .2,
                      smoothFactor = .5,
                      opacity = 1,
                      fillOpacity = 0.5,
                      fillColor = ~ pal_daily(Daily_confirmed),
                      highlightOptions = highlightOptions(weight = .5,
                                                          fillOpacity = 1,
                                                          bringToFront = TRUE)) %>%

          addLegend(position = "bottomleft",
                    pal = pal_daily,
                    values = ~ ind_states$Daily_confirmed,
                    title = "Daily Confirmed Cases",
                    opacity = 0.7)

css as per SO post:

```{r results="daily_leaflet_plt"}
cat("
<style>
.leaflet-container {
    background: #FFF;
}
</style>
")
```

But this doesn't work, How can I get white background space in leaflet plt in rmarkdown & shiny as eventually I will be using this in shiny.

I have also raised this in another SO post which is phrased differently & remains unanswered: How to change color of leaflet map around the polygon shape in r?

ViSa
  • 1,563
  • 8
  • 30

2 Answers2

1

I think your CSS selector is correct, but I'd personally find it easier to just specify that in the typical Shiny way, i.e.,

library(shiny)
library(leaflet)

ui <- fluidPage(
    tags$head(tags$style(HTML(".leaflet-container {background: none;}")))
    leafletOutput("map")
)

server <- function(input, output, session) {
    output$map <- renderLeaflet({
        leaflet(quakes) %>%
            addTiles()
     })
}

shinyApp(ui, server)

For R markdown, you can embed a CSS code chunk as you would an R code chunk, e.g.,

```{css, echo = FALSE}
.leaflet-container {
    background: none;
}
```


```{r}
library(leaflet)

leaflet(quakes) %>%
    addTiles()
```
heds1
  • 3,203
  • 2
  • 17
  • 32
  • Thanks alot @heds1 . I was stuck on this from a while & really glad this got resolved. Your code is working perfectly in `shiny` which was my final goal but somehow not working in `rmarkdown`. May be some cache issues I am not sure but it worked in my end goal shiny so I can relax now. Thanks again for complete solution, really appreciate ur help :) – ViSa Aug 13 '21 at 07:10
-1

I am not sure but I guess "results" part is wrong.

enter image description here

source: Source of image "Rmarkdown Reference"

I think you should use results = 'asis'

gokhan can
  • 189
  • 9
  • Thanks @G. Can but I am not sure why this is not working in my code. I have tried it multiple times but couldn't get the desired results! – ViSa Aug 13 '21 at 07:12