Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering legend in a given order while plotting spatvector attributes in terra

Tags:

r

terra

  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

like image 643
89_Simple Avatar asked Sep 06 '25 03:09

89_Simple


1 Answers

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

like image 182
Maël Avatar answered Sep 08 '25 00:09

Maël