From a6df540f698941e984b32b9cc73cab96d41849b1 Mon Sep 17 00:00:00 2001 From: Carson Date: Mon, 30 Nov 2020 15:27:33 -0600 Subject: [PATCH] Allow scale_{colour/fill}_ordinal() to be configured via new ggplot2.ordinal.{colour/fill} global options. Closes #4148 --- R/zxx.r | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/R/zxx.r b/R/zxx.r index cf379281fa..777729550a 100644 --- a/R/zxx.r +++ b/R/zxx.r @@ -3,7 +3,15 @@ #' @export #' @rdname scale_viridis #' @usage NULL -scale_colour_ordinal <- scale_colour_viridis_d +scale_colour_ordinal <- function(..., type = getOption("ggplot2.ordinal.colour", getOption("ggplot2.ordinal.fill"))) { + type <- type %||% scale_colour_viridis_d + if (is.function(type)) { + type(...) + } else { + discrete_scale("colour", "ordinal", ordinal_pal(type), ...) + } +} + #' @export #' @rdname scale_viridis @@ -62,7 +70,21 @@ scale_color_date <- scale_colour_date #' @export #' @rdname scale_viridis #' @usage NULL -scale_fill_ordinal <- scale_fill_viridis_d +scale_fill_ordinal <- function(..., type = getOption("ggplot2.ordinal.fill", getOption("ggplot2.ordinal.colour"))) { + type <- type %||% scale_fill_viridis_d + if (is.function(type)) { + type(...) + } else { + discrete_scale("fill", "ordinal", ordinal_pal(type), ...) + } +} + +ordinal_pal <- function(colours, na.color = "grey50", alpha = TRUE) { + pal <- scales::colour_ramp(colours, na.color = na.color, alpha = alpha) + function(n) { + pal(seq(0, 1, length.out = n)) + } +} #' @export #' @rdname scale_gradient