From 24dc51a55462f0b2f51457667c5ff7e27e3ac84e Mon Sep 17 00:00:00 2001 From: gregleleu <33321678+gregleleu@users.noreply.github.com> Date: Mon, 21 Feb 2022 10:43:39 +0100 Subject: [PATCH 1/2] Use the full range of the palette for binned viridis --- R/scale-viridis.r | 41 +++++++++++++++++++++++++++++------------ man/scale_viridis.Rd | 8 ++++++-- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/R/scale-viridis.r b/R/scale-viridis.r index 1ab871bdfb..6c5b3ec52a 100644 --- a/R/scale-viridis.r +++ b/R/scale-viridis.r @@ -7,6 +7,7 @@ #' #' @inheritParams scales::viridis_pal #' @inheritParams scales::gradient_n_pal +#' @param pal_full_range For the binned versions, use the full range of the palette #' @inheritParams continuous_scale #' @param ... Other arguments passed on to [discrete_scale()], #' [continuous_scale()], or [binned_scale] to control name, limits, breaks, @@ -123,15 +124,23 @@ scale_fill_viridis_c <- function(..., alpha = 1, begin = 0, end = 1, scale_colour_viridis_b <- function(..., alpha = 1, begin = 0, end = 1, direction = 1, option = "D", values = NULL, space = "Lab", na.value = "grey50", - guide = "coloursteps", aesthetics = "colour") { - binned_scale( - aesthetics, - "viridis_b", - gradient_n_pal( + guide = "coloursteps", aesthetics = "colour", pal_full_range = FALSE) { + if(pal_full_range) { + pal <- binned_pal( + viridis_pal(alpha, begin, end, direction, option) + ) + } else { + pal <- gradient_n_pal( viridis_pal(alpha, begin, end, direction, option)(6), values, space - ), + ) + } + + binned_scale( + aesthetics, + "viridis_b", + pal, na.value = na.value, guide = guide, ... @@ -143,15 +152,23 @@ scale_colour_viridis_b <- function(..., alpha = 1, begin = 0, end = 1, scale_fill_viridis_b <- function(..., alpha = 1, begin = 0, end = 1, direction = 1, option = "D", values = NULL, space = "Lab", na.value = "grey50", - guide = "coloursteps", aesthetics = "fill") { - binned_scale( - aesthetics, - "viridis_b", - gradient_n_pal( + guide = "coloursteps", aesthetics = "fill", pal_full_range = FALSE) { + if(pal_full_range) { + pal <- binned_pal( + viridis_pal(alpha, begin, end, direction, option) + ) + } else { + pal <- gradient_n_pal( viridis_pal(alpha, begin, end, direction, option)(6), values, space - ), + ) + } + + binned_scale( + aesthetics, + "viridis_b", + pal, na.value = na.value, guide = guide, ... diff --git a/man/scale_viridis.Rd b/man/scale_viridis.Rd index 12c8ed4192..020ac9b18c 100644 --- a/man/scale_viridis.Rd +++ b/man/scale_viridis.Rd @@ -74,7 +74,8 @@ scale_colour_viridis_b( space = "Lab", na.value = "grey50", guide = "coloursteps", - aesthetics = "colour" + aesthetics = "colour", + pal_full_range = FALSE ) scale_fill_viridis_b( @@ -88,7 +89,8 @@ scale_fill_viridis_b( space = "Lab", na.value = "grey50", guide = "coloursteps", - aesthetics = "fill" + aesthetics = "fill", + pal_full_range = FALSE ) } \arguments{ @@ -127,6 +129,8 @@ other values are deprecated.} \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} + +\item{pal_full_range}{For the binned versions, use the full range of the palette} } \description{ The \code{viridis} scales provide colour maps that are perceptually uniform in both From c8573a340533dcdb479144267217b9c1a259e355 Mon Sep 17 00:00:00 2001 From: gregleleu <33321678+gregleleu@users.noreply.github.com> Date: Tue, 31 May 2022 11:14:21 -0400 Subject: [PATCH 2/2] Switch to full-range behavior Removes argument to choose between behaviors --- NEWS.md | 2 ++ R/scale-viridis.r | 33 ++++++++------------------------- man/scale_viridis.Rd | 40 ++++++++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6579c613b1..6ec7f1161a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -53,6 +53,8 @@ * `geom_contour()` now accepts a function in the `breaks` argument (@eliocamp, #4652). +* VISUAL CHANGE: `scale_*_viridis_b()` now uses the full range of the viridis scales (@gregleleu, #4737) + # ggplot2 3.3.5 This is a very small release focusing on fixing a couple of untenable issues that surfaced with the 3.3.4 release diff --git a/R/scale-viridis.r b/R/scale-viridis.r index 6c5b3ec52a..707cffa4a8 100644 --- a/R/scale-viridis.r +++ b/R/scale-viridis.r @@ -7,7 +7,6 @@ #' #' @inheritParams scales::viridis_pal #' @inheritParams scales::gradient_n_pal -#' @param pal_full_range For the binned versions, use the full range of the palette #' @inheritParams continuous_scale #' @param ... Other arguments passed on to [discrete_scale()], #' [continuous_scale()], or [binned_scale] to control name, limits, breaks, @@ -124,18 +123,10 @@ scale_fill_viridis_c <- function(..., alpha = 1, begin = 0, end = 1, scale_colour_viridis_b <- function(..., alpha = 1, begin = 0, end = 1, direction = 1, option = "D", values = NULL, space = "Lab", na.value = "grey50", - guide = "coloursteps", aesthetics = "colour", pal_full_range = FALSE) { - if(pal_full_range) { - pal <- binned_pal( - viridis_pal(alpha, begin, end, direction, option) - ) - } else { - pal <- gradient_n_pal( - viridis_pal(alpha, begin, end, direction, option)(6), - values, - space - ) - } + guide = "coloursteps", aesthetics = "colour") { + pal <- binned_pal( + viridis_pal(alpha, begin, end, direction, option) + ) binned_scale( aesthetics, @@ -152,18 +143,10 @@ scale_colour_viridis_b <- function(..., alpha = 1, begin = 0, end = 1, scale_fill_viridis_b <- function(..., alpha = 1, begin = 0, end = 1, direction = 1, option = "D", values = NULL, space = "Lab", na.value = "grey50", - guide = "coloursteps", aesthetics = "fill", pal_full_range = FALSE) { - if(pal_full_range) { - pal <- binned_pal( - viridis_pal(alpha, begin, end, direction, option) - ) - } else { - pal <- gradient_n_pal( - viridis_pal(alpha, begin, end, direction, option)(6), - values, - space - ) - } + guide = "coloursteps", aesthetics = "fill") { + pal <- binned_pal( + viridis_pal(alpha, begin, end, direction, option) + ) binned_scale( aesthetics, diff --git a/man/scale_viridis.Rd b/man/scale_viridis.Rd index 020ac9b18c..84b7e95656 100644 --- a/man/scale_viridis.Rd +++ b/man/scale_viridis.Rd @@ -74,8 +74,7 @@ scale_colour_viridis_b( space = "Lab", na.value = "grey50", guide = "coloursteps", - aesthetics = "colour", - pal_full_range = FALSE + aesthetics = "colour" ) scale_fill_viridis_b( @@ -89,8 +88,7 @@ scale_fill_viridis_b( space = "Lab", na.value = "grey50", guide = "coloursteps", - aesthetics = "fill", - pal_full_range = FALSE + aesthetics = "fill" ) } \arguments{ @@ -101,16 +99,28 @@ labels and so forth.} \item{alpha}{The alpha transparency, a number in [0,1], see argument alpha in \code{\link[grDevices]{hsv}}.} -\item{begin}{The (corrected) hue in [0,1] at which the viridis colormap begins.} - -\item{end}{The (corrected) hue in [0,1] at which the viridis colormap ends.} - -\item{direction}{Sets the order of colors in the scale. If 1, the default, colors -are ordered from darkest to lightest. If -1, the order of colors is reversed.} - -\item{option}{A character string indicating the colormap option to use. Four -options are available: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), -"viridis" (or "D", the default option) and "cividis" (or "E").} +\item{begin}{The (corrected) hue in \verb{[0,1]} at which the color map +begins and ends.} + +\item{end}{The (corrected) hue in \verb{[0,1]} at which the color map +begins and ends.} + +\item{direction}{Sets the order of colors in the scale. If 1, the default, +colors are ordered from darkest to lightest. If -1, the order of colors is +reversed.} + +\item{option}{A character string indicating the color map option to use. +Eight options are available: +\itemize{ + \item "magma" (or "A") + \item "inferno" (or "B") + \item "plasma" (or "C") + \item "viridis" (or "D") + \item "cividis" (or "E") + \item "rocket" (or "F") + \item "mako" (or "G") + \item "turbo" (or "H") +}} \item{aesthetics}{Character string or vector of character strings listing the name(s) of the aesthetic(s) that this scale works with. This can be useful, for @@ -129,8 +139,6 @@ other values are deprecated.} \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} - -\item{pal_full_range}{For the binned versions, use the full range of the palette} } \description{ The \code{viridis} scales provide colour maps that are perceptually uniform in both