Ho to make markers have different colors based on specific variable in R using leaflet?

  Kiến thức lập trình

What I want:
1- I want to make markers appear with different colors
2- my map will have radio button to control (show/hide) markers by variable ‘color_status’
3- match color scale to the color of the markers –
sample of my code is here:

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_low"), status_code = c(1, 2, 3, 4) )
library(leaflet) library(leaflet.extras)

long <- as.integer(start_stations$lng) lat <-as.integer(start_stations$lat)station_name <- start_stations$station color_satus <- start_stations$status status_code <- start_stations$status_code

data_station <- as.data.frame(cbind(long,lat,station_name,color_satus, status_code))

pal <- colorFactor(

palette = c("#5F11A3","#FF1300","#0F0065", "#F70079"), domain = viz_lost_amc$color_satus)

— character to integer ———

data_station$status_code <- as.integer(data_station$status_code) data_station$long <- as.integer(data_station$long) data_station$lat <- as.integer(data_station$lat)

groups = as.character(unique(data_station$color_satus))

getColor <- function(data_station) { sapply(data_station$status_code, function(status_code) { if(status_code == 1) { "green" } else if(status_code == 2) { "orange" } else if(status_code == 3) { "blue" } else { "red" } }) }

icons <- awesomeIcons( icon = 'ios-close', iconColor = 'black', library = 'ion', markerColor = getColor(data_station) )

map <- leaflet(data = data_station) |>addProviderTiles("OpenStreetMap.France") |> # include name of provider here setView(lng = 51.1966577  , lat = 25.3271054, zoom = 8)#

———————————————–

Loop to create control button for the marks (hide/show)

—————————————

for (g in groups) { d =  data_station[data_station$color_satus == g, ]

map =  map |> addAwesomeMarkers(data = data_station, lng = ~long, lat = ~lat, icon = icon,

popup = paste(
  "<h3 style='color:#008000'> DETAILS</h3>",
  "<b style='color:#bb042b'> Station_name:</b>",data_station$station_name,"<br>",
  "<b style='color:#bb042b'> Station_no:</b>",data_station$status_code,"<br>"
 
), 
label = ~data_station$station_name, # when user hovers,
group = g
) 

}
map <- map |> addResetMapButton()|> 
addSearchFeatures( targetGroups = g, options = searchFeaturesOptions( zoom=16, textPlaceholder = "Search station point ,,,", openPopup = TRUE, firstTipSubmit = TRUE, autoCollapse = TRUE, hideMarkerOnCollapse = TRUE )) |> 
addLegend(data = data_station,'bottomright', pal = pal, values = ~ color_satus, title = "STATUS", opacity = 0.8,group = "Leyenda") |> 
addLayersControl(overlayGroups = groups) |> 
addControl("<P><B>Hint!</B> Search for ...Placeby Place Code</P>", position='bottomleft') |> addFullscreenControl() map

I tried to figure out how to map from specific variable and make markers have different colors on the leaflet map, but it shows all coordinates are shown at one point with no effect for color function!

LEAVE A COMMENT