Skip to content

Commit 9d1f81c

Browse files
authored
Colourbar transparency (#5550)
* Add `alpha` to set colourbar/steps transparency * incorporate `alpha` in tests * document `alpha` * add news bullet * resolve merge conflict * add alpha to coloursteps (constructors were separated)
1 parent abc70e2 commit 9d1f81c

8 files changed

+41
-15
lines changed

NEWS.md

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

3+
* `guide_colourbar()` and `guide_coloursteps()` gain an `alpha` argument to
4+
set the transparency of the bar (#5085).
5+
36
* `stat_count()` treats `x` as unique in the same manner `unique()` does
47
(#4609).
58

R/guide-colorbar.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ NULL
2121
#' raster object. If `FALSE` then the colourbar is rendered as a set of
2222
#' rectangles. Note that not all graphics devices are capable of rendering
2323
#' raster image.
24+
#' @param alpha A numeric between 0 and 1 setting the colour transparency of
25+
#' the bar. Use `NA` to preserve the alpha encoded in the colour itself
26+
#' (default).
2427
#' @param draw.ulim A logical specifying if the upper limit tick marks should
2528
#' be visible.
2629
#' @param draw.llim A logical specifying if the lower limit tick marks should
@@ -107,6 +110,7 @@ guide_colourbar <- function(
107110
theme = NULL,
108111
nbin = 300,
109112
raster = TRUE,
113+
alpha = NA,
110114
draw.ulim = TRUE,
111115
draw.llim = TRUE,
112116
position = NULL,
@@ -121,12 +125,14 @@ guide_colourbar <- function(
121125
if (!is.null(position)) {
122126
position <- arg_match0(position, c(.trbl, "inside"))
123127
}
128+
check_number_decimal(alpha, min = 0, max = 1, allow_na = TRUE)
124129

125130
new_guide(
126131
title = title,
127132
theme = theme,
128133
nbin = nbin,
129134
raster = raster,
135+
alpha = alpha,
130136
draw_lim = c(isTRUE(draw.llim), isTRUE(draw.ulim)),
131137
position = position,
132138
direction = direction,
@@ -162,6 +168,7 @@ GuideColourbar <- ggproto(
162168
# bar
163169
nbin = 300,
164170
raster = TRUE,
171+
alpha = NA,
165172

166173
draw_lim = c(TRUE, TRUE),
167174

@@ -204,15 +211,15 @@ GuideColourbar <- ggproto(
204211
Guide$extract_key(scale, aesthetic, ...)
205212
},
206213

207-
extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, ...) {
214+
extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, alpha = NA, ...) {
208215

209216
limits <- scale$get_limits()
210217
bar <- seq(limits[1], limits[2], length.out = nbin)
211218
if (length(bar) == 0) {
212219
bar <- unique0(limits)
213220
}
214221
bar <- data_frame0(
215-
colour = scale$map(bar),
222+
colour = alpha(scale$map(bar), alpha),
216223
value = bar,
217224
.size = length(bar)
218225
)

R/guide-colorsteps.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
guide_coloursteps <- function(
4747
title = waiver(),
4848
theme = NULL,
49+
alpha = NA,
4950
even.steps = TRUE,
5051
show.limits = NULL,
5152
direction = NULL,
@@ -56,10 +57,12 @@ guide_coloursteps <- function(
5657
) {
5758

5859
theme <- deprecated_guide_args(theme, ...)
60+
check_number_decimal(alpha, min = 0, max = 1, allow_na = TRUE)
5961

6062
new_guide(
6163
title = title,
6264
theme = theme,
65+
alpha = alpha,
6366
even.steps = even.steps,
6467
show.limits = show.limits,
6568
direction = direction,
@@ -120,11 +123,11 @@ GuideColoursteps <- ggproto(
120123

121124
extract_decor = function(scale, aesthetic, key,
122125
reverse = FALSE, even.steps = TRUE,
123-
nbin = 100, ...) {
126+
nbin = 100, alpha = NA,...) {
124127
if (even.steps) {
125128
bin_at <- attr(key, "bin_at", TRUE)
126129
bar <- data_frame0(
127-
colour = scale$map(bin_at),
130+
colour = alpha(scale$map(bin_at), alpha),
128131
min = seq_along(bin_at) - 1,
129132
max = seq_along(bin_at),
130133
.size = length(bin_at)
@@ -134,7 +137,7 @@ GuideColoursteps <- ggproto(
134137
n <- length(breaks)
135138
bin_at <- (breaks[-1] + breaks[-n]) / 2
136139
bar <- data_frame0(
137-
colour = scale$map(bin_at),
140+
colour = alpha(scale$map(bin_at), alpha),
138141
min = head(breaks, -1),
139142
max = tail(breaks, -1),
140143
.size = length(bin_at)

man/guide_colourbar.Rd

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

man/guide_coloursteps.Rd

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

tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg renamed to tests/testthat/_snaps/guides/guide-bins-can-show-ticks-and-transparancy.svg

Lines changed: 5 additions & 5 deletions
Loading

tests/testthat/_snaps/guides/white-to-red-colorbar-long-thick-black-ticks-green-frame.svg renamed to tests/testthat/_snaps/guides/white-to-red-semitransparent-colorbar-long-thick-black-ticks-green-frame.svg

Lines changed: 2 additions & 2 deletions
Loading

tests/testthat/test-guides.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,15 +900,15 @@ test_that("colorbar can be styled", {
900900
p + scale_color_gradient(low = 'white', high = 'red')
901901
)
902902

903-
expect_doppelganger("white-to-red colorbar, long thick black ticks, green frame",
903+
expect_doppelganger("white-to-red semitransparent colorbar, long thick black ticks, green frame",
904904
p + scale_color_gradient(
905905
low = 'white', high = 'red',
906906
guide = guide_colorbar(
907907
theme = theme(
908908
legend.frame = element_rect(colour = "green", linewidth = 1.5 / .pt),
909909
legend.ticks = element_line("black", linewidth = 2.5 / .pt),
910910
legend.ticks.length = unit(0.4, "npc")
911-
)
911+
), alpha = 0.75
912912
)
913913
)
914914
)
@@ -977,8 +977,9 @@ test_that("coloursteps guide can be styled correctly", {
977977
expect_doppelganger("guide_coloursteps can have bins relative to binsize",
978978
p + guides(colour = guide_coloursteps(even.steps = FALSE))
979979
)
980-
expect_doppelganger("guide_bins can show ticks",
980+
expect_doppelganger("guide_bins can show ticks and transparancy",
981981
p + guides(colour = guide_coloursteps(
982+
alpha = 0.75,
982983
theme = theme(legend.ticks = element_line(linewidth = 0.5 / .pt, colour = "white"))
983984
))
984985
)

0 commit comments

Comments
 (0)