Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC bin breaks derived from scale breaks #6174

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
minor_breaks = waiver(),
n.breaks = NULL,
trans = transform_identity(),
freeze_breaks = FALSE,
frozen_breaks = NULL,
frozen_minor_breaks = NULL,

is_discrete = function() FALSE,

Expand Down Expand Up @@ -749,10 +752,14 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
)
}

breaks_are_frozen <- !is.null(self$frozen_breaks)

# Compute `zero_range()` in transformed space in case `limits` in data space
# don't support conversion to numeric (#5304)
if (zero_range(as.numeric(transformation$transform(limits)))) {
breaks <- limits[1]
} else if (self$freeze_breaks && breaks_are_frozen) {
breaks <- self$frozen_breaks
} else if (is.waive(self$breaks)) {
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) {
breaks <- transformation$breaks(limits, self$n.breaks)
Expand All @@ -771,6 +778,10 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
breaks <- self$breaks
}

if (self$freeze_breaks && !breaks_are_frozen) {
self$frozen_breaks <- breaks
}

# Breaks in data space need to be converted back to transformed space
transformation$transform(breaks)
},
Expand All @@ -794,8 +805,12 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
# some transforms assume finite major breaks
b <- b[is.finite(b)]

breaks_are_frozen <- !is.null(self$frozen_minor_breaks)

transformation <- self$get_transformation()
if (is.waive(self$minor_breaks)) {
if (self$freeze_breaks && breaks_are_frozen) {
breaks <- self$frozen_minor_breaks
} else if (is.waive(self$minor_breaks)) {
if (is.null(b)) {
breaks <- NULL
} else {
Expand All @@ -819,6 +834,10 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
breaks <- transformation$transform(self$minor_breaks)
}

if (self$freeze_breaks && !breaks_are_frozen) {
self$frozen_minor_breaks <- breaks
}

# Any minor breaks outside the dimensions need to be thrown away
discard(breaks, limits)
},
Expand Down Expand Up @@ -875,6 +894,8 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
clone = function(self) {
new <- ggproto(NULL, self)
new$range <- ContinuousRange$new()
new$frozen_breaks <- NULL
new$frozen_minor_breaks <- NULL
new
},

Expand Down
8 changes: 7 additions & 1 deletion R/stat-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ stat_bin <- function(mapping = NULL, data = NULL,
closed = c("right", "left"),
pad = FALSE,
na.rm = FALSE,
follow.scale = FALSE,
keep.zeroes = "all",
orientation = NA,
show.legend = NA,
Expand All @@ -81,6 +82,7 @@ stat_bin <- function(mapping = NULL, data = NULL,
closed = closed,
pad = pad,
na.rm = na.rm,
follow.scale = follow.scale,
orientation = orientation,
keep.zeroes = keep.zeroes,
...
Expand Down Expand Up @@ -150,11 +152,15 @@ StatBin <- ggproto("StatBin", Stat,
center = NULL, boundary = NULL,
closed = c("right", "left"), pad = FALSE,
breaks = NULL, flipped_aes = FALSE, keep.zeroes = "all",
follow.scale = FALSE,
# The following arguments are not used, but must
# be listed so parameters are computed correctly
origin = NULL, right = NULL, drop = NULL) {
x <- flipped_names(flipped_aes)$x
if (!is.null(breaks)) {
if (follow.scale) {
breaks <- scales[[x]]$get_breaks()
bins <- bin_breaks(breaks, closed)
} else if (!is.null(breaks)) {
if (is.function(breaks)) {
breaks <- breaks(data[[x]])
}
Expand Down
Loading