17
17
# ' @inheritParams guide_legend
18
18
# ' @param nbin A numeric specifying the number of bins for drawing the
19
19
# ' 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.
24
32
# ' @param alpha A numeric between 0 and 1 setting the colour transparency of
25
33
# ' the bar. Use `NA` to preserve the alpha encoded in the colour itself
26
34
# ' (default).
108
116
guide_colourbar <- function (
109
117
title = waiver(),
110
118
theme = NULL ,
111
- nbin = 300 ,
112
- raster = TRUE ,
119
+ nbin = NULL ,
120
+ display = " raster" ,
121
+ raster = deprecated(),
113
122
alpha = NA ,
114
123
draw.ulim = TRUE ,
115
124
draw.llim = TRUE ,
@@ -120,6 +129,13 @@ guide_colourbar <- function(
120
129
available_aes = c(" colour" , " color" , " fill" ),
121
130
...
122
131
) {
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 )
123
139
124
140
theme <- deprecated_guide_args(theme , ... )
125
141
if (! is.null(position )) {
@@ -131,7 +147,7 @@ guide_colourbar <- function(
131
147
title = title ,
132
148
theme = theme ,
133
149
nbin = nbin ,
134
- raster = raster ,
150
+ display = display ,
135
151
alpha = alpha ,
136
152
draw_lim = c(isTRUE(draw.llim ), isTRUE(draw.ulim )),
137
153
position = position ,
@@ -167,7 +183,7 @@ GuideColourbar <- ggproto(
167
183
168
184
# bar
169
185
nbin = 300 ,
170
- raster = TRUE ,
186
+ display = " raster " ,
171
187
alpha = NA ,
172
188
173
189
draw_lim = c(TRUE , TRUE ),
@@ -232,13 +248,13 @@ GuideColourbar <- ggproto(
232
248
extract_params = function (scale , params ,
233
249
title = waiver(), ... ) {
234
250
params $ title <- scale $ make_title(params $ title %| W | % scale $ name %| W | % title )
235
-
236
251
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
241
256
)
257
+ params $ key $ .value <- rescale(params $ key $ .value , to = to , from = limits )
242
258
params
243
259
},
244
260
@@ -328,8 +344,7 @@ GuideColourbar <- ggproto(
328
344
},
329
345
330
346
build_decor = function (decor , grobs , elements , params ) {
331
-
332
- if (params $ raster ) {
347
+ if (params $ display == " raster" ) {
333
348
image <- switch (
334
349
params $ direction ,
335
350
" horizontal" = t(decor $ colour ),
@@ -343,7 +358,7 @@ GuideColourbar <- ggproto(
343
358
gp = gpar(col = NA ),
344
359
interpolate = TRUE
345
360
)
346
- } else {
361
+ } else if ( params $ display == " rectangles " ) {
347
362
if (params $ direction == " horizontal" ) {
348
363
width <- 1 / nrow(decor )
349
364
height <- 1
@@ -362,6 +377,20 @@ GuideColourbar <- ggproto(
362
377
default.units = " npc" ,
363
378
gp = gpar(col = NA , fill = decor $ colour )
364
379
)
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 ))
365
394
}
366
395
367
396
frame <- element_grob(elements $ frame , fill = NA )
0 commit comments