[R-sig-Geo] Colors for factors in leaflet r
Manuel Spínola
mspinola10 at gmail.com
Sun Feb 12 19:24:47 CET 2017
Thank you very much Kent.
Manuel
2017-02-12 11:53 GMT-06:00 Kent Johnson <kent3737 at gmail.com>:
> 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")
>
>
--
*Manuel Spínola, Ph.D.*
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspinola at una.cr <mspinola at una.ac.cr>
mspinola10 at gmail.com
Teléfono: (506) 8706 - 4662
Personal website: Lobito de río <https://sites.google.com/site/lobitoderio/>
Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list