Assume a data.frame has column "group" with unique values "group1" and "group2". In leaflet, we can assign these groups a color with ColorFactor()
. We can also change markerCluster colors with minimal CSS.
How can we assign distinct colors to the markerClusters of each group? In other words, I want all marker clusters for points within "group1" to be "navy", and all marker clusters for points within "group2" to be "red" at all levels of zoom, even down to the individual points.
In a Rmd file:
---
output: html_document
---
<style>
.marker-cluster-small {
background-color: green;
}
.marker-cluster-small div {
background-color: green;
}
.marker-cluster-medium {
background-color: green;
}
.marker-cluster-medium div {
background-color: green;
}
.marker-cluster-large {
background-color: green;
}
.marker-cluster-large div {
background-color: green;
}
</style>
```{r}
library(leaflet)
library(magrittr)
quakes$group <- sample(c("group1", "group2"), 1000, replace = TRUE)
pal_group <- colorFactor(c("navy", "red"), c("group1", "group2"))
leaflet() %>%
addTiles() %>%
addCircleMarkers(
lng = quakes$long,
lat = quakes$lat,
clusterOptions = markerClusterOptions(),
color = pal_group(quakes$group)
)