R/ggplot2_get_legend.R

Defines functions get_legend

#' Extract legend from a ggplot 2 object
#'
#' source: http://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots
#'
#' @param x a ggplot2 object
#'
#' @return a legend or an empty grob
#' @export
#'
get_legend <- function(x) {
  requireNamespace('ggplot2')
  grobs <- x %>%
    ggplot2::ggplot_build() %>%
    ggplot2::ggplot_gtable() %>%
    magrittr::extract2('grobs')

  sel <- which(vapply(grobs, function(x) x$name == "guide-box", logical(1)))

  grobs[[sel]]
}
s-fleck/hammr documentation built on July 19, 2023, 9:20 p.m.