R/plot_attpam.R

Defines functions lets.plot.attrpam

Documented in lets.plot.attrpam

#' Plot an Attribute-space PAM
#'
#' @title Plot attrPAM results
#' @description
#' Plots the richness (or a single species) in the attribute (trait) space
#' generated by \code{\link{lets.attrpam}}.
#'
#' @param x A list returned by \code{\link{lets.attrpam}}.
#' @param species Optional. A character string with the name of a species to highlight.
#'                If provided, plots its presence in trait space.
#' @param cell_id An integer or vector of integers indicating attribute space cell(s) to be highlighted.
#' @param col_rich Optional. A color palette function or vector. If `NULL`, a
#'                 default palette is used (red ramp for species maps, red–white ramp for richness).
#' @param ... Additional arguments passed to \code{plot()}.
#'
#'
#' @examples
#' \dontrun{
#' # Example with simulated traits
#' n <- 2000
#' Species <- paste0("sp", 1:n)
#' trait_a <- rnorm(n)
#' trait_b <- trait_a * 0.2 + rnorm(n)  # correlated trait
#' df <- data.frame(Species, trait_a, trait_b)
#'
#' # Build AttrPAM
#' x <- lets.attrpam(df, n_bins = 30)
#' lets.plot.attrpam(x)
#'}
#' @import terra grDevices graphics
#' @export
lets.plot.attrpam <- function(x,
                              species = NULL,
                              cell_id = NULL,
                              col_rich = NULL,
                              ...) {
  
  ## --- Define color palette --------------------------------------------------
  check_sp <- !is.null(species)
  check_cell <- !is.null(cell_id)
  
  if (is.null(col_rich)) {
    if (!(check_sp | check_cell)) {
      # Default richness palette (light to dark red)
      colfunc <- grDevices::colorRampPalette(c("#fff5f0", "#fb6a4a", "#67000d"))
    } else {
      # Default species palette (gray to red)
      colfunc <- grDevices::colorRampPalette(c("gray90", "red"))
    }
  } else {
    colfunc <- col_rich
  }
  
  ## --- Select values to plot -------------------------------------------------
  if (check_sp) {
    sp_id <- colnames(x[[1]]) == species
    pos   <- x[[1]][, sp_id] == 1
    cells <- x[[1]][pos, 1]
    v     <- terra::values(x[[2]])
    v[!is.na(v)] <- 0
    v[cells]    <- 1
    terra::values(x[[2]]) <- v
  }
  
  if (check_cell) {
    v <- terra::values(x[[2]])
    v[!is.na(v)] <- 0
    v[cell_id]    <- 1
    terra::values(x[[2]]) <- v
    
  }
  
  ## --- Axis scaling ----------------------------------------------------------
  ext_vals <- terra::ext(x[[2]])
  asp_ratio <- (ext_vals[2] - ext_vals[1]) / (ext_vals[4] - ext_vals[3])
  
  labs <- colnames(x[[1]])[2:3]
  
  ## --- Plot ------------------------------------------------------------------
  n_col <- length(table(terra::values(x[[2]]))) + 1
  plot(x[[2]],
       main = "Attribute space",
       col  = colfunc(n_col),
       asp  = asp_ratio,
       xlab = labs[1],
       ylab = labs[2],
       ...)
  
  invisible(NULL)
}

Try the letsR package in your browser

Any scripts or data that you put into this service are public.

letsR documentation built on Nov. 26, 2026, 9:09 a.m.