1

I created a leaflet map and now need to customize the order of the legend. By default leaflet alphabetizes the legend order. In the example below, I need the legend order to be (from top to bottom): Charlottesville, Richmond, Charlotte, Raleigh. How can I customize the leaflet legend order?

Example:

# Import needed libraries
library(tidyverse)
library(leaflet)

# Create example dataset
aa <- data.frame(
  city = c('Richmond','Charlottesville', 'Raleigh', 'Charlotte'),
  lat = c(37.53,38.01,35.78,35.22),
  lon = c(-77.44,-78.47,-78.63,-80.84))

# Create custom colors for markers
pal <- leaflet::colorFactor(c('springgreen', 'dodgerblue', 'red', 'purple'),
                            domain = c('Richmond','Charlottesville', 'Raleigh', 'Charlotte'),
                            ordered = TRUE)

# Make map
aa %>%
  leaflet(options = leafletOptions(attributionControl = F,
                                   zoomControl = F)) %>%
  addTiles() %>%
  addProviderTiles("Esri.WorldImagery") %>%
  setView(-78.47,
          36.53,
          zoom = 7) %>%
  addCircleMarkers(
    lng = aa$lon,
    lat = aa$lat,
    label = aa$city,
    fillColor = pal(aa$city),
    fillOpacity = 1,
    color = "black",
    stroke = TRUE,
    weight = 2,
    radius = 5) %>%
  addLegend('topright', pal = pal, values = aa$city, title = 'City', opacity = 1)

enter image description here

  • Here is a link to a similar SO question that I could not get to work

UPDATE:

Following the answer from the similar SO question linked above, I was able to reorder the legend however, now the legend colors do not match the marker color on the map.

For example, in the legend below Charlotte is red but on the map Charlotte is purple (i.e., the legend color for Charlotte should be purple). Need to be able to custom reorder the legend and keep the colors set appropriately.

# Create a new grouping variable
ord <- factor(aa$city, labels = c('Charlottesville', 'Richmond', 'Charlotte', 'Raleigh'))

# Create custom colors for markers
pal <- colorFactor(c('springgreen', 'dodgerblue', 'red', 'purple'),
                            levels = ord, ordered = TRUE)

# Make map
aa %>%
  leaflet(options = leafletOptions(attributionControl = F,
                                   zoomControl = F)) %>%
  addTiles() %>%
  addProviderTiles("Esri.WorldImagery") %>%
  setView(-78.47,
          36.53,
          zoom = 7) %>%
  addCircleMarkers(
    lng = aa$lon,
    lat = aa$lat,
    label = aa$city,
    fillColor = pal(ord),
    fillOpacity = 1,
    color = "black",
    stroke = TRUE,
    weight = 2,
    radius = 5) %>%
  addLegend('topright', pal = pal, values = ord, title = 'City', opacity = 1)

^ Legend colors do not match map colors (i.e. Charlotte is purple on map but red in legend).

2
  • The answer to the question you link to suggests converting vectors into ordered factors, which should work. What kind of problem did you have with it?
    – Cloudberry
    Commented Dec 12, 2023 at 21:06
  • @Cloudberry I added an update showing the issue I had when following the answer from the similar question I linked to.
    – tassones
    Commented Dec 12, 2023 at 21:39

1 Answer 1

1

You need to specify the levels argument of factor not the labels argument:

ord <- factor(aa$city, levels = c('Charlottesville', 'Richmond', 'Charlotte', 'Raleigh'))

# Create custom colors for markers
pal <- colorFactor(c('springgreen', 'dodgerblue', 'red', 'purple'), levels = ord, ordered = TRUE)

Map with markers properly colored

2
  • 1
    this answer worked once I added levels = ord, ordered = TRUE to pal. Consider editing your answer to reflect the need for these arguments.
    – tassones
    Commented Dec 13, 2023 at 14:01
  • 1
    ouch, i had it in my answer, (apparently, see the screenshot) don't know why it dropped while copying. +1
    – thothal
    Commented Dec 14, 2023 at 8:14

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