R/gg_extract_legend.R

Defines functions gg_extract_legend

Documented in gg_extract_legend

#' Extract legend from ggplot object
#'
#' @param p ggplot object
#'
#' @return
#' @export
#'
#'
gg_extract_legend <- function(p) {

  tmp <- ggplot2::ggplot_gtable(ggplot2::ggplot_build(p))

  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")

  legend <- tmp$grobs[[leg]]

  return(legend)

}
nicohuttmann/pOmics documentation built on Sept. 21, 2022, 9:28 a.m.