diff --git a/NEWS.md b/NEWS.md index 71f471a2fc..c91420661f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* `guide_colourbar()` and `guide_coloursteps()` gain an `alpha` argument to + set the transparency of the bar (#5085). + * `stat_count()` treats `x` as unique in the same manner `unique()` does (#4609). diff --git a/R/guide-colorbar.R b/R/guide-colorbar.R index 9fdc92c437..0baba64587 100644 --- a/R/guide-colorbar.R +++ b/R/guide-colorbar.R @@ -21,6 +21,9 @@ NULL #' raster object. If `FALSE` then the colourbar is rendered as a set of #' rectangles. Note that not all graphics devices are capable of rendering #' raster image. +#' @param alpha A numeric between 0 and 1 setting the colour transparency of +#' the bar. Use `NA` to preserve the alpha encoded in the colour itself +#' (default). #' @param draw.ulim A logical specifying if the upper limit tick marks should #' be visible. #' @param draw.llim A logical specifying if the lower limit tick marks should @@ -107,6 +110,7 @@ guide_colourbar <- function( theme = NULL, nbin = 300, raster = TRUE, + alpha = NA, draw.ulim = TRUE, draw.llim = TRUE, position = NULL, @@ -121,12 +125,14 @@ guide_colourbar <- function( if (!is.null(position)) { position <- arg_match0(position, c(.trbl, "inside")) } + check_number_decimal(alpha, min = 0, max = 1, allow_na = TRUE) new_guide( title = title, theme = theme, nbin = nbin, raster = raster, + alpha = alpha, draw_lim = c(isTRUE(draw.llim), isTRUE(draw.ulim)), position = position, direction = direction, @@ -162,6 +168,7 @@ GuideColourbar <- ggproto( # bar nbin = 300, raster = TRUE, + alpha = NA, draw_lim = c(TRUE, TRUE), @@ -204,7 +211,7 @@ GuideColourbar <- ggproto( Guide$extract_key(scale, aesthetic, ...) }, - extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, ...) { + extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, alpha = NA, ...) { limits <- scale$get_limits() bar <- seq(limits[1], limits[2], length.out = nbin) @@ -212,7 +219,7 @@ GuideColourbar <- ggproto( bar <- unique0(limits) } bar <- data_frame0( - colour = scale$map(bar), + colour = alpha(scale$map(bar), alpha), value = bar, .size = length(bar) ) diff --git a/R/guide-colorsteps.R b/R/guide-colorsteps.R index 45d0bed2d7..06dd277a3e 100644 --- a/R/guide-colorsteps.R +++ b/R/guide-colorsteps.R @@ -46,6 +46,7 @@ guide_coloursteps <- function( title = waiver(), theme = NULL, + alpha = NA, even.steps = TRUE, show.limits = NULL, direction = NULL, @@ -56,10 +57,12 @@ guide_coloursteps <- function( ) { theme <- deprecated_guide_args(theme, ...) + check_number_decimal(alpha, min = 0, max = 1, allow_na = TRUE) new_guide( title = title, theme = theme, + alpha = alpha, even.steps = even.steps, show.limits = show.limits, direction = direction, @@ -120,11 +123,11 @@ GuideColoursteps <- ggproto( extract_decor = function(scale, aesthetic, key, reverse = FALSE, even.steps = TRUE, - nbin = 100, ...) { + nbin = 100, alpha = NA,...) { if (even.steps) { bin_at <- attr(key, "bin_at", TRUE) bar <- data_frame0( - colour = scale$map(bin_at), + colour = alpha(scale$map(bin_at), alpha), min = seq_along(bin_at) - 1, max = seq_along(bin_at), .size = length(bin_at) @@ -134,7 +137,7 @@ GuideColoursteps <- ggproto( n <- length(breaks) bin_at <- (breaks[-1] + breaks[-n]) / 2 bar <- data_frame0( - colour = scale$map(bin_at), + colour = alpha(scale$map(bin_at), alpha), min = head(breaks, -1), max = tail(breaks, -1), .size = length(bin_at) diff --git a/man/guide_colourbar.Rd b/man/guide_colourbar.Rd index 2078bc13db..ca78b8765e 100644 --- a/man/guide_colourbar.Rd +++ b/man/guide_colourbar.Rd @@ -10,6 +10,7 @@ guide_colourbar( theme = NULL, nbin = 300, raster = TRUE, + alpha = NA, draw.ulim = TRUE, draw.llim = TRUE, position = NULL, @@ -25,6 +26,7 @@ guide_colorbar( theme = NULL, nbin = 300, raster = TRUE, + alpha = NA, draw.ulim = TRUE, draw.llim = TRUE, position = NULL, @@ -53,6 +55,10 @@ raster object. If \code{FALSE} then the colourbar is rendered as a set of rectangles. Note that not all graphics devices are capable of rendering raster image.} +\item{alpha}{A numeric between 0 and 1 setting the colour transparency of +the bar. Use \code{NA} to preserve the alpha encoded in the colour itself +(default).} + \item{draw.ulim}{A logical specifying if the upper limit tick marks should be visible.} diff --git a/man/guide_coloursteps.Rd b/man/guide_coloursteps.Rd index 3df628de34..6a2d1e96a1 100644 --- a/man/guide_coloursteps.Rd +++ b/man/guide_coloursteps.Rd @@ -8,6 +8,7 @@ guide_coloursteps( title = waiver(), theme = NULL, + alpha = NA, even.steps = TRUE, show.limits = NULL, direction = NULL, @@ -20,6 +21,7 @@ guide_coloursteps( guide_colorsteps( title = waiver(), theme = NULL, + alpha = NA, even.steps = TRUE, show.limits = NULL, direction = NULL, @@ -39,6 +41,10 @@ specified in \code{\link[=labs]{labs()}} is used for the title.} differently from the plot's theme settings. The \code{theme} argument in the guide overrides, and is combined with, the plot's theme.} +\item{alpha}{A numeric between 0 and 1 setting the colour transparency of +the bar. Use \code{NA} to preserve the alpha encoded in the colour itself +(default).} + \item{even.steps}{Should the rendered size of the bins be equal, or should they be proportional to their length in the data space? Defaults to \code{TRUE}} diff --git a/tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg b/tests/testthat/_snaps/guides/guide-bins-can-show-ticks-and-transparancy.svg similarity index 94% rename from tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg rename to tests/testthat/_snaps/guides/guide-bins-can-show-ticks-and-transparancy.svg index 27ba63dfb1..824123d482 100644 --- a/tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg +++ b/tests/testthat/_snaps/guides/guide-bins-can-show-ticks-and-transparancy.svg @@ -55,10 +55,10 @@ y x - - - - + + + + @@ -68,6 +68,6 @@ 1.5 2.0 3.0 -guide_bins can show ticks +guide_bins can show ticks and transparancy diff --git a/tests/testthat/_snaps/guides/white-to-red-colorbar-long-thick-black-ticks-green-frame.svg b/tests/testthat/_snaps/guides/white-to-red-semitransparent-colorbar-long-thick-black-ticks-green-frame.svg similarity index 95% rename from tests/testthat/_snaps/guides/white-to-red-colorbar-long-thick-black-ticks-green-frame.svg rename to tests/testthat/_snaps/guides/white-to-red-semitransparent-colorbar-long-thick-black-ticks-green-frame.svg index e5839bc3ff..6d07362244 100644 --- a/tests/testthat/_snaps/guides/white-to-red-colorbar-long-thick-black-ticks-green-frame.svg +++ b/tests/testthat/_snaps/guides/white-to-red-semitransparent-colorbar-long-thick-black-ticks-green-frame.svg @@ -57,7 +57,7 @@ x x - + @@ -74,6 +74,6 @@ 1.0 1.5 2.0 -white-to-red colorbar, long thick black ticks, green frame +white-to-red semitransparent colorbar, long thick black ticks, green frame diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index e258a5a7ed..2eaa859efe 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -900,7 +900,7 @@ test_that("colorbar can be styled", { p + scale_color_gradient(low = 'white', high = 'red') ) - expect_doppelganger("white-to-red colorbar, long thick black ticks, green frame", + expect_doppelganger("white-to-red semitransparent colorbar, long thick black ticks, green frame", p + scale_color_gradient( low = 'white', high = 'red', guide = guide_colorbar( @@ -908,7 +908,7 @@ test_that("colorbar can be styled", { legend.frame = element_rect(colour = "green", linewidth = 1.5 / .pt), legend.ticks = element_line("black", linewidth = 2.5 / .pt), legend.ticks.length = unit(0.4, "npc") - ) + ), alpha = 0.75 ) ) ) @@ -977,8 +977,9 @@ test_that("coloursteps guide can be styled correctly", { expect_doppelganger("guide_coloursteps can have bins relative to binsize", p + guides(colour = guide_coloursteps(even.steps = FALSE)) ) - expect_doppelganger("guide_bins can show ticks", + expect_doppelganger("guide_bins can show ticks and transparancy", p + guides(colour = guide_coloursteps( + alpha = 0.75, theme = theme(legend.ticks = element_line(linewidth = 0.5 / .pt, colour = "white")) )) )