0

I have attempted to create a leaflet map of the birthplaces of NHL players. However, I would like to add a title and/or a description in the map. I have attempted the addLegend argument and addControl but cant get a useful description. Below is the code I have used for the map.

    pal <- colorFactor(palette = c("red", "blue", "green", "yellow", "black"), 
       levels = c("G", "D", "C", "LW", "RW"))
  
    m <- map %>%
 addCircleMarkers(data = players, radius = 1, popup = ~fullname,
             color = ~pal(players$nationality),
             label = paste0(players$fullname, "(", players$nationality, ")"))

 m4 <- leaflet() %>% 
 addTiles(group = "OSM") %>% 
 addProviderTiles("Stamen.Terrain", group = "Stamen") %>% 
 addProviderTiles("CartoDB", group = "CartoDB") %>%
 addCircleMarkers(data = goalies, radius = 1, label = ~htmlEscape(fullname),
               color = ~pal(primaryposition), group = "G") %>%
 addCircleMarkers(data = defense, radius = 1, label = ~htmlEscape(fullname),
             color = ~pal(primaryposition), group = "D")%>%
 addCircleMarkers(data = centers, radius = 1, label = ~htmlEscape(fullname),
               color = ~pal(primaryposition), group = "C")%>%
 addCircleMarkers(data = rightwings, radius = 1, label = ~htmlEscape(fullname),
               color = ~pal(primaryposition), group = "RW")%>%
 addCircleMarkers(data = leftwings, radius = 1, label = ~htmlEscape(fullname),
               color = ~pal(primaryposition), group = "LW")%>%
 addLayersControl(baseGroups = c("OSM", "Stamen.Terrain", "CartoDB"), 
               overlayGroups = c("G", "D", "C", "LW", "RW")) %>% 
 setView(lat = 39.8282, lng = -98.5795, zoom = 2)

m4

Any tips?

1
  • Please share your data using dput(df)?
    – Quinten
    Apr 29, 2022 at 13:22

1 Answer 1

2

Please share some reproducible data next time. You can use the following code to add a title to your leaflet map:

library(leaflet)
library(htmlwidgets)
library(htmltools)

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

pal <- colorFactor(palette = c("red", "blue", "green", "yellow", "black"), 
                   levels = c("G", "D", "C", "LW", "RW"))

m <- map %>%
  addCircleMarkers(data = players, radius = 1, popup = ~fullname,
                   color = ~pal(players$nationality),
                   label = paste0(players$fullname, "(", players$nationality, ")"))

m4 <- leaflet() %>% 
  addTiles(group = "OSM") %>% 
  addProviderTiles("Stamen.Terrain", group = "Stamen") %>% 
  addProviderTiles("CartoDB", group = "CartoDB") %>%
  addCircleMarkers(data = goalies, radius = 1, label = ~htmlEscape(fullname),
                   color = ~pal(primaryposition), group = "G") %>%
  addCircleMarkers(data = defense, radius = 1, label = ~htmlEscape(fullname),
                   color = ~pal(primaryposition), group = "D")%>%
  addCircleMarkers(data = centers, radius = 1, label = ~htmlEscape(fullname),
                   color = ~pal(primaryposition), group = "C")%>%
  addCircleMarkers(data = rightwings, radius = 1, label = ~htmlEscape(fullname),
                   color = ~pal(primaryposition), group = "RW")%>%
  addCircleMarkers(data = leftwings, radius = 1, label = ~htmlEscape(fullname),
                   color = ~pal(primaryposition), group = "LW")%>%
  addLayersControl(baseGroups = c("OSM", "Stamen.Terrain", "CartoDB"), 
                   overlayGroups = c("G", "D", "C", "LW", "RW")) %>% 
  setView(lat = 39.8282, lng = -98.5795, zoom = 2) %>%
  addControl(title, position = "topleft", className="map-title")
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.