2

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)

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
  • 1
    mapview developer here. This is currently not possible in mapview. So for `levelplot` to pick this up, does the column name of the levels need to be called `label`? What if it is called `labels`, would it still work? If so, how? I am happy to consider a suitable solution here because I think integer labels are not very helpful. But I am currently struggling to see a general solution. It may be best to open an issue at https://github.com/r-spatial/mapview to properly discuss this and find a proper solution. – TimSalabim Feb 26 '19 at 19:53
  • Hi @TimSalabim, I followed your suggestion and opened an issue [here](https://github.com/r-spatial/mapview/issues/214). I mentioned more details in the code posted there - to my understanding `rasterVis::levelplot()` has the `att` argument which can be used to select any variable from the 'Raster Attribute Table'. It defaults to `1L`, so first variable, which happened that I named it `label`; so can be `labels` or any accepted name. Thanks for your time and the great package! – Valentin_Ștefan Feb 26 '19 at 21:57

0 Answers0