R: Add title to Leaflet map

YGS picture YGS · Mar 2, 2018 · Viewed 16.8k times · Source

I'd like to add a title to the whole map (different from the legend title: addLegend(..., title = "", ...): by "title", I mean an overlaid map component that stays in place while the map is moved (unlike an overlaid image)what the map title could look like.

Is that an option in RStudio's leaflet for R?

leafletR has a title="" argument but it updates the title of the webpage: it does not add a title to the map.

Answer

prusswan picture prusswan · Sep 7, 2018

@MLavoie's idea is correct, but I was looking for something more specific like this:

tag.map.title <- tags$style(HTML("
  .leaflet-control.map-title { 
    transform: translate(-50%,20%);
    position: fixed !important;
    left: 50%;
    text-align: center;
    padding-left: 10px; 
    padding-right: 10px; 
    background: rgba(255,255,255,0.75);
    font-weight: bold;
    font-size: 28px;
  }
"))

title <- tags$div(
  tag.map.title, HTML("Map title")
)  

map_leaflet <- leaflet() %>%
  addTiles() %>%
  addControl(title, position = "topleft", className="map-title")

This will center the leaflet-control title as shown in the screenshot and place it at the top.