7474# ' options(ggplot2.continuous.fill = tmp) # restore previous setting
7575# ' @export
7676scale_colour_continuous <- function (... ,
77- type = getOption(" ggplot2.continuous.colour" , default = " gradient" )) {
77+ type = getOption(" ggplot2.continuous.colour" )) {
78+ type <- type %|| % " gradient"
79+
7880 if (is.function(type )) {
79- type(... )
81+ check_scale_type( type(... ), " scale_colour_continuous " , " colour " )
8082 } else if (identical(type , " gradient" )) {
8183 scale_colour_gradient(... )
8284 } else if (identical(type , " viridis" )) {
@@ -89,9 +91,11 @@ scale_colour_continuous <- function(...,
8991# ' @rdname scale_colour_continuous
9092# ' @export
9193scale_fill_continuous <- function (... ,
92- type = getOption(" ggplot2.continuous.fill" , default = " gradient" )) {
94+ type = getOption(" ggplot2.continuous.fill" )) {
95+ type <- type %|| % " gradient"
96+
9397 if (is.function(type )) {
94- type(... )
98+ check_scale_type( type(... ), " scale_fill_continuous " , " fill " )
9599 } else if (identical(type , " gradient" )) {
96100 scale_fill_gradient(... )
97101 } else if (identical(type , " viridis" )) {
@@ -104,29 +108,68 @@ scale_fill_continuous <- function(...,
104108# ' @export
105109# ' @rdname scale_colour_continuous
106110scale_colour_binned <- function (... ,
107- type = getOption(" ggplot2.binned.colour" , default = getOption( " ggplot2.continuous.colour " , default = " gradient " ) )) {
111+ type = getOption(" ggplot2.binned.colour" )) {
108112 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" )
114114 } 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+ }
116131 }
117132}
118133
119134# ' @export
120135# ' @rdname scale_colour_continuous
121136scale_fill_binned <- function (... ,
122- type = getOption(" ggplot2.binned.fill" , default = getOption( " ggplot2.continuous.fill " , default = " gradient " ) )) {
137+ type = getOption(" ggplot2.binned.fill" )) {
123138 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" )
129140 } 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 = ', ')}" ))
131169 }
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
132175}
0 commit comments