Skip to content

Commit 0990114

Browse files
authored
Allow scale_{colour/fill}_ordinal() to be configured via new ggplot2.ordinal.{colour/fill} global options. Closes #4148 (#4277)
1 parent 23baab0 commit 0990114

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

R/zxx.r

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
#' @export
44
#' @rdname scale_viridis
55
#' @usage NULL
6-
scale_colour_ordinal <- scale_colour_viridis_d
6+
scale_colour_ordinal <- function(..., type = getOption("ggplot2.ordinal.colour", getOption("ggplot2.ordinal.fill"))) {
7+
type <- type %||% scale_colour_viridis_d
8+
if (is.function(type)) {
9+
type(...)
10+
} else {
11+
discrete_scale("colour", "ordinal", ordinal_pal(type), ...)
12+
}
13+
}
14+
715

816
#' @export
917
#' @rdname scale_viridis
@@ -62,7 +70,21 @@ scale_color_date <- scale_colour_date
6270
#' @export
6371
#' @rdname scale_viridis
6472
#' @usage NULL
65-
scale_fill_ordinal <- scale_fill_viridis_d
73+
scale_fill_ordinal <- function(..., type = getOption("ggplot2.ordinal.fill", getOption("ggplot2.ordinal.colour"))) {
74+
type <- type %||% scale_fill_viridis_d
75+
if (is.function(type)) {
76+
type(...)
77+
} else {
78+
discrete_scale("fill", "ordinal", ordinal_pal(type), ...)
79+
}
80+
}
81+
82+
ordinal_pal <- function(colours, na.color = "grey50", alpha = TRUE) {
83+
pal <- scales::colour_ramp(colours, na.color = na.color, alpha = alpha)
84+
function(n) {
85+
pal(seq(0, 1, length.out = n))
86+
}
87+
}
6688

6789
#' @export
6890
#' @rdname scale_gradient

0 commit comments

Comments
 (0)