25
25
# ' each major break)
26
26
# ' - A numeric vector of positions
27
27
# ' - A function that given the limits returns a vector of minor breaks.
28
+ # ' @param n.breaks An integer guiding the number of major breaks. The algorithm
29
+ # ' may choose a slightly different number to ensure nice break labels. Will
30
+ # ' only have an effect if `breaks = waiver()`. Use `NULL` to use the default
31
+ # ' number of breaks given by the transformation.
28
32
# ' @param labels One of:
29
33
# ' - `NULL` for no labels
30
34
# ' - `waiver()` for the default labels computed by the
78
82
# ' @param super The super class to use for the constructed scale
79
83
# ' @keywords internal
80
84
continuous_scale <- function (aesthetics , scale_name , palette , name = waiver(),
81
- breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL ,
82
- rescaler = rescale , oob = censor , expand = waiver(), na.value = NA_real_ ,
83
- trans = " identity" , guide = " legend" , position = " left" , super = ScaleContinuous ) {
85
+ breaks = waiver(), minor_breaks = waiver(), n.breaks = NULL ,
86
+ labels = waiver(), limits = NULL , rescaler = rescale ,
87
+ oob = censor , expand = waiver(), na.value = NA_real_ ,
88
+ trans = " identity" , guide = " legend" , position = " left" ,
89
+ super = ScaleContinuous ) {
84
90
85
91
aesthetics <- standardise_aes_names(aesthetics )
86
92
@@ -116,6 +122,7 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
116
122
name = name ,
117
123
breaks = breaks ,
118
124
minor_breaks = minor_breaks ,
125
+ n.breaks = n.breaks ,
119
126
120
127
labels = labels ,
121
128
guide = guide ,
@@ -524,6 +531,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
524
531
rescaler = rescale ,
525
532
oob = censor ,
526
533
minor_breaks = waiver(),
534
+ n.breaks = NULL ,
527
535
528
536
is_discrete = function () FALSE ,
529
537
@@ -535,10 +543,10 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
535
543
},
536
544
537
545
transform = function (self , x ) {
538
- new_x <- self $ trans $ transform(x )
539
- axis <- if (" x" %in% self $ aesthetics ) " x" else " y"
540
- check_transformation(x , new_x , self $ scale_name , axis )
541
- new_x
546
+ new_x <- self $ trans $ transform(x )
547
+ axis <- if (" x" %in% self $ aesthetics ) " x" else " y"
548
+ check_transformation(x , new_x , self $ scale_name , axis )
549
+ new_x
542
550
},
543
551
544
552
map = function (self , x , limits = self $ get_limits()) {
@@ -578,7 +586,14 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
578
586
if (zero_range(as.numeric(limits ))) {
579
587
breaks <- limits [1 ]
580
588
} else if (is.waive(self $ breaks )) {
581
- breaks <- self $ trans $ breaks(limits )
589
+ if (! is.null(self $ n.breaks ) && trans_support_nbreaks(self $ trans )) {
590
+ breaks <- self $ trans $ breaks(limits , self $ n.breaks )
591
+ } else {
592
+ if (! is.null(self $ n.breaks )) {
593
+ warning(" Ignoring n.breaks. Use a trans object that supports setting number of breaks" , call. = FALSE )
594
+ }
595
+ breaks <- self $ trans $ breaks(limits )
596
+ }
582
597
} else if (is.function(self $ breaks )) {
583
598
breaks <- self $ breaks(limits )
584
599
} else {
@@ -952,7 +967,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
952
967
stop(" Invalid breaks specification. Use NULL, not NA" , call. = FALSE )
953
968
} else if (is.waive(self $ breaks )) {
954
969
if (self $ nice.breaks ) {
955
- if (! is.null(self $ n.breaks ) && " n " %in% names(formals( self $ trans $ breaks ) )) {
970
+ if (! is.null(self $ n.breaks ) && trans_support_nbreaks( self $ trans )) {
956
971
breaks <- self $ trans $ breaks(limits , n = self $ n.breaks )
957
972
} else {
958
973
if (! is.null(self $ n.breaks )) {
@@ -1090,3 +1105,7 @@ check_transformation <- function(x, transformed, name, axis) {
1090
1105
warning(" Transformation introduced infinite values in " , type , " " , axis , " -axis" , call. = FALSE )
1091
1106
}
1092
1107
}
1108
+
1109
+ trans_support_nbreaks <- function (trans ) {
1110
+ " n" %in% names(formals(trans $ breaks ))
1111
+ }
0 commit comments