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 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.

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 = "smooth" for ggplot2::geom_smooth() (otherwise the layer may fail with an error about missing aesthetics the stat would have computed). with_pattern() keeps those of the layer it is given.

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 built) 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

with_pattern() to make a pattern layer from a layer.

Examples

library("ggplot2")
GeomSmoothPattern <- make_GeomPattern(GeomSmooth)
geom_smooth_pattern <- make_constructor(GeomSmoothPattern, stat = "smooth", se = TRUE)
if (requireNamespace("magick", quietly = TRUE)) {
  magick::magick_set_seed(42)
  ggplot(mtcars, aes(mpg, wt)) +
      geom_point() +
      geom_smooth_pattern(method = "lm", formula = y ~ x, pattern = "plasma", pattern_alpha = 0.6)
}