Skip to content

Fill the areas of {ggplot2} grobs with with_pattern() and export pattern geom helpers - #167

Merged
trevorld merged 15 commits into
masterfrom
with-pattern
Nov 14, 2026
Merged

trevorld merged 15 commits into
masterfrom
with-pattern

Conversation

@trevorld

Copy link
Copy Markdown
Owner

Closes #156, closes #158, closes #161.

Summary

Our geoms now draw the grob of the {ggplot2} geom they extend and fill each of its areas with a pattern in place (using gridpattern::with_pattern()), instead of copying {ggplot2}'s grob-construction logic. The same machinery is exported so pattern geoms can be made from other geoms, and the helpers it makes redundant are deprecated or removed.

New features

  • make_GeomPattern() makes a pattern geom from (almost) any {ggplot2} (or extension) geom. Its area_by argument says how the areas are matched to the rows of the data ("row", "group", a function, or "trace").
  • gridpattern::with_pattern() is re-exported and has a new Layer method e.g. with_pattern(geom_contour_filled(), pattern_fill = "white").
  • with_pattern_aes(), with_pattern_key(), and default_pattern_aes() for writing the draw methods of a pattern geom, plus a new "Developing Pattern Geoms" vignette (Export helpers for third-party pattern geoms #156).
  • geom_point_pattern() and its draw_key_point_pattern() key glyph.

Changes to existing geoms (#158)

  • Each area's fill, pattern, and border are now drawn in turn (as {ggplot2} draws its areas) instead of every border being drawn above every pattern.
  • draw_key_polygon_pattern(), draw_key_boxplot_pattern(), and draw_key_crossbar_pattern() now fill {ggplot2}'s own key glyphs, so key borders have the geom's border width and boxplot/crossbar keys respect orientation and the whisker, median, box, and staple parameters.
  • Ribbons, areas, and densities support a fill or alpha that varies along the ribbon.
  • Fixes the pattern position of polygons with a subgroup and of geom_sf_pattern(); geom_sf_pattern() also fills each polygon of a GEOMETRYCOLLECTION.

Deprecated and removed (#161)

  • create_polygon_df() and is_polygon_df() are soft-deprecated since nothing exported accepts a polygon_df. They share one help page marked @keywords internal and are no longer in the {pkgdown} reference index.
  • The .Defunct() stubs (unexported since v0.4.2), the ggpattern-defunct help page, and the commented out ggpattern-deprecated scaffold are removed.

Housekeeping

  • The {treemapify} test moves to tests/testthat/test-with-pattern-treemapify.R, which is in .Rbuildignore, so {treemapify} need not be in Suggests (it is only run from a source checkout).
  • with_pattern.Layer() gets its own "Patterned layers" section in the {pkgdown} reference index and the "Re-exported functions" section is dropped.

Notes for review

  • Requires the unreleased {gridpattern} (>= 1.6.0-15) (installed via Remotes: trevorld/gridpattern), so this cannot go to CRAN before {gridpattern} does.
  • Many visual snapshots change because of the new drawing order and key glyphs.
  • with_pattern() does not yet accept a list of layers (e.g. what geom_sf() returns); a list method has been proposed for {gridpattern}.

Testing

  • The full test suite passed locally against the development {gridpattern} (428 passing) before the last three small changes (dropping the deprecated page's example, moving the {treemapify} test, and the {pkgdown} index); after them only the with-pattern tests were re-run (82 passing).
  • pkgdown::check_pkgdown() reports no problems.
  • R CMD check has not been re-run on the final commit.

🤖 Generated with Claude Code

trevorld and others added 15 commits November 11, 2026 16:08
…ttern::with_pattern()` (#158)

Our geoms now draw the grob of the `{ggplot2}` geom they extend and fill each of
its areas with a pattern in place instead of copying its grob-construction logic.
The `draw_key_*_pattern()` functions likewise fill the areas of the corresponding
`{ggplot2}` key glyphs.

* Each area's fill, pattern, and border are now drawn in turn so overlapping areas stack correctly
* `geom_ribbon_pattern()` and friends support a `fill` or `alpha` that varies along the ribbon
* `geom_sf_pattern()` also fills each polygon of a `GEOMETRYCOLLECTION`
* Bumps required version of `{gridpattern}` to v1.6.0-11

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`geom_point_pattern()` fills each point with a filled symbol (`shape` 21 to 25,
its default is 21) with its own pattern.  Points with other shapes are drawn
as by `geom_point()`.  Requires R (>= 4.2.0).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…tests

Use `layer_grob()` instead of `print()` to trigger the "Pattern aesthetics can not vary along a ribbon" error.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Snapshots for the `gridpattern::with_pattern()` based geoms (#158), where each area's fill, pattern, and border are now drawn in turn, new `geom_point_pattern()` snapshots, and the previously stale `pattern_fill` snapshots.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* `make_GeomPattern()` (not exported yet) makes a pattern geom from a `{ggplot2}` geom:
  it draws the parent geom's grob and fills each of its areas with the pattern of its row
  (matched by its `rows` argument or else by tracing each area's fill back to its row).
* The `Layer` method of `gridpattern::with_pattern()` (registered but not re-exported)
  makes a pattern layer from a layer e.g. `with_pattern(geom_rect(), pattern_fill = "blue")`,
  and its `dotstackGrob` method fills the dots of `geom_dotplot()` when they are drawn.
* The pattern geoms now fill their areas with `with_pattern_aes()`
  and their key glyphs with `with_pattern_key()`.

Requires `{gridpattern}` (>= 1.6.0-14).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Its presets are now `"row"` (was `"all"`), `"group"`, and `"trace"`.
* `"constant"` is dropped: the geoms it was for (e.g. `GeomRibbon`) draw a single area per
  `draw_group()` call so `"group"` matches them, and `"group"` now checks that the
  pattern aesthetics don't vary within a group (as `"constant"` did).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…its areas

`make_GeomPattern()`'s geoms now catch `{gridpattern}`'s `gridpattern_area_mismatch` error
and re-raise it (class `ggpattern_area_mismatch`, with it as the parent) naming the geom
and its `area_by` and suggesting `area_by = "trace"`,
also when the error is only raised when the grob is drawn (`delay = TRUE`).

Requires `{gridpattern}` (>= 1.6.0-15).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Pattern()`

* Only the data, panel parameters, and coord before any `...` are passed by position
  e.g. `GeomViolin$draw_group(self, data, ..., quantile_gp, flipped_aes)`'s `quantile_gp`
  was passed into `...` (so a violin's quantile line styling was ignored).
* They are now given `{ggplot2}`'s names (`data`, `panel_params`, `coord`)
  which `Geom$parameters()` and `make_constructor()` leave out
  e.g. for `treemapify::GeomTreemap` (which calls its panel parameters `panel_scales`)
  `make_constructor()` no longer asks for a default `panel_scales`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ttern()`

* `make_GeomPattern()` makes a pattern geom from (almost) any geom
  and the `Layer` method of `with_pattern()` makes a pattern layer from a layer
  e.g. `with_pattern(geom_contour_filled(), pattern_fill = "white")`.
* Their `delay` argument is dropped: a grob with no areas when the panel is drawn
  (e.g. one that makes its areas in a `makeContent()` method) is now filled when it is drawn.
* An `area_by` function is now also given the draw method's parameters
  it has arguments for (or all of them if it has `...`).
* Filling areas other than rectangles, polygons, and paths requires R (>= 4.2.0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_pattern_aes()` (#156)

* `with_pattern_aes()` fills the areas of a grob with the patterns of their rows of a layer's data
  and `with_pattern_key()` fills a legend key glyph with the key's pattern,
  for writing the draw methods of a pattern geom.
* `default_pattern_aes()` returns the default pattern aesthetics for a pattern geom's `default_aes`.
* New "Developing Pattern Geoms" vignette
  (`with_pattern()`, `make_GeomPattern()` and its `area_by`, and writing the draw methods yourself).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Nothing exported accepts a `polygon_df` (a data frame with `x`, `y`, and `id` columns).
  To fill the areas of a grob with patterns see `with_pattern_aes()`.
* They now share a help page marked `@keywords internal`
  and are no longer listed in the `{pkgdown}` reference index.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* The `.Defunct()` stubs have been unexported since v0.4.2
  so only the `ggpattern-defunct` help page (and its test) was left of them.
* Also removes the (commented out) `ggpattern-deprecated` help page scaffold:
  deprecated functions are now marked `@keywords internal` instead.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…build`

`R CMD check` warned that `{treemapify}` is used in the tests but not declared.
Rather than add it to `Suggests` its test is now in a file in `.Rbuildignore`
so it is only run from a source checkout.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ction

* It is for users rather than for developing new geoms
  so it moves to a new "Patterned layers" section after the geom functions.
* Drops the "Re-exported functions" section (it listed a stray `reexports` beside `with_pattern()`).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@trevorld
trevorld merged commit 8c7a0a3 into master Nov 14, 2026
3 checks passed
@trevorld
trevorld deleted the with-pattern branch November 14, 2026 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant