Skip to contents

make_GeomPattern() makes a pattern geom from a geom: it draws the grob of geom and fills each of its areas with a pattern, has the pattern aesthetics as well as those of geom, and draws the key glyph of geom filled with the key's pattern. The grob it draws is named after the pattern geom (e.g. "geom_smooth_pattern" for GeomSmoothPattern). Use ggplot2::make_constructor() to make its geom_*_pattern() function, make_geom_pattern() to make a geom_*_pattern() function from a geom_*() function, or with_pattern() to make a pattern layer from a layer.

Usage

make_GeomPattern(
  geom,
  name = paste0(class(geom)[1L], "Pattern"),
  area_by = NULL
)

Arguments

geom

A ggplot2::Geom object e.g. ggplot2::GeomSmooth.

name

The class name of the pattern geom.

area_by

How the areas of the grob the geom draws are matched to the rows of the data frame its draw_group() method is given (or else its draw_panel() method if it doesn't have its own draw_group() method) i.e. which rows give the areas their patterns and in what order. One of:

  • "trace": works for most geoms without knowing how they draw their areas (but is the slowest). The geom is given a different fill for each kind of row and each area is then filled with the pattern of the row whose fill it was drawn with (and given back its actual fill). Areas that weren't drawn with a row's fill are left alone. Needs the geom to draw a row's area with its (character) fill (possibly with an alpha).

  • "row": the geom draws an area per row (in row order) e.g. ggplot2::GeomRect.

  • "group": the geom draws an area per group (sorted by group) with the pattern of its first row (it is an error if the pattern aesthetics vary within a group) e.g. ggplot2::GeomPolygon or a geom whose draw_group() method draws a single area e.g. ggplot2::GeomRibbon.

  • A function returning the rows of the areas (in the order of gridpattern::area_boundaries()) or a single row for every area. It is given the data frame (as its first argument) and the draw method's other parameters (e.g. lineend) by name if it has an argument of that name (or ...).

By default what we know the draw method of geom needs (if it is a {ggplot2} one) or else "trace". A grob whose areas are only made when it is drawn (e.g. in a makeContent() method) is filled when it is drawn.

Filling areas other than those of a grid::polygonGrob(), grid::pathGrob(), or grid::rectGrob() (e.g. the round rectangles of a {treemapify} treemap) requires R (>= 4.2.0) (see gridpattern::with_pattern()).

Value

A ggplot2::Geom object. A geom that is already a pattern geom (its default_aes has all the pattern aesthetics) is returned as it is (or as a subclass of it called name if that is given) and it is an error to set its area_by.

Details

A geom doesn't know the default stat and position of its geom_*() function so pass them to ggplot2::make_constructor() if they aren't "identity" e.g. stat = ggridges::StatDensityRidges, position = ggridges::position_points_sina() for ggridges::geom_density_ridges() (otherwise the layer may fail with an error about missing aesthetics the stat would have computed). Nor does it know any other defaults its geom_*() function passes to its draw method e.g. ggplot2::geom_smooth() passes se = TRUE (to draw its confidence band). with_pattern() and make_geom_pattern() given the geom_*() function keep all of them.

The pattern geom stores a copy of geom as it is when make_GeomPattern() is called. So if you call it at the top level of a package's code (which is run when the package is installed) with a geom from another package the pattern geom won't pick up later updates of that geom until your package is reinstalled: call it in your package's .onLoad() instead or write the pattern geom's draw methods yourself with with_pattern_aes() (see vignette("developing-pattern-geoms", package = "ggpattern")).

See also

make_geom_pattern() to make a geom_*_pattern() function from a geom_*() function or a geom. with_pattern() to make a pattern layer from a layer.

Examples

library("ggplot2")
if (requireNamespace("ggridges", quietly = TRUE)) {
  GeomDensityRidgesPattern <- make_GeomPattern(ggridges::GeomDensityRidges)
  geom_density_ridges_pattern <- make_constructor(GeomDensityRidgesPattern,
                                                  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