How to reorder facets in tmap r?
### How to reorder in `tmap` R?
When using `tmap` (v3) to reorder SpatRaster layers through subsetting, you might encounter issues with layer names. Here's a workaround:
**1. Sort by Dates**
Sort the layers by their encoded dates using `tidyverse` functions:
```html
library(tidyverse)
file_list <- c("RF_04.Apr.2022_LL", "RF_13.Dec.2021_LL", "RF_15.Feb.2022_LL", "RF_17.Dec.2020_LL", "RF_18.Nov.2021_LL", "RF_19.Feb.2021_LL")
# extract dates, use file_list vector for names
date_list <-
file_list |>
stringr::str_extract("(?<=_).*(?=_)") |>
lubridate::dmy() |>
setNames(file_list)
# sort by dates, extract names
(file_list_sorted <- sort(date_list) |> names())
```
**2. Import and Rename Rasters**
Import the rasters using `rast()`, then modify their names to include only the date values:
```html
# import rasters
x = rast(file_list_sorted)
# alter names to only keep date values, part between _ & _
names(x) <- names(x) |> stringr::str_extract("(?<=_).*(?=_)")
```
**3. Continue with `tmap`**
Proceed with `tmap` operations using the modified raster names. The layers should now be ordered chronologically.