I would appreciate help with finding a solution to display the custom legend labels of a categorical raster when plotting with mapview
. Below is a reproducible example - I wish to have the same legend labels as in the plot made with rasterVis::levelplot
(labels: "a", "b", "c"). I did not see anything in the help of mapView
(section S4 method for signature 'RasterLayer') to cover this, or maybe I'm missing something obvious.
library(raster)
#> Loading required package: sp
library(rasterVis)
#> Loading required package: lattice
#> Loading required package: latticeExtra
#> Loading required package: RColorBrewer
library(mapview)
library(RColorBrewer)
# Convert from continous raster to categorical one
my_rst <- reclassify(poppendorf[[5]], rcl = c(0.5*10^4, 1.5*10^4, 1,
1.5*10^4, 1.7*10^4, 2,
1.7*10^4, 2.5*10^4, 3))
my_rst <- ratify(my_rst)
# Add custom label for each class
levels(my_rst)[[1]]$label <- letters[1:3]
levels(my_rst)
#> [[1]]
#> ID label
#> 1 1 a
#> 2 2 b
#> 3 3 c
# Custom palette
my_palette <- brewer.pal(n = 3, name = "Dark2")
my_palette
#> [1] "#1B9E77" "#D95F02" "#7570B3"
# Plot
levelplot(my_rst, col.regions = my_palette)
mapView(my_rst, col.regions = my_palette)
Created on 2019-02-26 by the reprex package (v0.2.1)