Skip to content

Commit dd7887f

Browse files
authored
Gradient as colourbar (#5548)
* deprecate `raster` in favour of `display` * Use 15 colours as default for gradient * Don't account for bins to offset labels * add gradient display * deal with old R versions * document options * use `arg_match0()` * Add news bullet
1 parent 4d7e202 commit dd7887f

File tree

4 files changed

+73
-25
lines changed

4 files changed

+73
-25
lines changed

NEWS.md

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

3+
* New `display` argument in `guide_colourbar()` supplants the `raster` argument.
4+
In R 4.1.0 and above, `display = "gradient"` will draw a gradient.
35
* When using `geom_dotplot(binaxis = "x")` with a discrete y-variable, dots are
46
now stacked from the y-position rather than from 0 (@teunbrand, #5462)
57

R/backports.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ version_unavailable <- function(...) {
5252
viewport <- function(..., mask) grid::viewport(...)
5353
pattern <- version_unavailable
5454
as.mask <- version_unavailable
55+
# Unavailable prior to R 4.1.0
56+
linearGradient <- version_unavailable
57+
5558
on_load({
5659
if ("mask" %in% fn_fmls_names(grid::viewport)) {
5760
viewport <- grid::viewport
@@ -63,5 +66,7 @@ on_load({
6366
if ("as.mask" %in% getNamespaceExports("grid")) {
6467
as.mask <- grid::as.mask
6568
}
69+
if ("linearGradient" %in% getNamespaceExports("grid")) {
70+
linearGradient <- grid::linearGradient()
71+
}
6672
})
67-

R/guide-colorbar.R

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ NULL
1717
#' @inheritParams guide_legend
1818
#' @param nbin A numeric specifying the number of bins for drawing the
1919
#' colourbar. A smoother colourbar results from a larger value.
20-
#' @param raster A logical. If `TRUE` then the colourbar is rendered as a
21-
#' raster object. If `FALSE` then the colourbar is rendered as a set of
22-
#' rectangles. Note that not all graphics devices are capable of rendering
23-
#' raster image.
20+
#' @param display A string indicating a method to display the colourbar. Can be
21+
#' one of the following:
22+
#'
23+
#' * `"raster"` to display as a bitmap image.
24+
#' * `"rectangles"` to display as a series of rectangles.
25+
#' * `"gradient"` to display as a linear gradient.
26+
#'
27+
#' Note that not all devices are able to render rasters and gradients.
28+
#' @param raster `r lifecycle::badge("deprecated")` A logical. If `TRUE` then
29+
#' the colourbar is rendered as a raster object. If `FALSE` then the colourbar
30+
#' is rendered as a set of rectangles. Note that not all graphics devices are
31+
#' capable of rendering raster image.
2432
#' @param alpha A numeric between 0 and 1 setting the colour transparency of
2533
#' the bar. Use `NA` to preserve the alpha encoded in the colour itself
2634
#' (default).
@@ -108,8 +116,9 @@ NULL
108116
guide_colourbar <- function(
109117
title = waiver(),
110118
theme = NULL,
111-
nbin = 300,
112-
raster = TRUE,
119+
nbin = NULL,
120+
display = "raster",
121+
raster = deprecated(),
113122
alpha = NA,
114123
draw.ulim = TRUE,
115124
draw.llim = TRUE,
@@ -120,6 +129,13 @@ guide_colourbar <- function(
120129
available_aes = c("colour", "color", "fill"),
121130
...
122131
) {
132+
if (lifecycle::is_present(raster)) {
133+
deprecate_soft0("3.5.0", "guide_colourbar(raster)", "guide_colourbar(display)")
134+
check_bool(raster)
135+
display <- if (raster) "raster" else "rectangles"
136+
}
137+
display <- arg_match0(display, c("raster", "rectangles", "gradient"))
138+
nbin <- nbin %||% switch(display, gradient = 15, 300)
123139

124140
theme <- deprecated_guide_args(theme, ...)
125141
if (!is.null(position)) {
@@ -131,7 +147,7 @@ guide_colourbar <- function(
131147
title = title,
132148
theme = theme,
133149
nbin = nbin,
134-
raster = raster,
150+
display = display,
135151
alpha = alpha,
136152
draw_lim = c(isTRUE(draw.llim), isTRUE(draw.ulim)),
137153
position = position,
@@ -167,7 +183,7 @@ GuideColourbar <- ggproto(
167183

168184
# bar
169185
nbin = 300,
170-
raster = TRUE,
186+
display = "raster",
171187
alpha = NA,
172188

173189
draw_lim = c(TRUE, TRUE),
@@ -232,13 +248,13 @@ GuideColourbar <- ggproto(
232248
extract_params = function(scale, params,
233249
title = waiver(), ...) {
234250
params$title <- scale$make_title(params$title %|W|% scale$name %|W|% title)
235-
236251
limits <- params$decor$value[c(1L, nrow(params$decor))]
237-
params$key$.value <- rescale(
238-
params$key$.value,
239-
c(0.5, params$nbin - 0.5) / params$nbin,
240-
limits
252+
to <- switch(
253+
params$display,
254+
gradient = c(0, 1),
255+
c(0.5, params$nbin - 0.5) / params$nbin
241256
)
257+
params$key$.value <- rescale(params$key$.value, to = to, from = limits)
242258
params
243259
},
244260

@@ -328,8 +344,7 @@ GuideColourbar <- ggproto(
328344
},
329345

330346
build_decor = function(decor, grobs, elements, params) {
331-
332-
if (params$raster) {
347+
if (params$display == "raster") {
333348
image <- switch(
334349
params$direction,
335350
"horizontal" = t(decor$colour),
@@ -343,7 +358,7 @@ GuideColourbar <- ggproto(
343358
gp = gpar(col = NA),
344359
interpolate = TRUE
345360
)
346-
} else{
361+
} else if (params$display == "rectangles") {
347362
if (params$direction == "horizontal") {
348363
width <- 1 / nrow(decor)
349364
height <- 1
@@ -362,6 +377,20 @@ GuideColourbar <- ggproto(
362377
default.units = "npc",
363378
gp = gpar(col = NA, fill = decor$colour)
364379
)
380+
} else if (params$display == "gradient") {
381+
check_device("gradients", call = expr(guide_colourbar()))
382+
value <- if (isTRUE(params$reverse)) {
383+
rescale(decor$value, to = c(1, 0))
384+
} else {
385+
rescale(decor$value, to = c(0, 1))
386+
}
387+
position <- switch(
388+
params$direction,
389+
horizontal = list(y1 = unit(0.5, "npc"), y2 = unit(0.5, "npc")),
390+
vertical = list(x1 = unit(0.5, "npc"), x2 = unit(0.5, "npc"))
391+
)
392+
gradient <- inject(linearGradient(decor$colour, value, !!!position))
393+
grob <- rectGrob(gp = gpar(fill = gradient, col = NA))
365394
}
366395

367396
frame <- element_grob(elements$frame, fill = NA)

man/guide_colourbar.Rd

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)