17
17
# ' name(s) of the aesthetic(s) that this scale works with. This can be useful, for
18
18
# ' example, to apply colour settings to the `colour` and `fill` aesthetics at the
19
19
# ' same time, via `aesthetics = c("colour", "fill")`.
20
- # ' @param values a set of aesthetic values to map data values to. If this
21
- # ' is a named vector, then the values will be matched based on the names.
22
- # ' If unnamed, values will be matched in order (usually alphabetical) with
23
- # ' the limits of the scale. Any data values that don't match will be
24
- # ' given `na.value`.
20
+ # ' @param values a set of aesthetic values to map data values to. The values
21
+ # ' will be matched in order (usually alphabetical) with the limits of the
22
+ # ' scale, or with `breaks` if provided. If this is a named vector, then the
23
+ # ' values will be matched based on the names instead. Data values that don't
24
+ # ' 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
25
31
# ' @section Color Blindness:
26
32
# ' Many color palettes derived from RGB combinations (like the "rainbow" color
27
33
# ' palette) are not suitable to support all viewers, especially those with
74
80
75
81
# ' @rdname scale_manual
76
82
# ' @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 , ... )
79
85
}
80
86
81
87
# ' @rdname scale_manual
82
88
# ' @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 , ... )
85
91
}
86
92
87
93
# ' @rdname scale_manual
88
94
# ' @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 , ... )
91
97
}
92
98
93
99
# ' @rdname scale_manual
94
100
# ' @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 , ... )
97
103
}
98
104
99
105
# ' @rdname scale_manual
100
106
# ' @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 , ... )
103
109
}
104
110
105
111
# ' @rdname scale_manual
106
112
# ' @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 , ... )
109
115
}
110
116
111
117
# ' @rdname scale_manual
112
118
# ' @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 , ... )
115
121
}
116
122
117
123
118
- manual_scale <- function (aesthetic , values = NULL , ... ) {
124
+ manual_scale <- function (aesthetic , values = NULL , breaks = waiver(), ... ) {
119
125
# check for missing `values` parameter, in lieu of providing
120
126
# a default to all the different scale_*_manual() functions
121
127
if (is_missing(values )) {
@@ -124,12 +130,23 @@ manual_scale <- function(aesthetic, values = NULL, ...) {
124
130
force(values )
125
131
}
126
132
133
+ # order values according to breaks
134
+ if (is.vector(values ) && is.null(names(values )) && ! is.waive(breaks ) &&
135
+ ! is.null(breaks )) {
136
+ if (length(breaks ) != length(values )) {
137
+ stop(" Differing number of values and breaks in manual scale. " ,
138
+ length(values ), " values provided compared to " , length(breaks ),
139
+ " breaks." , call. = FALSE )
140
+ }
141
+ names(values ) <- breaks
142
+ }
143
+
127
144
pal <- function (n ) {
128
145
if (n > length(values )) {
129
146
stop(" Insufficient values in manual scale. " , n , " needed but only " ,
130
147
length(values ), " provided." , call. = FALSE )
131
148
}
132
149
values
133
150
}
134
- discrete_scale(aesthetic , " manual" , pal , ... )
151
+ discrete_scale(aesthetic , " manual" , pal , breaks = breaks , ... )
135
152
}
0 commit comments