Skip to content

Commit ad540b7

Browse files
authored
Barebones support for <GridPattern> fills. (#5299)
* Write pattern utilities * Intercept non-list patterns * Support pattern fills in geoms * Support pattern fills in keys * Note that `geom_raster()` cannot use pattern fills * More informative call in error message * Write tests * Document * Some version protections * Use device checker * Set white alpha mask * Clarify error message * deal with unavailable functions/arguments * typo * Also handle unlisted pattern * Invert viewport backport * `geom_raster()` throws error when fill is pattern * device check warns instead of aborts * reimplement `pattern_alpha` as S3 generic + methods * accept new snapshot * Add news bullet
1 parent 15bde2f commit ad540b7

29 files changed

+883
-18
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Collate:
275275
'utilities-grid.R'
276276
'utilities-help.R'
277277
'utilities-matrix.R'
278+
'utilities-patterns.R'
278279
'utilities-resolution.R'
279280
'utilities-tidy-eval.R'
280281
'zxx.R'

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ S3method(makeContext,dotstackGrob)
9595
S3method(merge_element,default)
9696
S3method(merge_element,element)
9797
S3method(merge_element,element_blank)
98+
S3method(pattern_alpha,GridPattern)
99+
S3method(pattern_alpha,GridTilingPattern)
100+
S3method(pattern_alpha,default)
101+
S3method(pattern_alpha,list)
98102
S3method(plot,ggplot)
99103
S3method(predictdf,default)
100104
S3method(predictdf,glm)
@@ -354,6 +358,7 @@ export(expr)
354358
export(facet_grid)
355359
export(facet_null)
356360
export(facet_wrap)
361+
export(fill_alpha)
357362
export(find_panel)
358363
export(flip_data)
359364
export(flipped_names)
@@ -476,6 +481,7 @@ export(new_guide)
476481
export(old_guide)
477482
export(panel_cols)
478483
export(panel_rows)
484+
export(pattern_alpha)
479485
export(position_dodge)
480486
export(position_dodge2)
481487
export(position_fill)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ggplot2 (development version)
22

3+
* The `fill` aesthetic in many geoms now accepts grid's patterns and gradients.
4+
For developers of layer extensions, this feature can be enabled by switching
5+
from `fill = alpha(fill, alpha)` to `fill = fill_alpha(fill, alpha)` when
6+
providing fills to `grid::gpar()` (@teunbrand, #3997).
7+
38
* The plot's title, subtitle and caption now obey horizontal text margins
49
(#5533).
510

R/backports.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,26 @@ if (getRversion() < "3.5") {
2222
isFALSE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x
2323
isTRUE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && x
2424
}
25+
26+
version_unavailable <- function(...) {
27+
fun <- as_label(current_call()[[1]])
28+
cli::cli_abort("{.fn {fun}} is not available in R version {getRversion()}.")
29+
}
30+
31+
# Ignore mask argument if on lower R version (<= 4.1)
32+
viewport <- function(..., mask) grid::viewport(...)
33+
pattern <- version_unavailable
34+
as.mask <- version_unavailable
35+
on_load({
36+
if ("mask" %in% fn_fmls_names(grid::viewport)) {
37+
viewport <- grid::viewport
38+
}
39+
# Replace version unavailable functions if found
40+
if ("pattern" %in% getNamespaceExports("grid")) {
41+
pattern <- grid::pattern
42+
}
43+
if ("as.mask" %in% getNamespaceExports("grid")) {
44+
as.mask <- grid::as.mask
45+
}
46+
})
47+

R/geom-.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ Geom <- ggproto("Geom",
126126
deprecate_soft0("3.4.0", I("Using the `size` aesthetic in this geom"), I("`linewidth` in the `default_aes` field and elsewhere"))
127127
default_aes$linewidth <- default_aes$size
128128
}
129+
if (is_pattern(params$fill)) {
130+
params$fill <- list(params$fill)
131+
}
132+
129133
# Fill in missing aesthetics with their defaults
130134
missing_aes <- setdiff(names(default_aes), names(data))
131135

R/geom-boxplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
239239
colour = data$colour,
240240
linewidth = data$linewidth,
241241
linetype = data$linetype,
242-
fill = alpha(data$fill, data$alpha),
242+
fill = fill_alpha(data$fill, data$alpha),
243243
group = data$group
244244
)
245245

R/geom-dotplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
294294
stackposition = tdata$stackpos, stackdir = stackdir, stackratio = stackratio,
295295
default.units = "npc",
296296
gp = gpar(col = alpha(tdata$colour, tdata$alpha),
297-
fill = alpha(tdata$fill, tdata$alpha),
297+
fill = fill_alpha(tdata$fill, tdata$alpha),
298298
lwd = tdata$stroke, lty = tdata$linetype,
299299
lineend = lineend))
300300
)

R/geom-hex.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ GeomHex <- ggproto("GeomHex", Geom,
9090
coords$x, coords$y,
9191
gp = gpar(
9292
col = data$colour,
93-
fill = alpha(data$fill, data$alpha),
93+
fill = fill_alpha(data$fill, data$alpha),
9494
lwd = data$linewidth * .pt,
9595
lty = data$linetype,
9696
lineend = lineend,

R/geom-label.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ GeomLabel <- ggproto("GeomLabel", Geom,
103103
),
104104
rect.gp = gpar(
105105
col = if (isTRUE(all.equal(label.size, 0))) NA else row$colour,
106-
fill = alpha(row$fill, row$alpha),
106+
fill = fill_alpha(row$fill, row$alpha),
107107
lwd = label.size * .pt
108108
)
109109
)

R/geom-map.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ GeomMap <- ggproto("GeomMap", GeomPolygon,
146146
polygonGrob(coords$x, coords$y, default.units = "native", id = grob_id,
147147
gp = gpar(
148148
col = data$colour,
149-
fill = alpha(data$fill, data$alpha),
149+
fill = fill_alpha(data$fill, data$alpha),
150150
lwd = data$linewidth * .pt,
151151
lineend = lineend,
152152
linejoin = linejoin,

0 commit comments

Comments
 (0)