Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as_group

Collect layers into a group that can be treated as a single layer


Description

While you often want to apply filters to layers one by one, there are times when one filter should be applied to a collection of layers as if they were one. This can be achieved by first combining all the layers into a group with as_group() and applying the filter to the resulting group. This can only be done to ggplot2 layers and grobs as the other supported objects are not part of a graphic stack.

Usage

as_group(..., id = NULL, include = is.null(id))

Arguments

...

A range of layers to combine

id

A string identifying this layer for later use

include

Should the layer itself be included in the graphic

Value

A list of Layer objects or a gTree depending on the input

See Also

Other layer references: as_colourspace(), as_reference()

Examples

library(ggplot2)

# With no grouping the filters on layers are applied one by one
ggplot(mtcars, aes(mpg, disp)) +
  with_shadow(geom_smooth(alpha = 1), sigma = 4) +
  with_shadow(geom_point(), sigma = 4)

# Grouping the layers allows you to apply a filter on the combined result
ggplot(mtcars, aes(mpg, disp)) +
  as_group(
    geom_smooth(alpha = 1),
    geom_point(),
    id = 'group_1'
  ) +
  with_shadow('group_1', sigma = 4)

ggfx

Pixel Filters for 'ggplot2' and 'grid'

v1.0.0
MIT + file LICENSE
Authors
Thomas Lin Pedersen [aut, cre] (<https://orcid.org/0000-0002-5147-4711>), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.