get_Legend | R Documentation |
This function extracts just the legend from a ggplot.
get_Legend(plot, legend = NULL)
object |
A |
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.
A gtable
object holding just the legend or NULL
if there is no legend.
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.