Skip to content

Commit c5a8f35

Browse files
author
Anthony Chiu
committed
Fix tidyverse#2429. Add formal breaks argument to scale_*_manual
1 parent d7a86a5 commit c5a8f35

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

R/scale-manual.r

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
#' scale, or with `breaks` if provided. If this is a named vector, then the
2323
#' values will be matched based on the names instead. Data values that don't
2424
#' match will be given `na.value`.
25+
#' @param breaks One of:
26+
#' - `NULL` for no breaks
27+
#' - `waiver()` for the default breaks (the scale limits)
28+
#' - A character vector of breaks
29+
#' - A function that takes the limits as input and returns breaks
30+
#' as output
2531
#' @section Color Blindness:
2632
#' Many color palettes derived from RGB combinations (like the "rainbow" color
2733
#' palette) are not suitable to support all viewers, especially those with
@@ -74,48 +80,48 @@ NULL
7480

7581
#' @rdname scale_manual
7682
#' @export
77-
scale_colour_manual <- function(..., values, aesthetics = "colour") {
78-
manual_scale(aesthetics, values, ...)
83+
scale_colour_manual <- function(..., values, aesthetics = "colour", breaks = waiver()) {
84+
manual_scale(aesthetics, values, breaks, ...)
7985
}
8086

8187
#' @rdname scale_manual
8288
#' @export
83-
scale_fill_manual <- function(..., values, aesthetics = "fill") {
84-
manual_scale(aesthetics, values, ...)
89+
scale_fill_manual <- function(..., values, aesthetics = "fill", breaks = waiver()) {
90+
manual_scale(aesthetics, values, breaks, ...)
8591
}
8692

8793
#' @rdname scale_manual
8894
#' @export
89-
scale_size_manual <- function(..., values) {
90-
manual_scale("size", values, ...)
95+
scale_size_manual <- function(..., values, breaks = waiver()) {
96+
manual_scale("size", values, breaks, ...)
9197
}
9298

9399
#' @rdname scale_manual
94100
#' @export
95-
scale_shape_manual <- function(..., values) {
96-
manual_scale("shape", values, ...)
101+
scale_shape_manual <- function(..., values, breaks = waiver()) {
102+
manual_scale("shape", values, breaks, ...)
97103
}
98104

99105
#' @rdname scale_manual
100106
#' @export
101-
scale_linetype_manual <- function(..., values) {
102-
manual_scale("linetype", values, ...)
107+
scale_linetype_manual <- function(..., values, breaks = waiver()) {
108+
manual_scale("linetype", values, breaks, ...)
103109
}
104110

105111
#' @rdname scale_manual
106112
#' @export
107-
scale_alpha_manual <- function(..., values) {
108-
manual_scale("alpha", values, ...)
113+
scale_alpha_manual <- function(..., values, breaks = waiver()) {
114+
manual_scale("alpha", values, breaks, ...)
109115
}
110116

111117
#' @rdname scale_manual
112118
#' @export
113-
scale_discrete_manual <- function(aesthetics, ..., values) {
114-
manual_scale(aesthetics, values, ...)
119+
scale_discrete_manual <- function(aesthetics, ..., values, breaks = waiver()) {
120+
manual_scale(aesthetics, values, breaks, ...)
115121
}
116122

117123

118-
manual_scale <- function(aesthetic, values = NULL, ...) {
124+
manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ...) {
119125
# check for missing `values` parameter, in lieu of providing
120126
# a default to all the different scale_*_manual() functions
121127
if (is_missing(values)) {
@@ -125,15 +131,14 @@ manual_scale <- function(aesthetic, values = NULL, ...) {
125131
}
126132

127133
# order values according to breaks
128-
args <- list(...)
129-
if (is.vector(values) && is.null(names(values))
130-
&& "breaks" %in% names(args)) {
131-
if (length(args[["breaks"]]) != length(values)) {
134+
if (is.vector(values) && is.null(names(values)) && !is.waive(breaks) &&
135+
!is.null(breaks)) {
136+
if (length(breaks) != length(values)) {
132137
stop("Differing number of values and breaks in manual scale. ",
133-
length(values), " values provided compared to ",
134-
length(args[["breaks"]]), " breaks.", call. = FALSE)
138+
length(values), " values provided compared to ", length(breaks),
139+
" breaks.", call. = FALSE)
135140
}
136-
names(values) <- args[["breaks"]]
141+
names(values) <- breaks
137142
}
138143

139144
pal <- function(n) {
@@ -143,5 +148,5 @@ manual_scale <- function(aesthetic, values = NULL, ...) {
143148
}
144149
values
145150
}
146-
discrete_scale(aesthetic, "manual", pal, ...)
151+
discrete_scale(aesthetic, "manual", pal, breaks = breaks, ...)
147152
}

man/scale_manual.Rd

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

0 commit comments

Comments
 (0)