Home > Software engineering >  levelplot not showing categorical classes on legend
levelplot not showing categorical classes on legend

Time:01-03

Trying to map a categorical raster using levelplot in R, but the legend is always showing the numeric IDs as opposed to the categorical classes. See example below. Anyone encounter this issue and know what may be the problem?

library(raster)
library(rasterVis)

r <- raster(ncol=10, nrow=10)
values(r) <- rep(1:4, each=25)

r <- ratify(r) 
rat <- levels(r)[[1]]
rat$legend <- c("Class A", "Class B", "Class C", "Class D")

levels(r) <- rat

rasterVis::levelplot(r)

Raster map using levelplot

enter image description here

CodePudding user response:

Somehow with a newer version of rasterVis the categorical legend doesn't work (Probably a bug). You could install an older version to get the following result:

library(remotes)
install_version("rasterVis", version = "0.24", repos = "http://cran.us.r-project.org")
#> Downloading package from url: http://cran.us.r-project.org/src/contrib/Archive/rasterVis/rasterVis_0.24.tar.gz

library(raster)
library(rasterVis)

r <- raster(ncol=10, nrow=10)
values(r) <- rep(1:4, each=25)

r <- raster::ratify(r) 
rat <- levels(r)[[1]]
rat$legend <- c("Class A", "Class B", "Class C", "Class D")

levels(r) <- rat

rasterVis::levelplot(r, att = "legend")

Created on 2023-01-02 with reprex v2.0.2

  • Related