Learn R Programming

ggpubr (version 0.1.6)

get_legend: Extract Legends from a ggplot object

Description

Extract the legend labels from a ggplot object.

Usage

get_legend(p)

Arguments

p

an object of class ggplot or a list of ggplots. If p is a list, only the first legend is returned.

Value

an object of class gtable.

Examples

Run this code
# NOT RUN {
# Create a scatter plot
p <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
        color = "Species", palette = "jco",
        ggtheme = theme_minimal())
p

# Extract the legend. Returns a gtable
leg <- get_legend(p)

# Convert to a ggplot and print
as_ggplot(leg)

# }

Run the code above in your browser using DataLab