Skip to content

Implement n_breaks in continuous_scale #3102

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

Merged
merged 7 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `scale_x_continuous()` and `scale_y_continuous()` gains an `n.breaks` argument
guiding the number of automatic generated breaks (@thomasp85, #3102)

* A new scale type has been added, that allows binning of aesthetics at the
scale level. It has versions for both position and non-position aesthetics and
comes with two new guides (`guide_bins` and `guide_coloursteps`) (@thomasp85, #3096)
Expand Down
229 changes: 50 additions & 179 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#' each major break)
#' - A numeric vector of positions
#' - A function that given the limits returns a vector of minor breaks.
#' @param n.breaks An integer guiding the number of major breaks. The algorithm
#' may choose a slightly different number to ensure nice break labels. Will
#' only have an effect if `breaks = waiver()`. Use `NULL` to use the default
#' number of breaks given by the transformation.
#' @param labels One of:
#' - `NULL` for no labels
#' - `waiver()` for the default labels computed by the
Expand Down Expand Up @@ -78,9 +82,11 @@
#' @param super The super class to use for the constructed scale
#' @keywords internal
continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL,
rescaler = rescale, oob = censor, expand = waiver(), na.value = NA_real_,
trans = "identity", guide = "legend", position = "left", super = ScaleContinuous) {
breaks = waiver(), minor_breaks = waiver(), n.breaks = NULL,
labels = waiver(), limits = NULL, rescaler = rescale,
oob = censor, expand = waiver(), na.value = NA_real_,
trans = "identity", guide = "legend", position = "left",
super = ScaleContinuous) {

aesthetics <- standardise_aes_names(aesthetics)

Expand Down Expand Up @@ -116,6 +122,7 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
name = name,
breaks = breaks,
minor_breaks = minor_breaks,
n.breaks = n.breaks,

labels = labels,
guide = guide,
Expand Down Expand Up @@ -524,7 +531,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
rescaler = rescale,
oob = censor,
minor_breaks = waiver(),
n_breaks = NULL,
n.breaks = NULL,

is_discrete = function() FALSE,

Expand All @@ -536,10 +543,10 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
},

transform = function(self, x) {
new_x <- self$trans$transform(x)
axis <- if ("x" %in% self$aesthetics) "x" else "y"
check_transformation(x, new_x, self$scale_name, axis)
new_x
new_x <- self$trans$transform(x)
axis <- if ("x" %in% self$aesthetics) "x" else "y"
check_transformation(x, new_x, self$scale_name, axis)
new_x
},

map = function(self, x, limits = self$get_limits()) {
Expand Down Expand Up @@ -579,18 +586,13 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
if (zero_range(as.numeric(limits))) {
breaks <- limits[1]
} else if (is.waive(self$breaks)) {
change_breaks <- if (!is.null(self$n_breaks)) {
if (trans_support_nbreaks(self$trans)) {
TRUE
} else {
warning("The transformation does not support changing number of breaks.\nPlease provide a transformation object that does or avoid using the `n_breaks` argument.", call. = FALSE)
FALSE
}
}
breaks <- if (change_breaks) {
self$trans$breaks(limits, self$n_breaks)
if (!is.null(self$n.breaks) && trans_support_nbreaks(self$trans)) {
breaks <- self$trans$breaks(limits, self$n.breaks)
} else {
self$trans$breaks(limits)
if (!is.null(self$n.breaks)) {
warning("Ignoring n.breaks. Use a trans object that supports setting number of breaks", call. = FALSE)
}
breaks <- self$trans$breaks(limits)
}
} else if (is.function(self$breaks)) {
breaks <- self$breaks(limits)
Expand Down Expand Up @@ -888,68 +890,16 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
#' @format NULL
#' @usage NULL
#' @export
#' @param aesthetics The names of the aesthetics that this scale works with
#' @param scale_name The name of the scale
#' @param palette A palette function that when called with a numeric vector with
#' values between 0 and 1 returns the corresponding values in the range the
#' scale maps to.
#' @param name The name of the scale. Used as the axis or legend title. If
#' `waiver()`, the default, the name of the scale is taken from the first
#' mapping used for that aesthetic. If `NULL`, the legend title will be
#' omitted.
#' @param breaks One of:
#' - `NULL` for no breaks
#' - `waiver()` for the default breaks computed by the
#' transformation object
#' - A numeric vector of positions
#' - A function that takes the limits as input and returns breaks
#' as output
#' @param minor_breaks One of:
#' - `NULL` for no minor breaks
#' - `waiver()` for the default breaks (one minor break between
#' each major break)
#' - A numeric vector of positions
#' - A function that given the limits returns a vector of minor breaks.
#' @param n_breaks An integer guiding the number of major breaks. The algorithm
#' may choose a slightly different number to ensure nice break labels. Will
#' only have an effect if `breaks = waiver()`. Use `NULL` to use the default
#' number of breaks given by the transformation.
#' @param labels One of:
#' - `NULL` for no labels
#' - `waiver()` for the default labels computed by the
#' transformation object
#' - A character vector giving labels (must be same length as `breaks`)
#' - A function that takes the breaks as input and returns labels
#' as output
#' @param limits A numeric vector of length two providing limits of the scale.
#' Use `NA` to refer to the existing minimum or maximum.
#' @param rescaler Used by diverging and n colour gradients
#' (i.e. [scale_colour_gradient2()], [scale_colour_gradientn()]).
#' A function used to scale the input values to the range \[0, 1].
#' @param oob Function that handles limits outside of the scale limits
#' (out of bounds). The default replaces out of bounds values with `NA`.
#' @inheritParams scale_x_discrete
#' @param na.value Missing values will be replaced with this value.
#' @param trans Either the name of a transformation object, or the
#' object itself. Built-in transformations include "asn", "atanh",
#' "boxcox", "exp", "identity", "log", "log10", "log1p", "log2",
#' "logit", "probability", "probit", "reciprocal", "reverse" and "sqrt".
#'
#' A transformation object bundles together a transform, its inverse,
#' and methods for generating breaks and labels. Transformation objects
#' are defined in the scales package, and are called `name_trans`, e.g.
#' [scales::boxcox_trans()]. You can create your own
#' transformation with [scales::trans_new()].
#' @param guide A function used to create a guide or its name. See
#' [guides()] for more info.
#' @param position The position of the axis. "left" or "right" for vertical
#' scales, "top" or "bottom" for horizontal scales
#' @param super The super class to use for the constructed scale
#' @keywords internal
continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
breaks = waiver(), minor_breaks = waiver(), n_breaks = NULL, labels = waiver(),
limits = NULL, rescaler = rescale, oob = censor, expand = waiver(), na.value = NA_real_,
trans = "identity", guide = "legend", position = "left", super = ScaleContinuous) {
ScaleBinned <- ggproto("ScaleBinned", Scale,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with the big diff here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must be looking at the code from before I merged master in? scale-.r was reorganised while this PR was developed

range = continuous_range(),
na.value = NA_real_,
rescaler = rescale,
oob = squish,
n.breaks = NULL,
nice.breaks = TRUE,
right = TRUE,
after.stat = FALSE,
show.limits = FALSE,

is_discrete = function() FALSE,

Expand Down Expand Up @@ -982,9 +932,9 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
breaks <- self$rescale(breaks, limits)

x_binned <- cut(x, breaks,
labels = FALSE,
include.lowest = TRUE,
right = self$right
labels = FALSE,
include.lowest = TRUE,
right = self$right
)

if (!is.null(self$palette.cache)) {
Expand Down Expand Up @@ -1017,7 +967,7 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
stop("Invalid breaks specification. Use NULL, not NA", call. = FALSE)
} else if (is.waive(self$breaks)) {
if (self$nice.breaks) {
if (!is.null(self$n.breaks) && "n" %in% names(formals(self$trans$breaks))) {
if (!is.null(self$n.breaks) && trans_support_nbreaks(self$trans)) {
breaks <- self$trans$breaks(limits, n = self$n.breaks)
} else {
if (!is.null(self$n.breaks)) {
Expand Down Expand Up @@ -1121,100 +1071,7 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
major = pal, minor = NULL,
major_source = major, minor_source = NULL)
}

trans <- as.trans(trans)
if (!is.null(limits)) {
limits <- trans$transform(limits)
}

ggproto(NULL, super,
call = match.call(),

aesthetics = aesthetics,
scale_name = scale_name,
palette = palette,

range = continuous_range(),
limits = limits,
trans = trans,
na.value = na.value,
expand = expand,
rescaler = rescaler, # Used by diverging and n colour gradients
oob = oob,

name = name,
breaks = breaks,
minor_breaks = minor_breaks,
n_breaks = n_breaks,

labels = labels,
guide = guide,
position = position
)
}

#' Discrete scale constructor.
#'
#' @export
#' @inheritParams continuous_scale
#' @param palette A palette function that when called with a single integer
#' argument (the number of levels in the scale) returns the values that
#' they should take.
#' @param breaks One of:
#' - `NULL` for no breaks
#' - `waiver()` for the default breaks computed by the
#' transformation object
#' - A character vector of breaks
#' - A function that takes the limits as input and returns breaks
#' as output
#' @param limits A character vector that defines possible values of the scale
#' and their order.
#' @param drop Should unused factor levels be omitted from the scale?
#' The default, `TRUE`, uses the levels that appear in the data;
#' `FALSE` uses all the levels in the factor.
#' @param na.translate Unlike continuous scales, discrete scales can easily show
#' missing values, and do so by default. If you want to remove missing values
#' from a discrete scale, specify `na.translate = FALSE`.
#' @param na.value If `na.translate = TRUE`, what value aesthetic
#' value should missing be displayed as? Does not apply to position scales
#' where `NA` is always placed at the far right.
#' @keywords internal
discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(),
na.translate = TRUE, na.value = NA, drop = TRUE,
guide = "legend", position = "left", super = ScaleDiscrete) {

aesthetics <- standardise_aes_names(aesthetics)

check_breaks_labels(breaks, labels)

position <- match.arg(position, c("left", "right", "top", "bottom"))

if (is.null(breaks) && !is_position_aes(aesthetics) && guide != "none") {
guide <- "none"
}

ggproto(NULL, super,
call = match.call(),

aesthetics = aesthetics,
scale_name = scale_name,
palette = palette,

range = discrete_range(),
limits = limits,
na.value = na.value,
na.translate = na.translate,
expand = expand,

name = name,
breaks = breaks,
labels = labels,
drop = drop,
guide = guide,
position = position
)
}
)

# In place modification of a scale to change the primary axis
scale_flip_position <- function(scale) {
Expand All @@ -1227,6 +1084,20 @@ scale_flip_position <- function(scale) {
)
invisible()
}

check_transformation <- function(x, transformed, name, axis) {
if (any(is.finite(x) != is.finite(transformed))) {
type <- if (name == "position_b") {
"binned"
} else if (name == "position_c") {
"continuous"
} else {
"discrete"
}
warning("Transformation introduced infinite values in ", type, " ", axis, "-axis", call. = FALSE)
}
}

trans_support_nbreaks <- function(trans) {
"n" %in% names(formals(trans$breaks))
}
18 changes: 10 additions & 8 deletions R/scale-continuous.r
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ NULL
#'
#' @export
scale_x_continuous <- function(name = waiver(), breaks = waiver(),
minor_breaks = waiver(), n_breaks = NULL,
minor_breaks = waiver(), n.breaks = NULL,
labels = waiver(), limits = NULL,
expand = waiver(), oob = censor,
na.value = NA_real_, trans = "identity",
position = "bottom", sec.axis = waiver()) {
guide = waiver(), position = "bottom",
sec.axis = waiver()) {
sc <- continuous_scale(
c("x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final", "xlower", "xmiddle", "xupper"),
"position_c", identity, name = name, breaks = breaks, n_breaks = n_breaks,
c("x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final", "xlower", "xmiddle", "xupper", "x0"),
"position_c", identity, name = name, breaks = breaks, n.breaks = n.breaks,
minor_breaks = minor_breaks, labels = labels, limits = limits,
expand = expand, oob = oob, na.value = na.value, trans = trans,
guide = guide, position = position, super = ScaleContinuousPosition
Expand All @@ -94,14 +95,15 @@ scale_x_continuous <- function(name = waiver(), breaks = waiver(),
#' @rdname scale_continuous
#' @export
scale_y_continuous <- function(name = waiver(), breaks = waiver(),
minor_breaks = waiver(), n_breaks = NULL,
minor_breaks = waiver(), n.breaks = NULL,
labels = waiver(), limits = NULL,
expand = waiver(), oob = censor,
na.value = NA_real_, trans = "identity",
position = "left", sec.axis = waiver()) {
guide = waiver(), position = "left",
sec.axis = waiver()) {
sc <- continuous_scale(
c("y", "ymin", "ymax", "yend", "yintercept", "ymin_final", "ymax_final", "lower", "middle", "upper"),
"position_c", identity, name = name, breaks = breaks, n_breaks = n_breaks,
c("y", "ymin", "ymax", "yend", "yintercept", "ymin_final", "ymax_final", "lower", "middle", "upper", "y0"),
"position_c", identity, name = name, breaks = breaks, n.breaks = n.breaks,
minor_breaks = minor_breaks, labels = labels, limits = limits,
expand = expand, oob = oob, na.value = na.value, trans = trans,
guide = guide, position = position, super = ScaleContinuousPosition
Expand Down
4 changes: 2 additions & 2 deletions man/continuous_scale.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading