I think you want this:

pal <- colorFactor(palette = c("red", "blue", "green"), levels =
levels(meuse$soil))
leaflet(meuse) %>%
  addTiles() %>%
  addCircleMarkers(stroke = FALSE, fillOpacity = 1, color = ~pal(soil)) %>%
  addLegend("topright", pal, values=~soil, labels=c("Type 1", "Type 2",
"Type 3"), title="Soil type")

Kent


> From: Manuel Sp?nola <mspinol...@gmail.com>
> To: "r-sig-geo@r-project.org" <r-sig-geo@r-project.org>
> Subject: [R-sig-Geo] Colors for factors in leaflet r
>
> I am trying to add a legend to a leaflet map but I got the following error
>
> data(meuse)
> coordinates(meuse) <- ~x+y
> proj4string(meuse) <- CRS("+init=epsg:28992")
> WGS84 <- CRS("+proj=longlat +ellps=WGS84")
> meuse <- spTransform(meuse, WGS84)
>
> pal <- colorFactor(palette = c("red", "blue", "green"), levels =
> meuse$soil)
> meuse$soil
> leaflet(meuse) %>%
>   addTiles() %>%
>   addCircleMarkers(stroke = FALSE, fillOpacity = 1) %>%
>   addLegend("topright", color = ~pal(soil), labels=c("Type 1", "Type 2",
> "Type 3"), title="Soil type")

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to