make_geom_pattern() makes a geom_*_pattern() function
from a geom_*() function (e.g. ggplot2::geom_smooth) or from a geom (e.g. ggridges::GeomDensityRidges).
Prefer giving it the geom_*() function if there is one.
Usage
make_geom_pattern(x, ...)
# S3 method for class '`function`'
make_geom_pattern(x, ..., area_by = NULL)
# S3 method for class 'Geom'
make_geom_pattern(x, ..., area_by = NULL, env = caller_env())Arguments
- x
A function that returns a layer (or a list of layers and other
{ggplot2}objects) e.g.ggplot2::geom_smoothor a ggplot2::Geom object e.g.ggridges::GeomDensityRidges.- ...
For a function must be empty. For a geom passed on to
ggplot2::make_constructor()e.g.stat = ggridges::StatDensityRidges.- area_by
How the areas the geom draws are matched to the rows of its data, see
make_GeomPattern(). It is an error (when the function made is called) if the layer of a function already is a pattern layer.- env
For a geom the
envofggplot2::make_constructor().
Details
Given a function it returns a function which calls
xand thenwith_pattern()on what it returns so it keeps the stat, position, and every other default (and check) ofx. Its arguments aremappingand...: the pattern aesthetics among them (set to a constant in...or mapped inmapping) are held back forwith_pattern()and all of the others are passed on tox(mappingby name ifxhas an argument of that name or else as its first argument). The pattern geom is made each time the function is called so (unlike a geom made bymake_GeomPattern()) it always uses the installed version of the geom.Given a geom it returns the function made by
ggplot2::make_constructor()for the pattern geom made bymake_GeomPattern()(our own pattern geom ifxis a{ggplot2}geom we have one for orxitself if it already is a pattern geom). A geom doesn't know the defaults of itsgeom_*()function so itsstatandposition(if they aren't"identity") and any other argument whose default differs from that of the geom's draw method must be given in...e.g.stat = ggridges::StatDensityRidges, position = ggridges::position_points_sina()forggridges::GeomDensityRidges. Some defaults can't be given this way e.g.ggplot2::geom_smooth()in the development version of{ggplot2}hides the outline of its confidence band which a pattern geom made fromggplot2::GeomSmoothwould draw. The function stores the pattern geom (see the note on packages inmake_GeomPattern()).
See also
with_pattern() to make a pattern layer from a layer
and make_GeomPattern() to make a pattern geom from a geom.
Examples
library("ggplot2")
geom_smooth_pattern <- make_geom_pattern(geom_smooth)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
geom_smooth_pattern(aes(pattern = factor(am)), method = "lm", formula = y ~ x,
pattern_colour = "grey40", pattern_spacing = 0.03)
# from a geom: we must give the defaults of its `geom_*()` function ourselves
if (requireNamespace("ggridges", quietly = TRUE)) {
geom_density_ridges_pattern <- make_geom_pattern(ggridges::GeomDensityRidges,
stat = ggridges::StatDensityRidges,
position = ggridges::position_points_sina())
ggplot(iris, aes(Sepal.Length, Species, pattern = Species)) +
geom_density_ridges_pattern(pattern_spacing = 0.03)
}
#> Picking joint bandwidth of 0.181
