Pipable ggplot2 stats
stat_.Rd
A ggplot2::stat intended for piping.
Arguments
- x
One of:
A string: The suffix of a
stat_XXX
function used to create this stat.A ggbuilder layer, layer_spec, or plot_data object representing earlier data and data transformations used as input to this stat. In this case, the second argument should be the suffix of a
stat_XXX
function used to create this stat.
- ...
arguments passed on to the stat's constructor.
Value
A ggbuilder layer, which is also a ggplot2::layer, and which can be
added to a ggplot()
object or piped into other ggbuilder functions.
Details
stat_()
can be used to replace typical stat_XXX()
calls in ggplot2
code to make them pipable by using the XXX
suffix as a string as the first
argument to stat_()
.
For example, stat_boxplot(...)
would become stat_("boxplot", ...)
.
The primary difference between the former and the latter is that the latter
is both a ggplot2::layer and a ggbuilder layer containing a layer_spec.
The contained layer_spec stores information on how to construct the
stat, which allows it to be combined with other objects (e.g. by piping it
into geom_()
) to construct more complex layers. See Examples.
Examples
library(ggplot2)
set.seed(123456)
df = data.frame(condition = c("A", "B", "C"), response = round(rnorm(30, 1:3), 1))
df |>
ggplot(aes(x = condition, y = response, color = condition)) +
geom_boxplot() +
stat_("boxplot", aes(y = response)) |>
geom_("label", aes(y = ymax, label = ymax)) |>
remap(aes(fill = scales::alpha(color, 0.1))) +
theme_light()
#> Warning: Duplicated aesthetics after name standardisation: NA
#> Warning: Duplicated aesthetics after name standardisation: NA