R/plot.ir.R

Defines functions plot.ir

Documented in plot.ir

#' Plots an object of class `ir`
#'
#' `plot.ir` is the plot method for objects of class `ir`.
#'
#' @param x An object of class [`ir`][ir_new_ir()].
#'
#' @param ... Further arguments, will be ignored.
#'
#' @return An object of class [`ggplot2`][ggplot2::ggplot].
#'
#' @examples
#' # simple plotting
#' plot(ir::ir_sample_data[1:2, ])
#'
#' # advanced functions
#' plot(ir::ir_sample_data) +
#'    ggplot2::facet_wrap(~ sample_type)
#'
#' @export
plot.ir <- function(x, ...) {

  x_unnested <-
    x %>%
    ir_check_ir() %>%
    dplyr::mutate(
      measurement_id = seq_along(.data$spectra),
      spectra =
        purrr::map(.data$spectra, function(.x) { # to avoid errors in geom_path() when there are spectra with one point
          if(nrow(.x) == 1) {
            rbind(.x, .x)
          } else {
            .x
          }
        })
      ) %>%
    tidyr::unnest(cols = "spectra")

  ggplot2::ggplot(
    x_unnested,
    mapping = ggplot2::aes(
      x = .data$x,
      y = .data$y,
      group = .data$measurement_id
    )
  ) +
    ggplot2::geom_path()

}
henningte/ir documentation built on July 4, 2025, 5:12 p.m.