Skip to content

Commit 8a5b03c

Browse files
committed
use scale class constructors
1 parent 28aec3a commit 8a5b03c

File tree

1 file changed

+90
-146
lines changed

1 file changed

+90
-146
lines changed

R/scale-.R

Lines changed: 90 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -107,62 +107,15 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
107107
guide = "legend", position = "left",
108108
call = caller_call(),
109109
super = ScaleContinuous) {
110-
call <- call %||% current_call()
111110
if (lifecycle::is_present(scale_name)) {
112111
deprecate_soft0("3.5.0", "continuous_scale(scale_name)")
113112
}
114113
if (lifecycle::is_present(trans)) {
115114
deprecate_soft0("3.5.0", "continuous_scale(trans)", "continuous_scale(transform)")
116115
transform <- trans
117116
}
118-
119-
aesthetics <- standardise_aes_names(aesthetics)
120-
121-
check_breaks_labels(breaks, labels, call = call)
122-
123-
position <- arg_match0(position, c("left", "right", "top", "bottom"))
124-
125-
# If the scale is non-positional, break = NULL means removing the guide
126-
if (is.null(breaks) && all(!is_position_aes(aesthetics))) {
127-
guide <- "none"
128-
}
129-
130-
transform <- as.transform(transform)
131-
if (!is.null(limits) && !is.function(limits)) {
132-
limits <- transform$transform(limits)
133-
}
134-
135-
# Convert formula to function if appropriate
136-
limits <- allow_lambda(limits)
137-
breaks <- allow_lambda(breaks)
138-
labels <- allow_lambda(labels)
139-
rescaler <- allow_lambda(rescaler)
140-
oob <- allow_lambda(oob)
141-
minor_breaks <- allow_lambda(minor_breaks)
142-
143-
ggproto(NULL, super,
144-
call = call,
145-
146-
aesthetics = aesthetics,
147-
palette = palette,
148-
149-
range = ContinuousRange$new(),
150-
limits = limits,
151-
trans = transform,
152-
na.value = na.value,
153-
expand = expand,
154-
rescaler = rescaler,
155-
oob = oob,
156-
157-
name = name,
158-
breaks = breaks,
159-
minor_breaks = minor_breaks,
160-
n.breaks = n.breaks,
161-
162-
labels = labels,
163-
guide = guide,
164-
position = position
165-
)
117+
args <- find_args(call = NULL, scale_name = NULL, trans = NULL)
118+
inject(super$new(!!!args, call = call %||% current_call()))
166119
}
167120

168121
#' Discrete scale constructor
@@ -206,55 +159,11 @@ discrete_scale <- function(aesthetics, scale_name = deprecated(), palette, name
206159
guide = "legend", position = "left",
207160
call = caller_call(),
208161
super = ScaleDiscrete) {
209-
call <- call %||% current_call()
210162
if (lifecycle::is_present(scale_name)) {
211163
deprecate_soft0("3.5.0", "discrete_scale(scale_name)")
212164
}
213-
214-
aesthetics <- standardise_aes_names(aesthetics)
215-
216-
check_breaks_labels(breaks, labels, call = call)
217-
218-
# Convert formula input to function if appropriate
219-
limits <- allow_lambda(limits)
220-
breaks <- allow_lambda(breaks)
221-
labels <- allow_lambda(labels)
222-
minor_breaks <- allow_lambda(minor_breaks)
223-
224-
if (!is.function(limits) && (length(limits) > 0) && !is.discrete(limits)) {
225-
cli::cli_warn(c(
226-
"Continuous limits supplied to discrete scale.",
227-
"i" = "Did you mean {.code limits = factor(...)} or {.fn scale_*_continuous}?"
228-
), call = call)
229-
}
230-
231-
position <- arg_match0(position, c("left", "right", "top", "bottom"))
232-
233-
# If the scale is non-positional, break = NULL means removing the guide
234-
if (is.null(breaks) && all(!is_position_aes(aesthetics))) {
235-
guide <- "none"
236-
}
237-
238-
ggproto(NULL, super,
239-
call = call,
240-
241-
aesthetics = aesthetics,
242-
palette = palette,
243-
244-
range = DiscreteRange$new(),
245-
limits = limits,
246-
na.value = na.value,
247-
na.translate = na.translate,
248-
expand = expand,
249-
250-
name = name,
251-
breaks = breaks,
252-
minor_breaks = minor_breaks,
253-
labels = labels,
254-
drop = drop,
255-
guide = guide,
256-
position = position
257-
)
165+
args <- find_args(call = NULL, scale_name = NULL)
166+
inject(super$new(!!!args, call = call %||% current_call()))
258167
}
259168

260169
#' Binning scale constructor
@@ -301,56 +210,8 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
301210
deprecate_soft0("3.5.0", "binned_scale(trans)", "binned_scale(transform)")
302211
transform <- trans
303212
}
304-
305-
call <- call %||% current_call()
306-
307-
aesthetics <- standardise_aes_names(aesthetics)
308-
309-
check_breaks_labels(breaks, labels, call = call)
310-
311-
position <- arg_match0(position, c("left", "right", "top", "bottom"))
312-
313-
if (is.null(breaks) && !is_position_aes(aesthetics) && guide != "none") {
314-
guide <- "none"
315-
}
316-
317-
transform <- as.transform(transform)
318-
if (!is.null(limits)) {
319-
limits <- transform$transform(limits)
320-
}
321-
322-
# Convert formula input to function if appropriate
323-
limits <- allow_lambda(limits)
324-
breaks <- allow_lambda(breaks)
325-
labels <- allow_lambda(labels)
326-
rescaler <- allow_lambda(rescaler)
327-
oob <- allow_lambda(oob)
328-
329-
ggproto(NULL, super,
330-
call = call,
331-
332-
aesthetics = aesthetics,
333-
palette = palette,
334-
335-
range = ContinuousRange$new(),
336-
limits = limits,
337-
trans = transform,
338-
na.value = na.value,
339-
expand = expand,
340-
rescaler = rescaler,
341-
oob = oob,
342-
n.breaks = n.breaks,
343-
nice.breaks = nice.breaks,
344-
right = right,
345-
show.limits = show.limits,
346-
347-
name = name,
348-
breaks = breaks,
349-
350-
labels = labels,
351-
guide = guide,
352-
position = position
353-
)
213+
args <- find_args(call = NULL, scale_name = NULL, trans = NULL)
214+
inject(super$new(!!!args, call = call %||% current_call()))
354215
}
355216

356217
#' @section Scales:
@@ -600,6 +461,42 @@ Scale <- ggproto("Scale", NULL,
600461

601462
make_sec_title = function(title) {
602463
title
464+
},
465+
466+
new = function(self, aesthetics, palette, name = waiver(), breaks = waiver(),
467+
minor_breaks = waiver(), labels = waiver(), limits = NULL,
468+
expand = waiver(), guide = "legend", position = "left",
469+
call = caller_call(), ..., super = NULL) {
470+
471+
call <- call %||% current_call()
472+
aesthetics <- standardise_aes_names(aesthetics)
473+
check_breaks_labels(breaks, labels, call = call)
474+
limits <- allow_lambda(limits)
475+
breaks <- allow_lambda(breaks)
476+
labels <- allow_lambda(labels)
477+
minor_breaks <- allow_lambda(minor_breaks)
478+
position <- arg_match0(position, .trbl)
479+
if (is.null(breaks) & all(!is_position_aes(aesthetics))) {
480+
guide <- "none"
481+
}
482+
483+
super <- super %||% self
484+
ggproto(
485+
NULL, super,
486+
call = call,
487+
aesthetics = aesthetics,
488+
palette = palette,
489+
limits = limits,
490+
expand = expand,
491+
name = name,
492+
breaks = breaks,
493+
minor_breaks = minor_breaks,
494+
labels = labels,
495+
guide = guide,
496+
position = position,
497+
...
498+
)
499+
603500
}
604501
)
605502

@@ -912,10 +809,33 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
912809
} else {
913810
cat(" Limits: ", show_range(self$dimension()), "\n", sep = "")
914811
}
812+
},
813+
814+
new = function(self, rescaler = rescale, oob = censor,
815+
range = ContinuousRange$new(),
816+
transform = "identity", limits = NULL, ...,
817+
super = NULL) {
818+
819+
transform <- as.transform(transform)
820+
if (!is.null(limits) && !is.function(limits) && !is.formula(limits)) {
821+
limits = transform$transform(limits)
822+
}
823+
824+
rescaler <- allow_lambda(rescaler)
825+
oob <- allow_lambda(oob)
826+
827+
ggproto_parent(Scale, self)$new(
828+
rescaler = rescaler,
829+
range = range,
830+
oob = oob,
831+
trans = transform,
832+
limits = limits,
833+
...,
834+
super = super %||% self
835+
)
915836
}
916837
)
917838

918-
919839
#' @rdname ggplot2-ggproto
920840
#' @format NULL
921841
#' @usage NULL
@@ -1136,6 +1056,26 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
11361056
major_source = major,
11371057
minor_source = NULL
11381058
)
1059+
},
1060+
1061+
new = function(self, limits = NULL, call = caller_call(),
1062+
range = DiscreteRange$new(),
1063+
..., super = NULL) {
1064+
call <- call %||% current_call()
1065+
limits <- allow_lambda(limits)
1066+
if (!is.function(limits) && (length(limits) > 0 && !is.discrete(limits))) {
1067+
cli::cli_warn(c(
1068+
"Continuous limits supplied to discrete scale.",
1069+
i = "Did you mean {.code limits = factor(...)} or {.fn scale_*_continuous}?"
1070+
), call = call)
1071+
}
1072+
ggproto_parent(Scale, self)$new(
1073+
limits = limits,
1074+
range = range,
1075+
call = call,
1076+
...,
1077+
super = super %||% self
1078+
)
11391079
}
11401080
)
11411081

@@ -1370,6 +1310,10 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
13701310
list(range = range, labels = labels,
13711311
major = pal, minor = NULL,
13721312
major_source = major, minor_source = NULL)
1313+
},
1314+
1315+
new = function(self, ..., super = NULL) {
1316+
ggproto_parent(ScaleContinuous, self)$new(..., super = super %||% self)
13731317
}
13741318
)
13751319

0 commit comments

Comments
 (0)