ggExtractLegend: Extract legend from ggplot

View source: R/ggExtractLegend.R

ggExtractLegendR Documentation

Extract legend from ggplot

Description

This is a magnificent piece of code to extract a legend from a ggplot, e.g. for further use with grid.draw. The code was originally taken from https://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots.

Usage

ggExtractLegend(...)

Arguments

...

An object of class 'ggplot'.

Value

An object of class 'TableGrob'.

Author(s)

Florian Detsch

References

https://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots

See Also

ggplot, ggplot_gtable

Examples

 
library(ggplot2)
library(grid)

p <- qplot(mpg, wt, data = mtcars, colour = factor(cyl))

p_legend <- ggExtractLegend(p)
grid.draw(p_legend)
              

environmentalinformatics-marburg/Rsenal documentation built on July 28, 2023, 6:09 a.m.