get_legend: Extract Legends from a ggplot object

Description Usage Arguments Value Examples

View source: R/get_legend.R

Description

Extract the legend labels from a ggplot object.

Usage

1
get_legend(p, position = NULL)

Arguments

p

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

position

character specifying legend position. Allowed values are one of c("top", "bottom", "left", "right", "none"). To remove the legend use legend = "none".

Value

an object of class gtable.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 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)

ggpubr documentation built on July 2, 2020, 2:12 a.m.