R/grobs.R

Defines functions annotation_custom2 add_grob grobs

Documented in add_grob grobs

#' grobs
#' @param options parameters (except `plotlist`) of [cowplot::plot_grid()]
#' @keywords internal
#' @export
grobs <- function(..., options = list(nrow = 1)) {
  params = c(plotlist = list(...), options)
  do.call(cowplot::plot_grid, params)
}


#' add grob to a plot
#' 
#' @param p ggplot object
#' @param ... grob objects
#' @param ggplot logical, if TRUE, return a ggplot object
#' 
#' @example R/examples/ex-add_grob.R
#' 
#' @import ggplotify
#' @export
add_grob <- function(p, ..., ggplot = TRUE) {
  g = grid::grobTree(
    as.grob(p),
    grid::grobTree(...,
    vp = grid::viewport()
    )
  )
  if (ggplot) as.ggplot(g) else g
}


annotation_custom2 <- function(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data) {
  layer(
    data = data, stat = StatIdentity, position = PositionIdentity,
    geom = ggplot2:::GeomCustomAnn,
    inherit.aes = TRUE, params = list(
      grob = grob,
      xmin = xmin, xmax = xmax,
      ymin = ymin, ymax = ymax
    )
  )
}
rpkgs/gg.layers documentation built on Sept. 14, 2024, 11:07 p.m.