How to make markers have different colors based on specific variable in data frame using leaflet map?

I tired and I'm getting the result but with some colors are missing on the leaflet map. The total points are shown on the map. There is one color that is not showing from the list and same thing happens if I use more colors!
Any hints to make it work properly?

library(dplyr)
library(stringr)
library(leaflet.extras)


start_stations <-
  data.frame(
    station = c("StreeterDr", "MichiganAve", "WellsSt", "Highwayzone"),
    lat = c(25.27091982, 25.28078061, 25.2842742,25.28212071),
    lng = c(51.53083782,51.51318414,51.50223763,51.54843926),
    status = c("high", "medium", "low", "extreme"),
    status_code = c(1, 2, 3, 4)
  )


start_stations <- start_stations %>% 
  mutate(color = case_when(str_detect(status, "high") ~ "blue",
                           str_detect(status, "medium") ~ "red",
                           str_detect(status, "low") ~ "green",
                           str_detect(status, "extreme") ~ "yellow",
                           TRUE ~ "a default"))


icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = start_stations$color
)



library(leaflet)
leaflet(start_stations) %>% 
  addProviderTiles("OpenStreetMap.France") %>% 
   setView(lng = 51.1966577  , lat = 25.3271054, zoom = 8) %>%#  base coordinates
  addAwesomeMarkers(
    icon = icons,
     lng = ~lng, 
    lat = ~lat,
    label = ~start_stations$status
                   
                   )

NOTE:
I have seen a solution here in this link and followed but mine is not working!

Hi @zmn2024

the accepted answer gives a good explanation why some colors are missing.

Hi, Thank you for the tip and it indeed says a lot about this topic though it deeply discusses the solution in different IDE not R.
I got my solution using awesomeIconList ():


library(readr)
library(dplyr)
library(stringr)
)
my_data_frame <- read_csv("data.csv")
library(leaflet.extras)
markers <- awesomeIconList(
 `1` = makeAwesomeIcon(
    icon="empty",
    markerColor = "orange",
    library="fa"
  ),
  `2` = makeAwesomeIcon(
    icon="empty", 
    markerColor ="pink",
    library="fa"
  ),
  `3`  = makeAwesomeIcon(
    icon="empty", 
    markerColor ="red",
    library="fa"
  ),
  `4` = makeAwesomeIcon(
    icon="empty", 
    markerColor ="blue",
    library="fa"
  ),
 `5` = makeAwesomeIcon(
    icon="empty", 
    markerColor ="green",
    library="fa"
  ),
 `6` = makeAwesomeIcon(
    icon="empty", 
    markerColor ="black",
    library="fa"
  ),
 `7` = makeAwesomeIcon(
    icon="empty", 
    markerColor ="cadetblue",
    library="fa"
  ),
 `8` = makeAwesomeIcon(
    icon="empty", 
    markerColor ="white",
    library="fa"
  )
)

data.140 <- my_data_frame[1:140,]     # named the target rows
map <- leaflet(data = vmy_data_frame) |> # Jan and Feb  
  addProviderTiles("OpenStreetMap.France") |> # include name of provider here
  setView(lng = 51.1966577  , lat = 25.3271054, zoom = 8)# coordinates

map =  map |>
    addAwesomeMarkers(data = data.140,
    lng = ~long, 
    lat = ~lat,
    icon = ~ markers[my_data_frame$status_1], # status_1 is target coded #factor to the same numbers in awesomeIconList() function like (1,2,3,4,5,6,7,8)
label = ~my_data_frame$project_no, # when user hovers, show town,
popup = paste(
"<h3 style='color:#008000'> DETAILS</h3>",
      "<b style='color:#bb042b'> Project_no:</b>",my_data_frame$project_no,"<br>"
)
)
map

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.