Skip to content

get_labels() returns a named list of labels, written as character strings, indicating which labels are used by a plot.

Usage

get_labels(p, aes = NULL)

Arguments

p

A ggplot object

aes

If aes is a character vector, returns only the labels corresponding to the included aesthetics. Defaults to NULL, which returns all labels.

Value

A named list of character strings.

Details

Note that get_labels() will return NULL if a label is explicitly set to NULL or if a requested aesthetic is not present in the plot.

See also

Other functions for checking labels: get_default_labels(), uses_labels()

Examples

require(ggplot2)

p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(mapping = aes(color = class)) +
  geom_smooth() +
  labs(x = "Weight", y = "MPG", color = NULL)

get_labels(p)
#> $x
#> [1] "Weight"
#> 
#> $y
#> [1] "MPG"
#> 
#> $colour
#> NULL
#> 
get_labels(p, c("x", "y"))
#> $x
#> [1] "Weight"
#> 
#> $y
#> [1] "MPG"
#> 

# The colo(u)r aesthetic can be matched with or without a u
get_labels(p, "color")
#> $color
#> NULL
#> 
get_labels(p, "colour")
#> $colour
#> NULL
#>