get_Legend: Retrieve the legend of a plot

View source: R/func_basics.R

get_LegendR Documentation

Retrieve the legend of a plot

Description

This function extracts just the legend from a ggplot.

Usage

get_Legend(plot, legend = NULL)

Arguments

object

A ggplot or gtable from which to retrieve the legend.

Details

The get_legend function from the cowplot package stopped working properly with ggplot2 v3.5.0. A modified function was contributed by Teun van den Brand (teunbrand) on cowplot's GitHub Issues and should work with the newer version of ggplot2. Please refer to https://github.com/wilkelab/cowplot/issues/202 on the discussion of this bug and fixes.

Value

A gtable object holding just the legend or NULL if there is no legend.

Examples

library(ggplot2)

p <- ggplot(mpg, aes(displ, hwy, colour = factor(cyl), shape = factor(year))) +
  geom_point() + guides(shape = guide_legend(position = "bottom"))

plot(get_Legend(p))

ycl6/scRUtils documentation built on Feb. 18, 2025, 6:14 a.m.