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 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
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)

* `geom_sf()` now removes rows that can't be plotted due to `NA` aesthetics
(#3546, @thomasp85)

Expand Down
37 changes: 28 additions & 9 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,6 +531,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
rescaler = rescale,
oob = censor,
minor_breaks = waiver(),
n.breaks = NULL,

is_discrete = function() FALSE,

Expand All @@ -535,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 @@ -578,7 +586,14 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
if (zero_range(as.numeric(limits))) {
breaks <- limits[1]
} else if (is.waive(self$breaks)) {
breaks <- self$trans$breaks(limits)
if (!is.null(self$n.breaks) && trans_support_nbreaks(self$trans)) {
breaks <- self$trans$breaks(limits, self$n.breaks)
} else {
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)
} else {
Expand Down Expand Up @@ -952,7 +967,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
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 @@ -1090,3 +1105,7 @@ check_transformation <- function(x, transformed, name, axis) {
warning("Transformation introduced infinite values in ", type, " ", axis, "-axis", call. = FALSE)
}
}

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

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

21 changes: 13 additions & 8 deletions man/scale_continuous.Rd

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

4 changes: 4 additions & 0 deletions man/scale_gradient.Rd

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

10 changes: 8 additions & 2 deletions man/scale_size.Rd

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