[R-sig-Geo] Colors for factors in leaflet r

Kent Johnson kent3737 at gmail.com
Sun Feb 12 18:53:32 CET 2017


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 <mspinola10 at gmail.com>
> To: "r-sig-geo at r-project.org" <r-sig-geo at 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]]



More information about the R-sig-Geo mailing list