74
74
# ' options(ggplot2.continuous.fill = tmp) # restore previous setting
75
75
# ' @export
76
76
scale_colour_continuous <- function (... ,
77
- type = getOption(" ggplot2.continuous.colour" , default = " gradient" )) {
77
+ type = getOption(" ggplot2.continuous.colour" )) {
78
+ type <- type %|| % " gradient"
79
+
78
80
if (is.function(type )) {
79
- type(... )
81
+ check_scale_type( type(... ), " scale_colour_continuous " , " colour " )
80
82
} else if (identical(type , " gradient" )) {
81
83
scale_colour_gradient(... )
82
84
} else if (identical(type , " viridis" )) {
@@ -89,9 +91,11 @@ scale_colour_continuous <- function(...,
89
91
# ' @rdname scale_colour_continuous
90
92
# ' @export
91
93
scale_fill_continuous <- function (... ,
92
- type = getOption(" ggplot2.continuous.fill" , default = " gradient" )) {
94
+ type = getOption(" ggplot2.continuous.fill" )) {
95
+ type <- type %|| % " gradient"
96
+
93
97
if (is.function(type )) {
94
- type(... )
98
+ check_scale_type( type(... ), " scale_fill_continuous " , " fill " )
95
99
} else if (identical(type , " gradient" )) {
96
100
scale_fill_gradient(... )
97
101
} else if (identical(type , " viridis" )) {
@@ -104,29 +108,68 @@ scale_fill_continuous <- function(...,
104
108
# ' @export
105
109
# ' @rdname scale_colour_continuous
106
110
scale_colour_binned <- function (... ,
107
- type = getOption(" ggplot2.binned.colour" , default = getOption( " ggplot2.continuous.colour " , default = " gradient " ) )) {
111
+ type = getOption(" ggplot2.binned.colour" )) {
108
112
if (is.function(type )) {
109
- type(... )
110
- } else if (identical(type , " gradient" )) {
111
- scale_colour_steps(... )
112
- } else if (identical(type , " viridis" )) {
113
- scale_colour_viridis_b(... )
113
+ check_scale_type(type(... ), " scale_colour_binned" , " colour" )
114
114
} else {
115
- abort(" Unknown scale type" )
115
+ type_fallback <- getOption(" ggplot2.continuous.colour" , default = " gradient" )
116
+ # don't use fallback from scale_colour_continuous() if it is
117
+ # a function, since that would change the type of the color
118
+ # scale from binned to continuous
119
+ if (is.function(type_fallback )) {
120
+ type_fallback <- " gradient"
121
+ }
122
+ type <- type %|| % type_fallback
123
+
124
+ if (identical(type , " gradient" )) {
125
+ scale_colour_steps(... )
126
+ } else if (identical(type , " viridis" )) {
127
+ scale_colour_viridis_b(... )
128
+ } else {
129
+ abort(" Unknown scale type" )
130
+ }
116
131
}
117
132
}
118
133
119
134
# ' @export
120
135
# ' @rdname scale_colour_continuous
121
136
scale_fill_binned <- function (... ,
122
- type = getOption(" ggplot2.binned.fill" , default = getOption( " ggplot2.continuous.fill " , default = " gradient " ) )) {
137
+ type = getOption(" ggplot2.binned.fill" )) {
123
138
if (is.function(type )) {
124
- type(... )
125
- } else if (identical(type , " gradient" )) {
126
- scale_fill_steps(... )
127
- } else if (identical(type , " viridis" )) {
128
- scale_fill_viridis_b(... )
139
+ check_scale_type(type(... ), " scale_fill_binned" , " fill" )
129
140
} else {
130
- abort(" Unknown scale type" )
141
+ type_fallback <- getOption(" ggplot2.continuous.fill" , default = " gradient" )
142
+ # don't use fallback from scale_colour_continuous() if it is
143
+ # a function, since that would change the type of the color
144
+ # scale from binned to continuous
145
+ if (is.function(type_fallback )) {
146
+ type_fallback <- " gradient"
147
+ }
148
+ type <- type %|| % type_fallback
149
+
150
+ if (identical(type , " gradient" )) {
151
+ scale_fill_steps(... )
152
+ } else if (identical(type , " viridis" )) {
153
+ scale_fill_viridis_b(... )
154
+ } else {
155
+ abort(" Unknown scale type" )
156
+ }
157
+ }
158
+ }
159
+
160
+
161
+ # helper function to make sure that the provided scale is of the correct
162
+ # type (i.e., is continuous and works with the provided aesthetic)
163
+ check_scale_type <- function (scale , name , aesthetic ) {
164
+ if (! is.ggproto(scale ) || ! inherits(scale , " Scale" )) {
165
+ abort(glue(" The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic. The provided object is not a scale function." ))
166
+ }
167
+ if (! isTRUE(aesthetic %in% scale $ aesthetics )) {
168
+ abort(glue(" The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic. The provided scale works with the following aesthetics: {glue_collapse(scale$aesthetics, sep = ', ')}" ))
131
169
}
170
+ if (isTRUE(scale $ is_discrete())) {
171
+ abort(glue(" The `type` argument of `{name}()` must return a continuous scale for the {aesthetic} aesthetic, but the provided scale is discrete." ))
172
+ }
173
+
174
+ scale
132
175
}
0 commit comments