library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point() +
labs(color = "Cylinders")
# Extract the legend
leg <- get_legend(p)
# Draw just the legend
grid::grid.newpage()
grid::grid.draw(leg)
Run the code above in your browser using DataLab