rr-leaflet

How to implement labels that only refer to the current location for spot icons?


I am using the following df:

df <- structure(list(name = c("name1", "name2", "name3", "name4"), 
    postcode = c("NE6 4AG", "YO12 4HG", "N1 2SR", "CR4 3SR"), 
    type = c("type1", "type1", "type1", "type2"), lat = c(54.9849, 
    54.26435, 51.53963, 51.402), long = c(-1.56043, -0.41642, 
    -0.09786, -0.1743)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

I am getting labels for each location that contain information on all locations.

map of sites with wrong labels

How to make labels that contain information only on the single site it refers to?

I am using the following script:

library(leaflet)
library(htmlwidgets)
library(dplyr)
library(tidygeocoder)
library(leaflet.extras)  
library(htmltools)  

# Define colors for site types
site_colors <- c("type1" = "red", "type2" = "blue")

# Define custom icons for the markers
type1_icon <- makeIcon(
  iconUrl = "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-red.png",
  iconWidth = 25, iconHeight = 41,
  iconAnchorX = 12, iconAnchorY = 41
)

type2_icon <- makeIcon(
  iconUrl = "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-blue.png",
  iconWidth = 25, iconHeight = 41,
  iconAnchorX = 12, iconAnchorY = 41
)

# Split the data into two subsets
type1df <- df %>% filter(type == "type1")
type2df <- df %>% filter(type == "type2")

# Create the leaflet map
map <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap tiles
  
  # Add markers for type1 sites with the red icon
  addMarkers(
    data = type1df,
    lng = ~long,
    lat = ~lat,
    icon = type1_icon,
    label = ~htmltools::HTML(
      paste(
        "<strong>", name, "</strong><br>",
        postcode
      )
    ),
    labelOptions = labelOptions(
      noHide = TRUE,  # Makes the label always visible
      direction = "top",
      opacity = 0.8,
      textsize = "12px"
    )
  ) %>%
  
  # Add markers for type2 sites with the blue icon
  addMarkers(
    data = type2df,
    lng = ~long,
    lat = ~lat,
    icon = type2_icon,
    label = ~htmltools::HTML(
      paste(
        "<strong>", name, "</strong><br>",
        postcode
      )
    ),
    labelOptions = labelOptions(
      noHide = TRUE, 
      direction = "auto",  
      opacity = 0.8,  
      textsize = "12px"  
    )
  ) %>%
  # Add legend
  addLegend(
    "bottomright",
    colors = c("red", "blue"),
    labels = c("type1", "type2"),
    title = "Site Type"
  )

Solution

  • You can use lapply to generate the labels with the single current location information:

    label = lapply(
        paste("<strong>", type2df$name, "</strong><br>", type2df$postcode), 
        htmltools::HTML
    )
    

    enter image description here

    library(leaflet)
    
    df <- structure(list(name = c("name1", "name2", "name3", "name4"), 
                         postcode = c("NE6 4AG", "YO12 4HG", "N1 2SR", "CR4 3SR"), 
                         type = c("type1", "type1", "type1", "type2"), lat = c(54.9849, 
                                                                               54.26435, 51.53963, 51.402), long = c(-1.56043, -0.41642, 
                                                                                                                     -0.09786, -0.1743)), row.names = c(NA, -4L), class = c("tbl_df", 
                                                                                                                                                                            "tbl", "data.frame"))
    
    # Define colors for site types
    site_colors <- c("type1" = "red", "type2" = "blue")
    
    # Define custom icons for the markers
    type1_icon <- makeIcon(
      iconUrl = "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-red.png",
      iconWidth = 25, iconHeight = 41,
      iconAnchorX = 12, iconAnchorY = 41
    )
    
    type2_icon <- makeIcon(
      iconUrl = "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-blue.png",
      iconWidth = 25, iconHeight = 41,
      iconAnchorX = 12, iconAnchorY = 41
    )
    
    # Split the data into two subsets
    type1df <- df[df$type == "type1", ]
    type2df <- df[df$type == "type2", ]
    
    # Create the leaflet map
    leaflet() |> 
      addTiles() |>   # Add default OpenStreetMap tiles
      
      # Add markers for type1 sites with the red icon
      addMarkers(
        data = type1df,
        lng = ~long,
        lat = ~lat,
        icon = type1_icon,
        label = lapply(
          paste("<strong>", type1df$name, "</strong><br>", type1df$postcode), 
          htmltools::HTML
        ),
        labelOptions = labelOptions(
          noHide = TRUE,  # Makes the label always visible
          direction = "top",
          opacity = 0.8,
          textsize = "12px"
        )
      ) |> 
      
      # Add markers for type2 sites with the blue icon
      addMarkers(
        data = type2df,
        lng = ~long,
        lat = ~lat,
        icon = type2_icon,
        label = lapply(
          paste("<strong>", type2df$name, "</strong><br>", type2df$postcode), 
          htmltools::HTML
        ),
        labelOptions = labelOptions(
          noHide = TRUE, 
          direction = "auto",  
          opacity = 0.8,  
          textsize = "12px"  
        )
      ) |> 
      # Add legend
      addLegend(
        "bottomright",
        colors = c("red", "blue"),
        labels = c("type1", "type2"),
        title = "Site Type"
      )