'leaflet: different colors of addMarkers for each group/layer
I am trying to replicate the answer to this question, but with a) addMarkers instead of addCircleMarkers, and b) with a distinct color for each group. Below my reprex. Unfortunately, I am struggling to get different colors for each group. Grateful for any hint. Many thanks.
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 4.1.3
library(leaflet)
#> Warning: package 'leaflet' was built under R version 4.1.3
library(RColorBrewer)
df3 <- structure(list(id = 1:20, groupID = c(
277698L, 277715L, 277704L,
277706L, 277705L, 277705L, 277719L, 277705L, 277705L, 277709L,
277706L, 277705L, 277704L, 277706L, 277715L, 277702L, 277719L,
277706L, 277715L, 277706L
), lat = c(
-10.8172615660655, -10.8904055150991,
-10.8887597563482, -10.90203509, -10.9001514, -10.8997748900025,
-10.8960177351343, -10.8896179908615, -10.8991450456802, -10.9224848475651,
-10.9000373151094, -10.8905013650562, -10.8889438100208, -10.9001234797436,
-10.8861897462579, -10.9326053452642, -10.8916601751623, -10.902075281944,
-10.8822231928033, -10.9079483812524
), lon = c(
-36.9248145687343,
-37.0665064455395, -37.0921721937304, -37.05829295, -37.0969278,
-37.0976847916125, -37.0840372102666, -37.0963566353117, -37.0945971936751,
-37.0549293249471, -37.066113628594, -37.0940632483155, -37.095505683692,
-37.0590422449149, -37.0782556623101, -37.0698746017798, -37.0841003949028,
-37.0593363285999, -37.0724709841895, -37.0817244836096
)), .Names = c(
"id",
"groupID", "lat", "lon"
), row.names = c(
20L, 23L, 8L, 36L, 14L,
13L, 16L, 2L, 11L, 1L, 26L, 6L, 5L, 31L, 22L, 50L, 17L, 34L,
25L, 42L
), class = "data.frame")
groups <- unique(df3$groupID)
length(unique(df3$groupID))
#> [1] 8
vec_cols <- brewer.pal(n = length(unique(df3$groupID)), name = "RdBu")
names(vec_cols) <- unique(df3$groupID)
fn_marker_color <- function(x) {
vec_cols <- brewer.pal(n = length(unique(df3$groupID)), name = "RdBu")
names(vec_cols) <- unique(df3$groupID)
vec_cols[x]
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = ~fn_marker_color(g)
)
map <- leaflet(df3) %>% addTiles(group = "OpenStreetMap")
for (g in groups) {
d <- df3[df3$groupID == g, ]
map <- map %>%
addMarkers(
data = d, lng = ~lon, lat = ~lat,
icon=icons,
group = g
)
}
map %>% addLayersControl(overlayGroups = groups)
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
#> Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

Created on 2022-04-20 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
