Skip to contents

The optimTFE package is a spatial conservation prioritization (SCP) tool created to help conservation managers identify priority recovery areas, represented by planning units, for species recovery planning efforts.

This vignette will demonstrate how to bring in data for use in the optimTFE algorithm to generate conservation solutions, which we refer to as conservation footprints. It will then provide examples of how to generate relevant summary statistics in order to explore conservation footprints and tradeoffs.

Project Setup

The optimTFE package includes an example dataset that we will use for the following examples. First, we will load the package and define our project directory. For demonstration purposes, we will create an ephemeral temporary directory, projectDir. In a real project you would use the root of your project directory and optimTFE will use the current working directory by default if not specified. The run_id (default = “optimTFE”) will be used as a subdirectory to store outputs for the current optimTFE run, allowing multiple runs to be organized within a single project.

library(optimTFE)
library(dplyr)
library(ggplot2)
projectDir <- tempdir()
run_id <- "optimTFE"

Minimally, optimTFE must be provided with 2 required inputs. The first input specifies the required targets for each species (i.e., the number of desired planning units per species). It can be provided as a file path to a csv, where the first column contains the species names and the second column specifies the total species-specific targets (note: species names must not contain spaces or special characters). Alternatively, the species targets can be provided as a pre-loaded R data.frame or matrix, with the species names set as the row names, not in a separate column. The optimTFE package includes an example data set of 30 species and an example targets data frame (with total targets in the first column) that can be used for testing.

example_targets[1:10, 1, drop = FALSE]
##       total
## sp_1     10
## sp_2     10
## sp_3     10
## sp_4     10
## sp_5     10
## sp_6     10
## sp_7     10
## sp_8     10
## sp_9     10
## sp_10    10

The species suitability can be provided as a file path to a csv with species names in the first column or a preloaded R data.frame or matrix with species names set as row names. The species names provided in the suitability matrix must match those provided in the targets input. Values in the suitability matrix should fall between 0 and 1 (inclusive) and binary values can be used when data on relative suitability across units are not available. The example suitability matrix included in the package includes data for the 30 species across a hexagonal grid of 795 planning units.

example_suitability[1:10, 1:5]
##                sp_1       sp_2        sp_3       sp_4      sp_5
## unit_1  0.001960665 0.02733608 0.001481227 0.03737422 0.2208343
## unit_2  0.003175365 0.02936978 0.001696577 0.03857623 0.2552939
## unit_3  0.005038974 0.03094247 0.001817711 0.03857623 0.2942858
## unit_4  0.007789768 0.03193833 0.002473365 0.03737422 0.3378808
## unit_5  0.011641326 0.03227946 0.004573422 0.04644064 0.3857163
## unit_6  0.016651109 0.03193833 0.008351537 0.06049417 0.4366035
## unit_7  0.022513833 0.03094247 0.014980556 0.07776645 0.4878725
## unit_8  0.028366253 0.02936978 0.026168931 0.09823579 0.5344800
## unit_9  0.032829771 0.02733608 0.043890287 0.12123203 0.5685397
## unit_10 0.034517178 0.02497801 0.069019582 0.14505001 0.5812752

Running optimTFE

We can then run the minimal example, which will generate output files prefixed with the run_id in the specified directory, out_dir.

optimTFE(
  targets = example_targets[, 1, drop = FALSE],
  suitability = example_suitability,
  min_spp_suit_score = 0.1, # Set suitability scores lower than 0.1 to 0
  rand_tolerance = 5, # Range of species richness to consider at each step
  n = 10000, # number of solutions to generate
  out_dir = projectDir,
  run_id = run_id,
  output_csv = TRUE,
  cores = 8, # number of processing threads to use
  seed = 101010 # Seed for reproducible output
)
## Beginning optimTFE...
## Species targets loaded: 30
## Species suitability matrix loaded: 795 planning units
## Generating 10000 solutions in 16 batches
## Finished generating 10000 solutions in 7.8 secs

Output files generated by optimTFE:

list.files(projectDir, recursive = TRUE)
##  [1] "file24bd19471ca3"                                               
##  [2] "file24bd364d8026"                                               
##  [3] "file24bd3c23bc79"                                               
##  [4] "file24bd4095ba6b"                                               
##  [5] "file24bd663c5732"                                               
##  [6] "optimTFE/optimTFE.meta"                                         
##  [7] "optimTFE/optimTFE.n_units.pdf"                                  
##  [8] "optimTFE/optimTFE.solutions.csv"                                
##  [9] "optimTFE/optimTFE.summary.csv"                                  
## [10] "optimTFE/solutions/009c0613-2563-4199-a023-27be6a6d0591.parquet"
## [11] "optimTFE/solutions/060a8b13-a3ae-447b-8322-3b6d8970c820.parquet"
## [12] "optimTFE/solutions/0a9eae4f-0a56-45b6-9f07-75c0e2baaba9.parquet"
## [13] "optimTFE/solutions/3420a322-00bf-4a5c-9da5-ec4b4b62520a.parquet"
## [14] "optimTFE/solutions/486b299d-04a0-4a2b-844c-cbc6ef8673e7.parquet"
## [15] "optimTFE/solutions/5e6a4dcc-113b-408e-b892-4736fb2ecf7a.parquet"
## [16] "optimTFE/solutions/814eb5f5-0ed3-4b16-b438-6770ef468d0b.parquet"
## [17] "optimTFE/solutions/8f8eb351-003a-41e3-ac63-27da825d68c5.parquet"
## [18] "optimTFE/solutions/99d3d558-e65c-4525-9859-fc8ed4bbb621.parquet"
## [19] "optimTFE/solutions/9f2b0804-b27e-4369-970f-f1cbb8da0940.parquet"
## [20] "optimTFE/solutions/a832832e-99ba-4fd2-9332-20bc6ff1d13f.parquet"
## [21] "optimTFE/solutions/ab5faf41-3674-4ea1-9660-2b0463449294.parquet"
## [22] "optimTFE/solutions/b79bbaf8-9848-41e4-8fc5-3f6618c86d2d.parquet"
## [23] "optimTFE/solutions/bc2ce0b7-8b2c-4028-9036-2825e62d130e.parquet"
## [24] "optimTFE/solutions/dad0b5ed-c50e-4b99-bedb-7af9db64aabd.parquet"
## [25] "optimTFE/solutions/ea82fb2f-bd16-4e51-a292-8a501f91e029.parquet"
## [26] "rmarkdown-str24bd4e7bc5fd.html"

Input Validation

The optimTFE algorithm will attempt to validate all inputs to ensure that inputs are in the proper format, that the species names and planning unit ids are consistent across inputs, and that the requested targets can be met. A dry_run parameter is available to validate inputs without running the algorithm:

impossible_targets <- example_targets[, 1, drop = FALSE]
impossible_targets[1:5, 1] <- 1000
optimTFE(
  targets = impossible_targets,
  suitability = example_suitability,
  dry_run = TRUE
)
## Beginning optimTFE...
## Species targets loaded: 30
## Species suitability matrix loaded: 795 planning units
## Not enough units to meet all targets:
## --   sp_1-global
## --   sp_2-global
## --   sp_3-global
## --   sp_4-global
## --   sp_5-global

Progress Updates

When generating a large number of solutions it can be helpful to have an indicator bar showing progress of the algorithm. optimTFE uses the package {progressr} to provide progress notification. You can activate progress bars for optimTFE by either turnning on the global progress handler once at the beginning of your R session:

progressr::handlers(global = TRUE)

Or you can explicitly wrap your individual function calls:

progressr::with_progress({
  optimTFE(...)
})

Either method will display a progress bar for long running calls to optimTFE. However, because optimTFE batches solution generation for efficiency, progress updated will occur at the batch level. As a result, if the number of batches in not significantly greater than the number of processing cores in use, updated will be minimal.

Optional Inputs

Known Populations

When there are known populations that should be prioritized for conservation targets, an additional populations input can be provided. Known populations can be provided as a file path to a csv in one of 2 formats. The file should either be in long-format, with 3 columns, unit_id, species, and population, or wide format, with columns for each species and one row per unit_id (with unit_id in the first column). Alternatively, the populations input can be provided directly as a wide format R matrix with unit_ids for row names and species names as column names. In the example populations data, viewing the first 10 planning units for the first 10 species shows single known population, “F”, in unit_5 for sp_5:

example_populations[1:10, 1:10]
##         sp_1 sp_2 sp_3 sp_4 sp_5 sp_6 sp_7 sp_8 sp_9 sp_10
## unit_1  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_2  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_3  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_4  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_5  NA   NA   NA   NA   "F"  NA   NA   NA   NA   NA   
## unit_6  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_7  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_8  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_9  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   
## unit_10 NA   NA   NA   NA   NA   NA   NA   NA   NA   NA

When provided to the optimTFE algorithm, species populations will always be preferentially selected and populations with suitability scores below the threshold (min_spp_suitability) will not be excluded.

Subregions

When explicit minimum numbers of targets are required within one or more subregions of the larger conservation area, subregion inputs can be provided. The subregion input should consist of a path to a csv with unit_id in the first column and a binary indicator in the remaining columns showing which region each planning unit occurs in. Alternatively, an R data.frame or matrix with unit_ids assigned to the row names can be provided. Planning units not assigned to a region will be assigned to a ‘default’ region at run time. For the first 10 planning units of the example data, units 1:7 will be placed in the default region, while units 8:10 occur in the ‘left’ region:

example_subregions[1:10, ]
##         left right
## unit_1     0     0
## unit_2     0     0
## unit_3     0     0
## unit_4     0     0
## unit_5     0     0
## unit_6     0     0
## unit_7     0     0
## unit_8     1     0
## unit_9     1     0
## unit_10    1     0

When subregions are included, the minimum subregion targets should be provided in the targets input, with the extra column names matching those in the subregions file. Here, we can see the minimum species targets required in the “left” and “right” subregions for the first 10 species in the example data:

example_targets[1:10, ]
##       total left right
## sp_1     10    0     0
## sp_2     10    0     0
## sp_3     10    4     3
## sp_4     10    1     5
## sp_5     10    3     4
## sp_6     10    2     3
## sp_7     10    1     1
## sp_8     10    0     0
## sp_9     10    3     1
## sp_10    10    3     3

Species Incompatibilities

When species with overlapping ranges must not be assigned to the same conservation planning units (e.g., due to the potential for undesired hybridization), a species incompatibility matrix can be provided. This input does not need to include all species (only those with incompatibilities) but must be a symmetric binary matrix with species names defining rows and columns with 1 indicating an incompatibility. Here we can see the incompatibilities between a few of the first 10 species in the example data:

example_incompatibility[1:10, 1:10]
##       sp_1 sp_2 sp_3 sp_4 sp_5 sp_6 sp_7 sp_8 sp_9 sp_10
## sp_1     0    0    0    0    0    0    0    0    0     0
## sp_2     0    0    0    0    0    0    0    0    0     0
## sp_3     0    0    0    0    0    0    0    0    0     0
## sp_4     0    0    0    0    1    0    0    0    0     0
## sp_5     0    0    0    1    0    0    0    0    0     0
## sp_6     0    0    0    0    0    0    0    0    0     0
## sp_7     0    0    0    0    0    0    0    0    0     0
## sp_8     0    0    0    0    0    0    0    0    0     0
## sp_9     0    0    0    0    0    0    0    0    0     0
## sp_10    0    0    0    0    0    0    0    0    0     0

Exploring Results

By default, the complete details of all solutions will be saved as a collection of .parquet files in the solutions subdirectory, but can also be saved as a single csv file with the output_csv = T option. However, generating a very large number of solutions can result in large file sizes and it will most often be more efficient to query the parquet files using the {arrow} package, which provide efficient data filtering and loading without requiring the entire output to be read into memory. The optimTFE package includes some helper function to simplify this process. For, example, the get_solutions() function can be used to import one or more solutions from the output. For example, to extract the first 10 solutions:

sols_1_to_10 <- extract_solutions(
  solutions = 1:10,
  dir = projectDir,
  run_id = run_id
)
sols_1_to_10[1:10, ]
## # A tibble: 10 × 36
##    solution order unit_id  passing richness suitability  sp_1  sp_2  sp_3  sp_4
##       <dbl> <dbl> <chr>      <dbl>    <dbl>       <dbl> <dbl> <dbl> <dbl> <dbl>
##  1        1     1 unit_454       1       22       0.434     0     1     1     1
##  2        1     2 unit_294       1       25       0.339     1     1     0     1
##  3        1     3 unit_191       1       25       0.556     1     0     1     1
##  4        1     4 unit_279       1       25       0.386     1     1     0     1
##  5        1     5 unit_87        1       23       0.459     0     0     1     1
##  6        1     6 unit_364       1       23       0.280     0     1     1     1
##  7        1     7 unit_323       1       23       0.288     1     1     0     1
##  8        1     8 unit_371       1       21       0.350     1     1     0     1
##  9        1     9 unit_440       1       22       0.397     0     1     1     1
## 10        1    10 unit_70        1       23       0.424     0     0     1     1
## # ℹ 26 more variables: sp_5 <dbl>, sp_6 <dbl>, sp_7 <dbl>, sp_8 <dbl>,
## #   sp_9 <dbl>, sp_10 <dbl>, sp_11 <dbl>, sp_12 <dbl>, sp_13 <dbl>,
## #   sp_14 <dbl>, sp_15 <dbl>, sp_16 <dbl>, sp_17 <dbl>, sp_18 <dbl>,
## #   sp_19 <dbl>, sp_20 <dbl>, sp_21 <dbl>, sp_22 <dbl>, sp_23 <dbl>,
## #   sp_24 <dbl>, sp_25 <dbl>, sp_26 <dbl>, sp_27 <dbl>, sp_28 <dbl>,
## #   sp_29 <dbl>, sp_30 <dbl>

A condensed summary of the output, with one row per solution and some summary statistics, will be saved in the {run_id}.summary.csv file. This file will include the solution number, the number of units selected for the solution, the mean suitability score for species in their selected planning units, the mean and max species richness, whether the solution met all specified requirements (1 = yes) and the list of planning units.

(summary_fn <- file.path(projectDir, run_id, paste0(run_id, ".summary.csv")))
## [1] "/tmp/Rtmpyvg5pj/optimTFE/optimTFE.summary.csv"
sol_summary <- read.csv(summary_fn)
sol_summary[1:10, ]
##    solution n_units mean_suitability mean_richness max_richness passing
## 1         1      30        0.3665954     10.000000           25       1
## 2         2      30        0.4033268     10.000000           25       1
## 3         3      32        0.4318763      9.375000           25       1
## 4         4      29        0.4048654     10.344828           26       1
## 5         5      33        0.3668606      9.090909           25       1
## 6         6      29        0.3597939     10.344828           26       1
## 7         7      32        0.4004126      9.375000           26       1
## 8         8      35        0.3773514      8.571429           25       1
## 9         9      31        0.3777751      9.677419           26       1
## 10       10      31        0.3895276      9.677419           25       1
##                                                                                                                                                                                                                                                                                                                                                                                             units
## 1                                                           ["unit_11","unit_172","unit_190","unit_191","unit_247","unit_279","unit_28","unit_294","unit_323","unit_337","unit_364","unit_371","unit_39","unit_401","unit_41","unit_440","unit_445","unit_454","unit_482","unit_515","unit_534","unit_572","unit_619","unit_631","unit_633","unit_636","unit_649","unit_70","unit_708","unit_87"]
## 2                                                        ["unit_118","unit_120","unit_127","unit_130","unit_14","unit_161","unit_208","unit_223","unit_235","unit_295","unit_308","unit_322","unit_337","unit_353","unit_381","unit_410","unit_446","unit_456","unit_514","unit_529","unit_531","unit_559","unit_574","unit_588","unit_589","unit_664","unit_678","unit_708","unit_82","unit_86"]
## 3                                  ["unit_101","unit_129","unit_130","unit_189","unit_191","unit_222","unit_237","unit_291","unit_30","unit_325","unit_326","unit_387","unit_396","unit_402","unit_412","unit_456","unit_469","unit_513","unit_515","unit_559","unit_59","unit_605","unit_621","unit_653","unit_664","unit_679","unit_684","unit_698","unit_712","unit_725","unit_739","unit_86"]
## 4                                                                 ["unit_102","unit_103","unit_147","unit_159","unit_195","unit_206","unit_210","unit_217","unit_234","unit_236","unit_277","unit_297","unit_384","unit_415","unit_439","unit_474","unit_483","unit_486","unit_497","unit_512","unit_518","unit_528","unit_573","unit_652","unit_665","unit_679","unit_681","unit_694","unit_86"]
## 5                     ["unit_101","unit_115","unit_134","unit_148","unit_149","unit_194","unit_209","unit_217","unit_220","unit_237","unit_238","unit_295","unit_328","unit_329","unit_386","unit_398","unit_411","unit_412","unit_424","unit_425","unit_499","unit_506","unit_518","unit_527","unit_531","unit_572","unit_589","unit_617","unit_649","unit_651","unit_682","unit_721","unit_73"]
## 6                                                                 ["unit_101","unit_113","unit_115","unit_118","unit_149","unit_236","unit_238","unit_266","unit_278","unit_322","unit_340","unit_353","unit_365","unit_385","unit_400","unit_425","unit_457","unit_528","unit_530","unit_586","unit_588","unit_632","unit_651","unit_664","unit_722","unit_753","unit_767","unit_768","unit_87"]
## 7                                 ["unit_11","unit_131","unit_160","unit_162","unit_175","unit_194","unit_267","unit_281","unit_283","unit_295","unit_299","unit_306","unit_326","unit_332","unit_363","unit_423","unit_430","unit_458","unit_474","unit_545","unit_557","unit_571","unit_573","unit_587","unit_594","unit_601","unit_622","unit_648","unit_68","unit_694","unit_711","unit_730"]
## 8  ["unit_10","unit_115","unit_133","unit_163","unit_208","unit_210","unit_222","unit_247","unit_281","unit_286","unit_308","unit_310","unit_321","unit_328","unit_354","unit_384","unit_40","unit_408","unit_41","unit_410","unit_438","unit_486","unit_501","unit_516","unit_56","unit_573","unit_574","unit_576","unit_592","unit_594","unit_617","unit_633","unit_635","unit_751","unit_753"]
## 9                                              ["unit_10","unit_11","unit_117","unit_162","unit_178","unit_179","unit_187","unit_223","unit_249","unit_297","unit_326","unit_381","unit_383","unit_386","unit_396","unit_424","unit_427","unit_456","unit_497","unit_592","unit_604","unit_665","unit_677","unit_694","unit_70","unit_701","unit_710","unit_723","unit_724","unit_784","unit_86"]
## 10                                            ["unit_116","unit_119","unit_131","unit_148","unit_164","unit_176","unit_187","unit_203","unit_209","unit_224","unit_269","unit_311","unit_337","unit_367","unit_415","unit_426","unit_468","unit_483","unit_486","unit_546","unit_55","unit_56","unit_602","unit_606","unit_634","unit_663","unit_679","unit_71","unit_710","unit_738","unit_789"]

The summary data can be use to find the range of footprint size across all solutions and extract a subset. In this case, will will plot the distribution of the number of units selected per footprint:

sol_summary |>
  dplyr::summarise(
    Frequency = dplyr::n(),
    .by = n_units
  ) |>
  ggplot(aes(x = n_units, y = Frequency)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  theme(
    panel.background = element_blank(),
    plot.title = element_text(hjust = 0.5),
    axis.text.x = element_text(angle = 45, hjust = 1)
  ) +
  labs(
    title = "Planning unit counts across all solutions",
    x = "Planning unit count",
    y = "Frequency"
  )

Then we can use the helper function, extract_solutions() to extract the solution data for the smallest 10% for footprints base on the n_units summary attribute:

min_10_pct <- quantile(sol_summary$n_units, probs = 0.1)
min_10_pct
## 10% 
##  28
sols_1_to_10 <- extract_solutions(
  solutions = which(sol_summary$n_units <= min_10_pct),
  dir = projectDir,
  run_id = run_id
)

Spatial Data

Spatial data can be added to the solution summary output using the function spatial_stats(). The spatial input to this function should be a path to an ESRI .shp file or .gpkg or a preloaded spatial data frame. In each case, the unique planning unit ids must be included in the first column of the attribute table. The function will return the input summary data frame with 3 new columns: area, perrimeter, and their ratio (pa_ratio).

sol_summary_spatial <- spatial_stats(
  data = sol_summary,
  spatial = example_spatial
)
## Summarizing spatial data for 10000 solutions...
sol_summary_spatial[1:10, c(1, 7:10)]
##    solution     area perimeter pa_ratio
## 1         1 103.9230  189.3709 1.822222
## 2         2 103.9230  187.0615 1.800000
## 3         3 110.8513  205.5367 1.854167
## 4         4 100.4589  187.0615 1.862069
## 5         5 114.3154  221.7025 1.939394
## 6         6 100.4589  191.6803 1.908046
## 7         7 110.8513  200.9179 1.812500
## 8         8 121.2436  217.0837 1.790476
## 9         9 107.3872  200.9179 1.870968
## 10       10 107.3872  198.6085 1.849462
##                                                                                                                                                                                                                                                                                                                                                                                             units
## 1                                                           ["unit_11","unit_172","unit_190","unit_191","unit_247","unit_279","unit_28","unit_294","unit_323","unit_337","unit_364","unit_371","unit_39","unit_401","unit_41","unit_440","unit_445","unit_454","unit_482","unit_515","unit_534","unit_572","unit_619","unit_631","unit_633","unit_636","unit_649","unit_70","unit_708","unit_87"]
## 2                                                        ["unit_118","unit_120","unit_127","unit_130","unit_14","unit_161","unit_208","unit_223","unit_235","unit_295","unit_308","unit_322","unit_337","unit_353","unit_381","unit_410","unit_446","unit_456","unit_514","unit_529","unit_531","unit_559","unit_574","unit_588","unit_589","unit_664","unit_678","unit_708","unit_82","unit_86"]
## 3                                  ["unit_101","unit_129","unit_130","unit_189","unit_191","unit_222","unit_237","unit_291","unit_30","unit_325","unit_326","unit_387","unit_396","unit_402","unit_412","unit_456","unit_469","unit_513","unit_515","unit_559","unit_59","unit_605","unit_621","unit_653","unit_664","unit_679","unit_684","unit_698","unit_712","unit_725","unit_739","unit_86"]
## 4                                                                 ["unit_102","unit_103","unit_147","unit_159","unit_195","unit_206","unit_210","unit_217","unit_234","unit_236","unit_277","unit_297","unit_384","unit_415","unit_439","unit_474","unit_483","unit_486","unit_497","unit_512","unit_518","unit_528","unit_573","unit_652","unit_665","unit_679","unit_681","unit_694","unit_86"]
## 5                     ["unit_101","unit_115","unit_134","unit_148","unit_149","unit_194","unit_209","unit_217","unit_220","unit_237","unit_238","unit_295","unit_328","unit_329","unit_386","unit_398","unit_411","unit_412","unit_424","unit_425","unit_499","unit_506","unit_518","unit_527","unit_531","unit_572","unit_589","unit_617","unit_649","unit_651","unit_682","unit_721","unit_73"]
## 6                                                                 ["unit_101","unit_113","unit_115","unit_118","unit_149","unit_236","unit_238","unit_266","unit_278","unit_322","unit_340","unit_353","unit_365","unit_385","unit_400","unit_425","unit_457","unit_528","unit_530","unit_586","unit_588","unit_632","unit_651","unit_664","unit_722","unit_753","unit_767","unit_768","unit_87"]
## 7                                 ["unit_11","unit_131","unit_160","unit_162","unit_175","unit_194","unit_267","unit_281","unit_283","unit_295","unit_299","unit_306","unit_326","unit_332","unit_363","unit_423","unit_430","unit_458","unit_474","unit_545","unit_557","unit_571","unit_573","unit_587","unit_594","unit_601","unit_622","unit_648","unit_68","unit_694","unit_711","unit_730"]
## 8  ["unit_10","unit_115","unit_133","unit_163","unit_208","unit_210","unit_222","unit_247","unit_281","unit_286","unit_308","unit_310","unit_321","unit_328","unit_354","unit_384","unit_40","unit_408","unit_41","unit_410","unit_438","unit_486","unit_501","unit_516","unit_56","unit_573","unit_574","unit_576","unit_592","unit_594","unit_617","unit_633","unit_635","unit_751","unit_753"]
## 9                                              ["unit_10","unit_11","unit_117","unit_162","unit_178","unit_179","unit_187","unit_223","unit_249","unit_297","unit_326","unit_381","unit_383","unit_386","unit_396","unit_424","unit_427","unit_456","unit_497","unit_592","unit_604","unit_665","unit_677","unit_694","unit_70","unit_701","unit_710","unit_723","unit_724","unit_784","unit_86"]
## 10                                            ["unit_116","unit_119","unit_131","unit_148","unit_164","unit_176","unit_187","unit_203","unit_209","unit_224","unit_269","unit_311","unit_337","unit_367","unit_415","unit_426","unit_468","unit_483","unit_486","unit_546","unit_55","unit_56","unit_602","unit_606","unit_634","unit_663","unit_679","unit_71","unit_710","unit_738","unit_789"]

Spatial data can also be used to generate a map of a given solution. In this example we generate a map of one of the footprints with lowest total area (smallest 5%) and lowest perrimeter to area ratio, showing the species richness for each selected unit:

# Find a minimal solution
min_solution <- sol_summary_spatial |>
  filter(area <= quantile(area, probs = 0.05)) |>
  arrange(pa_ratio) |>
  slice(1) |>
  pull(solution) |>
  extract_solutions(
    solutions = _,
    dir = projectDir,
    run_id = run_id
  )

# Join with spatial data and plot
left_join(
  example_spatial,
  min_solution,
  by = "unit_id"
) |>
  ggplot(aes(fill = richness)) +
  geom_sf(color = "grey") +
  scale_fill_viridis_c(
    direction = -1,
    na.value = "white",
    name = "Richness"
  ) +
  theme_void() +
  labs(title = "Minimal Footprint") +
  theme(plot.title = element_text(hjust = 0.5))

Or, to create a map of planning unit selection frequency across all solutions:

# Calculate unit frequency
unit_frequency <- extract_solutions(
  dir = projectDir,
  run_id = run_id,
  columns = c("solution", "unit_id")
) |>
  summarise(
    Frequency = n() / 1000, # n = 1000
    .by = "unit_id"
  )
# Join with spatial data and plot
left_join(
  example_spatial,
  unit_frequency,
  by = "unit_id"
) |>
  ggplot(aes(fill = Frequency)) +
  geom_sf(color = "grey") +
  scale_fill_viridis_c(
    direction = -1,
    na.value = "white",
    name = "Frequency"
  ) +
  theme_void() +
  labs(title = "Frequency of planning unit selection") +
  theme(plot.title = element_text(hjust = 0.5))