get_geom_layer: Isolate a geom layer from a plot

View source: R/layers.R

get_geom_layerR Documentation

Isolate a geom layer from a plot

Description

get_geom_layer returns a geom layer from a plot along with the global data sets and aesthetic mappings that the layer may inherit from.

Usage

get_geom_layer(p, geom = NULL, i = NULL)

Arguments

p

A ggplot object

geom

A character string found in the suffix of a ggplot2 geom function, e.g. "point".

i

A numerical index, e.g. 1.

Details

Users can specify a layer in one of 3 ways:

  1. By order of appearance with i. The first layer to appear in the plot (the one drawn first, on the bottom) corresponds to i = 1.

  2. By type of geom with geom. get_geom_layer will return the first layer that uses the geom.

  3. By a combination of geom and i. get_geom_layer will return the ith layer that uses the geom.

Value

An object with class layer_to_check to be manipulated further with ggcheck functions.

Examples

require(ggplot2)
p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(color = "red") +
  geom_point(mapping = aes(color = class)) +
  geom_smooth(se = FALSE)

get_geom_layer(p, i = 1)
get_geom_layer(p, geom = "smooth")
get_geom_layer(p, geom = "point", i = 2)

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