Layer operations
Source:R/class-operation-.R
, R/operation-.R
, R/operation-adjust.R
, and 5 more
operation-class.Rd
Layer operations are composable transformations that can be applied to ggplot2
layer-like objects, such as stat
s, geom
s, and lists of stat
s and
geom
s; see the layer-like documentation page for a description of valid
layer-like objects.
Usage
# S4 method for operation
show(object)
# S4 method for operation
format(x, ...)
# S4 method for adjust
format(x, ...)
# S4 method for affine_transform
format(x, ...)
# S4 method for blend
format(x, ...)
# S4 method for operation_composition
format(x, ...)
# S4 method for nop
format(x, ...)
# S4 method for operation_product
format(x, ...)
Value
For show()
, an invisible()
copy of the input.
For format()
, a character string representing the input.
Details
operations can be composed using the +
and *
operators (see operation_sum
and operation_product). Addition and multiplication of operations and layer-like
objects obeys the distributive law.
operations can be applied to layer-like objects using *
or |>
, with slightly
different results:
Using
*
, application of operations to a list of layer-like objects is distributive. For example,list(geom_line(), geom_point()) * blend("multiply")
is equivalent tolist(geom_line() * blend("multiply"), geom_point() * blend("multiply"))
; i.e. it multiply-blends the contents of the two layers individually.Using
|>
, application of operations to a list of layer-like objects is not distributive (unless the only reasonable interpretation of applying the transformation is necessarily distributive; e.g.adjust()
). For example,list(geom_line(), geom_point()) |> blend("multiply")
would multiply-blend both layers together, rather than multiply-blending the contents of the two layers individually.
Methods (by generic)
show(operation)
: Print an operation.format(operation)
: Format an operation for printing.
Examples
library(ggplot2)
# operations can stand alone
adjust(aes(color = x))
#> <operation>: adjust(aes(colour = ~x))
# they can also be applied to layers through multiplication or piping
geom_line() |> adjust(aes(color = x))
#> mapping: colour = ~x
#> geom_line: na.rm = FALSE, orientation = NA
#> stat_identity: na.rm = FALSE
#> position_identity
geom_line() * adjust(aes(color = x))
#> mapping: colour = ~x
#> geom_line: na.rm = FALSE, orientation = NA
#> stat_identity: na.rm = FALSE
#> position_identity
# layer operations act as a small algebra, and can be combined through
# multiplication and addition
(adjust(fill = "green") + 1) * blend("multiply")
#> <operation>: (adjust(fill = "green") * blend("multiply") + blend("multiply"))