'add colorFactor palette to mapview object

The aim is to provide fixed colors to factor values. I struggle applying a colorFactor scale to a mapview object. However, the palette does not seem to match zcol.

I tried the following, similar to a leaflet map.

library(mapview)

colors <- colorFactor(palette = c("Red", "Green", "Blue"),
                      levels = c("Oberfranken","Mittelfranken", "Unterfranken"))

mapview(franconia, zcol = "district",col.regions=colors)

Mapview Output

I get the following error-message:

1: In col.regions(nregions) :   Some values were outside the color
scale and will be treated as NA

Any help on that?

the following would work on leaflet, but does not make use of mapview.

franconia %>% leaflet() %>% addTiles() %>% addPolygons(fillColor = ~colors(district))

leaflet output



Solution 1:[1]

mapview::mapviewColors seems to do the trick:

library(mapview)

colors <- mapviewColors(x=franconia,
                        zcol = "district", 
                        colors = c("Red", "Green", "Blue"),
                        at = c("Oberfranken","Mittelfranken", "Unterfranken"))

mapview(franconia, zcol = "district",col.regions = colors)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Beni