Skip to contents

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_smooth or 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 env of ggplot2::make_constructor().

Value

A function that returns a layer (or whatever x returns with its layers made pattern layers).

Details

  • Given a function it returns a function which calls x and then with_pattern() on what it returns so it keeps the stat, position, and every other default (and check) of x. Its arguments are mapping and ...: the pattern aesthetics among them (set to a constant in ... or mapped in mapping) are held back for with_pattern() and all of the others are passed on to x (mapping by name if x has 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 by make_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 by make_GeomPattern() (our own pattern geom if x is a {ggplot2} geom we have one for or x itself if it already is a pattern geom). A geom doesn't know the defaults of its geom_*() function so its stat and position (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() for ggridges::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 from ggplot2::GeomSmooth would draw. The function stores the pattern geom (see the note on packages in make_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