Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/ggplot2-devel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Checks the package against the development version of {ggplot2}
# to get early warning of changes that break us
# e.g. to the order its geoms draw their areas in (which the pattern geoms rely on).
# The visual snapshot tests all `skip_on_ci()` so unrelated changes in how {ggplot2} draws don't fail it.
on:
schedule:
- cron: '0 6 * * 1' # weekly (Mondays)
workflow_dispatch:

name: ggplot2-devel

jobs:
ggplot2-devel:
runs-on: ubuntu-latest

name: ubuntu-latest (release) with the development ggplot2

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: release
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: github::tidyverse/ggplot2, any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
12 changes: 8 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ggpattern
Type: Package
Title: 'ggplot2' Pattern Geoms
Version: 1.4.0-4
Version: 1.4.0-5
Authors@R: c(person("Mike", "FC", role = "aut"),
person("Trevor L.", "Davis", role = c("aut", "cre"),
email = "trevor.l.davis@gmail.com",
Expand All @@ -20,7 +20,7 @@ Imports:
ggplot2 (>= 4.0.2),
glue,
grid,
gridpattern (>= 1.6.0-15),
gridpattern (>= 1.6.0-17),
lifecycle,
rlang (>= 1.1.3),
scales,
Expand All @@ -40,7 +40,7 @@ Suggests:
rmarkdown (>= 2.27),
sf (>= 0.7-3),
svglite (>= 2.1.3),
testthat (>= 2.1.0),
testthat (>= 3.1.9),
vdiffr (>= 1.0.6),
withr
Remotes:
Expand All @@ -61,11 +61,15 @@ Collate:
'geom-bin2d.R'
'geom-boxplot.R'
'geom-col.R'
'geom-polygon.R'
'geom-contour.R'
'geom-crossbar.R'
'geom-ribbon.R'
'geom-density.R'
'geom-density2d.R'
'geom-dotplot.R'
'geom-hex.R'
'geom-histogram.R'
'geom-polygon.R'
'geom-map.R'
'geom-point.R'
'geom-sf.R'
Expand Down
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# Generated by roxygen2: do not edit by hand

S3method(gridpattern::with_pattern,Geom)
S3method(gridpattern::with_pattern,Layer)
S3method(gridpattern::with_pattern,dotstackGrob)
S3method(gridpattern::with_pattern,gg)
S3method(gridpattern::with_pattern,ggplot)
S3method(makeContent,ggpattern_area_mismatch_context)
S3method(makeContent,ggpattern_traced)
export(GeomAreaPattern)
export(GeomBarPattern)
export(GeomBin2dPattern)
export(GeomBoxplotPattern)
export(GeomColPattern)
export(GeomContourFilledPattern)
export(GeomCrossbarPattern)
export(GeomDensity2dFilledPattern)
export(GeomDensityPattern)
export(GeomDotplotPattern)
export(GeomHexPattern)
export(GeomMapPattern)
export(GeomPointPattern)
export(GeomPolygonPattern)
Expand All @@ -31,8 +38,13 @@ export(geom_bin2d_pattern)
export(geom_bin_2d_pattern)
export(geom_boxplot_pattern)
export(geom_col_pattern)
export(geom_contour_filled_pattern)
export(geom_crossbar_pattern)
export(geom_density2d_filled_pattern)
export(geom_density_2d_filled_pattern)
export(geom_density_pattern)
export(geom_dotplot_pattern)
export(geom_hex_pattern)
export(geom_histogram_pattern)
export(geom_map_pattern)
export(geom_point_pattern)
Expand Down
21 changes: 19 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,30 @@
each area is traced back to its row by the fill it was drawn with (`"trace"`).
Filling areas other than rectangles, polygons, and paths (e.g. circles or round rectangles) requires R (>= 4.2.0).
* We now re-export `gridpattern::with_pattern()` whose new `Layer` method makes a pattern layer from a layer
e.g. `with_pattern(geom_contour_filled(), pattern_fill = "white")`
e.g. `with_pattern(geom_smooth(), pattern_fill = "white")`
(using our pattern geom for the layer's geom if we have one else `make_GeomPattern()`).
It also has a new `gg` method which returns any other `{ggplot2}` object as it is
(e.g. a coord, scale, facet, theme, `labs()`, or `guides()`) so that it can be given a list of them and layers
e.g. what `geom_sf()` returns: `with_pattern(geom_sf(), pattern = "circle")`.
It is an error to give it a plot or a geom.
A `NULL` in such a list is also left as it is e.g. `with_pattern(list(geom_col(), if (flip) coord_flip()))`.
Requires `{gridpattern}` (>= 1.6.0-17).
* The grob a `make_GeomPattern()` geom draws is named after the geom
(e.g. `"geom_smooth_pattern"` for `GeomSmoothPattern`) like `{ggplot2}` names the grobs of its geoms.
* New `with_pattern_aes()`, `with_pattern_key()`, and `default_pattern_aes()`
for writing the draw methods of a pattern geom (#156)
and a new "Developing Pattern Geoms" vignette.
* New `geom_contour_filled_pattern()`, `geom_density_2d_filled_pattern()`
(and its alias `geom_density2d_filled_pattern()`), `geom_dotplot_pattern()`, and `geom_hex_pattern()`
(and their `GeomContourFilledPattern`, `GeomDensity2dFilledPattern`, `GeomDotplotPattern`,
and `GeomHexPattern` geoms) (#162).
`geom_dotplot_pattern()` requires R (>= 4.2.0) (it is an error otherwise)
and `geom_hex_pattern()` requires `{hexbin}`.
* New `geom_point_pattern()` which fills each point with a filled symbol (`shape` 21 to 25, its default is 21)
with its own pattern (e.g. a different image per point using the "image" pattern and `pattern_filename`)
and its `draw_key_point_pattern()` legend key glyph.
Points with other shapes are drawn as by `geom_point()`.
Requires R (>= 4.2.0).
Requires R (>= 4.2.0) (it is an error otherwise).
* New `pattern_linewidth` aesthetic for the stroke line width of patterns
(matching `{ggplot2}`'s renaming of the `size` aesthetic to `linewidth` for line widths)
with new `scale_pattern_linewidth()`, `scale_pattern_linewidth_continuous()`,
Expand All @@ -79,6 +93,9 @@

## Bug fixes and minor improvements

* `GeomTilePattern` (and so `GeomBin2dPattern`) now has the same `non_missing_aes` as `ggplot2::GeomTile`.
* The default `linetype` of `GeomSfPattern` is now the theme's `linetype` like that of `ggplot2::GeomSf`
(previously that of a polygon i.e. the theme's `bordertype`).
* 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()`, #158)
instead of copying its grob-construction logic.
Expand Down
17 changes: 17 additions & 0 deletions R/a-geom-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#' so for finer geometry patterns within (small) points use an absolute unit
#' e.g. `pattern_units = "mm"` with a small `pattern_spacing`.
#'
#' `geom_dotplot_pattern()` fills each dot with its own pattern and requires R (>= 4.2.0).
#' Like [ggplot2::geom_dotplot()] its default `fill` is the theme's `ink` colour.
#' `geom_hex_pattern()` requires the suggested package `{hexbin}`.
#'
#' @section Pattern Arguments:
#'
#' Not all arguments apply to all patterns.
Expand Down Expand Up @@ -66,6 +70,19 @@
#' @inheritParams ggplot2::geom_ribbon
#' @inheritParams ggplot2::geom_tile
#' @inheritParams ggplot2::geom_violin
#' @inheritParams ggplot2::geom_contour_filled
#' @inheritParams ggplot2::geom_density_2d_filled
#' @inheritParams ggplot2::geom_dotplot
#' @inheritParams ggplot2::geom_hex
#' @param bins For `geom_histogram_pattern()` the number of bins (overridden by `binwidth`, defaults to 30)
#' and for `geom_contour_filled_pattern()` the number of contour bins (overridden by `breaks`).
#' @param breaks For `geom_contour_filled_pattern()` the contour breaks:
#' a numeric vector or a function of the range of the data and the `binwidth` returning them
#' (see [ggplot2::geom_contour_filled()]). Overrides `binwidth` and `bins`.
#' @param binwidth For `geom_histogram_pattern()` the width of the bins (see [ggplot2::geom_histogram()]),
#' for `geom_dotplot_pattern()` the maximum bin width when `method` is `"dotdensity"`
#' or else the bin width (see [ggplot2::geom_dotplot()]),
#' and for `geom_contour_filled_pattern()` the width of the contour bins (overridden by `bins`).
#' @examples
#' if (require("ggplot2")) {
#'
Expand Down
15 changes: 15 additions & 0 deletions R/aaa-ggplot2-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ binned_pal <- function(palette) {

# Wrapping unique0() to accept NULL
unique0 <- function(x, ...) if (is.null(x)) x else vec_unique(x, ...)

snake_class <- function(x) {
snakeize(class(x)[1])
}

snakeize <- function(x) {
x <- gsub("([A-Za-z])([A-Z])([a-z])", "\\1_\\2\\3", x)
x <- gsub(".", "_", x, fixed = TRUE)
x <- gsub("([a-z])([A-Z])", "\\1_\\2", x)
to_lower_ascii(x)
}

upper_ascii <- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lower_ascii <- "abcdefghijklmnopqrstuvwxyz"
to_lower_ascii <- function(x) chartr(upper_ascii, lower_ascii, x)
12 changes: 10 additions & 2 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pattern_aesthetics <- aes(
#' See `vignette("developing-pattern-geoms", package = "ggpattern")` for writing a pattern geom.
#' @examples
#' names(default_pattern_aes())
#' # e.g. for the `default_aes` of a pattern geom made from `ggplot2::GeomHex`
#' default_aes <- ggplot2::aes(!!!ggplot2::GeomHex$default_aes, !!!default_pattern_aes())
#' # e.g. for the `default_aes` of a pattern geom made from `ggplot2::GeomSmooth`
#' default_aes <- ggplot2::aes(!!!ggplot2::GeomSmooth$default_aes, !!!default_pattern_aes())
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
default_pattern_aes <- function() pattern_aesthetics
Expand Down Expand Up @@ -188,3 +188,11 @@ draw_key_boxplot_pattern <- function(data, params, size, aspect_ratio = get_aspe
draw_key_crossbar_pattern <- function(data, params, size, aspect_ratio = get_aspect_ratio()) {
with_pattern_key(ggplot2::draw_key_crossbar(data, params, size), data, size, aspect_ratio)
}

# Aborts if R is too old for `what` (e.g. to fill areas other than rectangles, polygons, and paths)
check_r_version <- function(what, version = getRversion()) {
if (version < "4.2.0") {
cli::cli_abort("{.fn {what}} requires R (>= 4.2.0) but this is R {version}.", call = NULL)
}
invisible(NULL)
}
13 changes: 13 additions & 0 deletions R/geom-contour.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' @rdname ggpattern-ggproto
#' @format NULL
#' @usage NULL
#' @include geom-polygon.R
#' @export
GeomContourFilledPattern <- ggproto("GeomContourFilledPattern", GeomPolygonPattern)

#' @rdname geom-docs
#' @export
geom_contour_filled_pattern <- make_constructor(
GeomContourFilledPattern, stat = "contour_filled",
bins = NULL, binwidth = NULL, breaks = NULL
)
36 changes: 36 additions & 0 deletions R/geom-density2d.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' @rdname ggpattern-ggproto
#' @format NULL
#' @usage NULL
#' @include geom-polygon.R
#' @export
GeomDensity2dFilledPattern <- ggproto("GeomDensity2dFilledPattern", GeomPolygonPattern)

#' @rdname geom-docs
#' @export
geom_density_2d_filled_pattern <- function(mapping = NULL, data = NULL,
stat = "density_2d_filled", position = "identity",
...,
contour_var = "density",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomDensity2dFilledPattern,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
na.rm = na.rm,
contour = TRUE,
contour_var = contour_var,
...
)
)
}

#' @rdname geom-docs
#' @export
geom_density2d_filled_pattern <- geom_density_2d_filled_pattern
94 changes: 94 additions & 0 deletions R/geom-dotplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#' @rdname ggpattern-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomDotplotPattern <- ggproto("GeomDotplotPattern", GeomDotplot,
default_aes = defaults(GeomDotplot$default_aes, pattern_aesthetics),

setup_params = function(self, data, params) {
check_r_version("geom_dotplot_pattern")
ggproto_parent(GeomDotplot, self)$setup_params(data, params)
},

draw_group = function(self, data, panel_params, coord, lineend = "butt", na.rm = FALSE,
binaxis = "x", stackdir = "up", stackratio = 1,
dotsize = 1, stackgroups = FALSE) {
grob <- ggproto_parent(GeomDotplot, self)$draw_group(
data, panel_params, coord, lineend = lineend, na.rm = na.rm,
binaxis = binaxis, stackdir = stackdir, stackratio = stackratio,
dotsize = dotsize, stackgroups = stackgroups
)
# one dot per row
# (the dots of the `dotstackGrob` are only made when it is drawn so they are filled then)
ggname("geom_dotplot_pattern", with_pattern_aes(grob, data))
},

draw_key = function(data, params, size) {
with_pattern_key(ggplot2::draw_key_dotplot(data, params, size), data, size)
}
)

#' @rdname geom-docs
#' @export
geom_dotplot_pattern <- function(mapping = NULL, data = NULL,
position = "identity",
...,
binwidth = NULL,
binaxis = "x",
method = "dotdensity",
binpositions = "bygroup",
stackdir = "up",
stackratio = 1,
dotsize = 1,
stackgroups = FALSE,
origin = NULL,
right = TRUE,
width = 0.9,
drop = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
# If identical(position, "stack") or position is position_stack(), tell them
# to use stackgroups instead
if (!is.null(position) &&
(identical(position, "stack") || (inherits(position, "PositionStack")))) {
cli::cli_inform(
"{.code position = \"stack\"} doesn't work properly with {.fn geom_dotplot_pattern}. Use {.code stackgroups = TRUE} instead."
)
}

if (stackgroups && method == "dotdensity" && binpositions == "bygroup") {
cli::cli_inform(c(
"{.fn geom_dotplot_pattern} called with {.code stackgroups = TRUE} and {.code method = \"dotdensity\"}.",
i = "Do you want {.code binpositions = \"all\"} instead?"
))
}

stackdir <- arg_match0(stackdir, c("up", "down", "center", "centerwhole"), "stackdir")
layer(
data = data,
mapping = mapping,
stat = StatBindot,
geom = GeomDotplotPattern,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
# Need to make sure that the binaxis goes to both the stat and the geom
params = list2(
binaxis = binaxis,
binwidth = binwidth,
binpositions = binpositions,
method = method,
origin = origin,
right = right,
width = width,
drop = drop,
stackdir = stackdir,
stackratio = stackratio,
dotsize = dotsize,
stackgroups = stackgroups,
na.rm = na.rm,
...
)
)
}
Loading
Loading