@@ -640,6 +640,12 @@ check_breaks_labels <- function(breaks, labels, call = NULL) {
640
640
if (is.null(breaks ) || is.null(labels )) {
641
641
return (invisible ())
642
642
}
643
+ if (identical(breaks , NA )) {
644
+ cli :: cli_abort(
645
+ " Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}." ,
646
+ call = call
647
+ )
648
+ }
643
649
644
650
bad_labels <- is.atomic(breaks ) && is.atomic(labels ) &&
645
651
length(breaks ) != length(labels )
@@ -751,48 +757,38 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
751
757
return (numeric ())
752
758
}
753
759
transformation <- self $ get_transformation()
760
+ breaks <- self $ breaks %| W | % transformation $ breaks
761
+
762
+ if (is.null(breaks )) {
763
+ return (NULL )
764
+ }
765
+
754
766
# Ensure limits don't exceed domain (#980)
755
767
domain <- suppressWarnings(transformation $ transform(transformation $ domain ))
756
768
domain <- sort(domain )
757
769
# To avoid NaN causing issues. NaN are dropped by the sort()
758
770
if (length(domain ) == 2 && ! zero_range(domain )) {
759
771
limits <- oob_squish(limits , domain )
760
772
}
761
-
762
- # Limits in transformed space need to be converted back to data space
763
- limits <- transformation $ inverse(limits )
764
-
765
- if (is.null(self $ breaks )) {
766
- return (NULL )
767
- }
768
-
769
- if (identical(self $ breaks , NA )) {
770
- cli :: cli_abort(
771
- " Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}." ,
772
- call = self $ call
773
- )
773
+ if (zero_range(as.numeric(limits ))) {
774
+ return (limits [1 ])
774
775
}
775
776
776
- # Compute `zero_range()` in transformed space in case `limits` in data space
777
- # don't support conversion to numeric (#5304)
778
- if (zero_range(as.numeric(transformation $ transform(limits )))) {
779
- breaks <- limits [1 ]
780
- } else if (is.waiver(self $ breaks )) {
781
- if (! is.null(self $ n.breaks ) && trans_support_nbreaks(transformation )) {
782
- breaks <- transformation $ breaks(limits , self $ n.breaks )
777
+ if (is.function(breaks )) {
778
+ # Limits in transformed space need to be converted back to data space
779
+ limits <- transformation $ inverse(limits )
780
+ if (! is.null(self $ n.breaks ) && support_nbreaks(breaks )) {
781
+ breaks <- breaks(limits , n = self $ n.breaks )
783
782
} else {
783
+ breaks <- breaks(limits )
784
784
if (! is.null(self $ n.breaks )) {
785
785
cli :: cli_warn(
786
- " Ignoring {.arg n.breaks}. Use a {.cls transform} object that supports setting number of breaks." ,
786
+ " Ignoring {.arg n.breaks}. Use a {.cls transform} object or \\
787
+ {.arg breaks} function that supports setting number of breaks" ,
787
788
call = self $ call
788
789
)
789
790
}
790
- breaks <- transformation $ breaks(limits )
791
791
}
792
- } else if (is.function(self $ breaks )) {
793
- breaks <- self $ breaks(limits )
794
- } else {
795
- breaks <- self $ breaks
796
792
}
797
793
798
794
# Breaks in data space need to be converted back to transformed space
@@ -1046,13 +1042,6 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
1046
1042
return (NULL )
1047
1043
}
1048
1044
1049
- if (identical(self $ breaks , NA )) {
1050
- cli :: cli_abort(
1051
- " Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}." ,
1052
- call = self $ call
1053
- )
1054
- }
1055
-
1056
1045
if (is.waiver(self $ breaks )) {
1057
1046
breaks <- limits
1058
1047
} else if (is.function(self $ breaks )) {
@@ -1268,14 +1257,9 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
1268
1257
1269
1258
if (is.null(self $ breaks )) {
1270
1259
return (NULL )
1271
- } else if (identical(self $ breaks , NA )) {
1272
- cli :: cli_abort(
1273
- " Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}." ,
1274
- call = self $ call
1275
- )
1276
1260
} else if (is.waiver(self $ breaks )) {
1277
1261
if (self $ nice.breaks ) {
1278
- if (! is.null(self $ n.breaks ) && trans_support_nbreaks (transformation )) {
1262
+ if (! is.null(self $ n.breaks ) && support_nbreaks (transformation $ breaks )) {
1279
1263
breaks <- transformation $ breaks(limits , n = self $ n.breaks )
1280
1264
} else {
1281
1265
if (! is.null(self $ n.breaks )) {
@@ -1332,9 +1316,16 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
1332
1316
}
1333
1317
}
1334
1318
} else if (is.function(self $ breaks )) {
1335
- if (" n.breaks" %in% names(formals(environment(self $ breaks )$ f ))) {
1319
+ fmls <- names(formals(environment(self $ breaks )$ f ))
1320
+ if (any(c(" n" , " n.breaks" ) %in% fmls )) {
1336
1321
n.breaks <- self $ n.breaks %|| % 5 # same default as trans objects
1337
- breaks <- self $ breaks(limits , n.breaks = n.breaks )
1322
+ # TODO: we should only allow `n` argument and not `n.breaks` to be
1323
+ # consistent with other scales. We should start deprecation at some point.
1324
+ if (" n.breaks" %in% fmls ) {
1325
+ breaks <- self $ breaks(limits , n.breaks = n.breaks )
1326
+ } else {
1327
+ breaks <- self $ breaks(limits , n = n.breaks )
1328
+ }
1338
1329
} else {
1339
1330
if (! is.null(self $ n.breaks )) {
1340
1331
cli :: cli_warn(
@@ -1439,6 +1430,14 @@ check_transformation <- function(x, transformed, name, arg = NULL, call = NULL)
1439
1430
cli :: cli_warn(msg , call = call )
1440
1431
}
1441
1432
1433
+
1434
+ support_nbreaks <- function (fun ) {
1435
+ if (inherits(fun , " ggproto_method" )) {
1436
+ fun <- environment(fun )$ f
1437
+ }
1438
+ " n" %in% fn_fmls_names(fun )
1439
+ }
1440
+
1442
1441
check_continuous_limits <- function (limits , ... ,
1443
1442
arg = caller_arg(limits ),
1444
1443
call = caller_env()) {
@@ -1449,10 +1448,6 @@ check_continuous_limits <- function(limits, ...,
1449
1448
check_length(limits , 2L , arg = arg , call = call )
1450
1449
}
1451
1450
1452
- trans_support_nbreaks <- function (trans ) {
1453
- " n" %in% names(formals(trans $ breaks ))
1454
- }
1455
-
1456
1451
allow_lambda <- function (x ) {
1457
1452
if (is_formula(x )) as_function(x ) else x
1458
1453
}
0 commit comments