Home > Enterprise >  Ordering legend in a given order while plotting spatvector attributes in terra
Ordering legend in a given order while plotting spatvector attributes in terra

Time:11-05

  library(geodata)
  library(terra)
  
  shp <- geodata::gadm('FRA', level = 1, path = getwd())
  shp$value <- c(1, 10, 22, 30, 44, 50, 60, 70, 80, 100)
  shp$cut_hazard <- cut(shp$value,
                        breaks = c(-Inf, 29, 69, 99,  Inf),
                        labels = c("1-29 (Low)", "30-69 (Moderate)", "70-99 (High)","100 (Very High)"),
                        include.lowest = T,
                        right = T)
  shp$cut_hazard
  # Levels: 1-29 (Low) 30-69 (Moderate) 70-99 (High) 100 (Very High)
  terra::plot(shp, "cut_hazard")

I am not able to organise the legend so that they follow an order i.e. Low, Moderate, High and Very High

enter image description here

CodePudding user response:

You can use sort = labels in plot to sort your legend according to a ordering vector:

labels <- c("1-29 (Low)", "30-69 (Moderate)", "70-99 (High)","100 (Very High)")
terra::plot(shp, "cut_hazard", sort = labels)

enter image description here

  • Related