uses_labels: Does a plot use one or more labels?

View source: R/labels.R

uses_labelsR Documentation

Does a plot use one or more labels?

Description

uses_labels() tests whether a plot uses one or more labels.

Usage

uses_labels(p, ...)

Arguments

p

A ggplot object

...

<dynamic-dots> Character strings. Unnamed arguments will check whether a label exists for that aesthetic. Named arguments will check whether the aesthetic with the same name has a label with a matching value. Each argument should have a matching ggplot aesthetic or label. Strings may be input as individual arguments or as list elements.

Details

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

Value

A named logical vector of the same length as the number of inputs to ....

See Also

Other functions for checking labels: get_default_labels(), get_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)

# Unnamed arguments check if a label is set for the given aesthetic
uses_labels(p, "title", "subtitle", "x", "y")

# The check will return TRUE for labels set to NULL
uses_labels(p, "color")

# The check will return TRUE for aesthetics with default labels
uses_labels(p, "shape")

# Named arguments check if the label matches an expected value
uses_labels(p, x = "Weight")
uses_labels(p, x = "Weight", y = "MPG", color = NULL)

# You can check for default labels with default_label()
uses_labels(p, shape = default_label(), x = default_label())

# The colo(u)r aesthetic can be matched with or without a u
uses_labels(p, color = NULL)
uses_labels(p, colour = NULL)

# Inputs can be passed from a list, with or without the !!! operator
label_list <- list(x = "Weight", y = "MPG", color = NULL)
uses_labels(p, label_list)
uses_labels(p, !!!label_list)

rstudio-education/ggcheck documentation built on May 12, 2023, 11:33 a.m.