MathMarEcol / spatialplanr

Spatial Planning workflow for the MME Lab
https://mathmarecol.github.io/spatialplanr/
Other
8 stars 1 forks source link

Function for plotting RWR #21

Closed jaseeverett closed 1 year ago

jaseeverett commented 2 years ago

# -------- Importance score plot -RWR (USE WHEN MINIMUM SHORTFALL)

#' @noRd
#'

create_impScoreRWRPlot <- function(s1, p1){

  s1 <- s1 %>% tibble::as_tibble()
  rwr1 <- prioritizr::eval_rare_richness_importance(p1, s1[, "solution_1"]) %>%
    dplyr::mutate(geometry = s1$geometry) %>%
    sf::st_as_sf()

  selectedRWR <- rwr1 %>%
    dplyr::filter(.data$rwr != 0)

  quant95 <- round(stats::quantile(selectedRWR$rwr, 0.95), 2) #get importance score at 95th percentile of all selected planning units
  seq95 <- seq(0, quant95, length.out = 5)
  lab <- c(seq95[1], seq95[2], seq95[3], seq95[4], paste0("\u2265", quant95, sep = " "))

  rwr1$rwr[rwr1$rwr >= quant95] <- quant95

  gg_impScore <- ggplot2::ggplot() +
    ggplot2::geom_sf(data = rwr1, ggplot2::aes(fill = .data$rwr), colour = NA) +
    ggplot2::scale_fill_viridis_c(option = "A", 
                                  direction = -1, breaks = seq95, labels = lab, 
                                  guide = ggplot2::guide_colourbar(title.position = "right", 
                                                                   title = "Importance Scores \n(Rarity Weighted Richness)",
                                                                   barwidth = 2, barheight = 20))+#, oob=squish)
    gg_add_layers() +
    ggplot2::theme(legend.title = ggplot2::element_text(angle = -90, hjust = 0.5),
                   text = ggplot2::element_text(size = 20),
                   axis.title = ggplot2::element_blank())
  return(gg_impScore)
}
jaseeverett commented 1 year ago

@sandra-neubert I think this is some of that code that didn't make it into the visible portion of WSMPA2