Skip to content

What is the default label for a plot aesthetic?

Usage

get_default_labels(p, aes = NULL)

Arguments

p

A ggplot object

aes

If aes is a character vector, returns only the default labels (based on the plot p) that correspond to the included aesthetics. Defaults to NULL, which returns the default values of all labels.

Value

A named list in which each element is a character string or NULL. Strings are returned for aesthetics with a default value. NULL is returned for aesthetics that do not exist in the plot, or non-aesthetic labels that do not have a default value, like title.

See also

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

Examples

require(ggplot2)

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

# Returns the label the ggplot would create by default for an aesthetic
get_default_labels(p, "x")
#> $x
#> [1] "displ"
#> 
get_default_labels(p, c("x", "y"))
#> $x
#> [1] "displ"
#> 
#> $y
#> [1] "hwy"
#> 
get_default_labels(p)
#> $x
#> [1] "displ"
#> 
#> $y
#> [1] "hwy"
#> 
#> $colour
#> [1] "class"
#> 
#> $title
#> NULL
#> 
#> $shape
#> [1] "drv"
#> 

# If an aesthetic does not exist, returns NULL
get_default_labels(p, "size")
#> $size
#> NULL
#> 

# Non-aesthetic labels have no default value, so they also return NULL
get_default_labels(p, "title")
#> $title
#> NULL
#> 
get_default_labels(p, "comment")
#> $comment
#> NULL
#> 

# The colo(u)r aesthetic can be matched with or without a u
get_default_labels(p, "color")
#> $color
#> [1] "class"
#> 
get_default_labels(p, "colour")
#> $colour
#> [1] "class"
#>