View source: R/ggExtractLegend.R
ggExtractLegend | R Documentation |
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.
ggExtractLegend(...)
... |
An object of class 'ggplot'. |
An object of class 'TableGrob'.
Florian Detsch
https://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots
ggplot
, ggplot_gtable
library(ggplot2)
library(grid)
p <- qplot(mpg, wt, data = mtcars, colour = factor(cyl))
p_legend <- ggExtractLegend(p)
grid.draw(p_legend)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.