Skip to content

Revert default resolution(), add argument #5765

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
Apr 2, 2024
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
Expand Up @@ -17,6 +17,9 @@
* Patterns and gradients are now also enabled in `geom_sf()`
(@teunbrand, #5716).
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
* The default behaviour of `resolution()` has been reverted to pre-3.5.0
behaviour. Whether mapped discrete vectors should be treated as having
resolution of 1 is controlled by the new `discrete` argument.
* Fixed bug in `guide_bins()` and `guide_coloursteps()` where discrete breaks,
such as the levels produced by `cut()`, were ordered incorrectly
(@teunbrand, #5757).
Expand Down
2 changes: 1 addition & 1 deletion R/geom-boxplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
data$flipped_aes <- params$flipped_aes
data <- flip_data(data, params$flipped_aes)
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)

if (isFALSE(params$outliers)) {
data$outliers <- NULL
Expand Down
2 changes: 1 addition & 1 deletion R/geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,

setup_data = function(data, params) {
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)

# Set up the stacking function and range
if (is.null(params$stackdir) || params$stackdir == "up") {
Expand Down
2 changes: 1 addition & 1 deletion R/geom-errorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
data$flipped_aes <- params$flipped_aes
data <- flip_data(data, params$flipped_aes)
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)
data <- transform(data,
xmin = x - width / 2, xmax = x + width / 2, width = NULL
)
Expand Down
2 changes: 1 addition & 1 deletion R/geom-errorbarh.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,

setup_data = function(data, params) {
data$height <- data$height %||%
params$height %||% (resolution(data$y, FALSE) * 0.9)
params$height %||% (resolution(data$y, FALSE, TRUE) * 0.9)

transform(data,
ymin = y - height / 2, ymax = y + height / 2, height = NULL
Expand Down
4 changes: 2 additions & 2 deletions R/geom-hex.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ GeomHex <- ggproto("GeomHex", Geom,
if (!is.null(data$width)) {
dx <- data$width[1] / 2
} else {
dx <- resolution(data$x, FALSE)
dx <- resolution(data$x, FALSE, TRUE)
}
# Adjust for difference in width and height of regular hexagon. 1.15 adjusts
# for the effect of the overlapping range in y-direction on the resolution
# calculation
if (!is.null(data$height)) {
dy <- data$height[1] / sqrt(3) / 2
} else {
dy <- resolution(data$y, FALSE) / sqrt(3) / 2 * 1.15
dy <- resolution(data$y, FALSE, TRUE) / sqrt(3) / 2 * 1.15
}

hexC <- hexbin::hexcoords(dx, dy, n = 1)
Expand Down
4 changes: 2 additions & 2 deletions R/geom-raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ GeomRaster <- ggproto("GeomRaster", Geom,
}

# Convert vector of data to raster
x_pos <- as.integer((data$x - min(data$x)) / resolution(unclass(data$x), FALSE))
y_pos <- as.integer((data$y - min(data$y)) / resolution(unclass(data$y), FALSE))
x_pos <- as.integer((data$x - min(data$x)) / resolution(data$x, FALSE))
y_pos <- as.integer((data$y - min(data$y)) / resolution(data$y, FALSE))

data <- coord$transform(data, panel_params)

Expand Down
4 changes: 2 additions & 2 deletions R/geom-tile.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ GeomTile <- ggproto("GeomTile", GeomRect,
extra_params = c("na.rm"),

setup_data = function(data, params) {
data$width <- data$width %||% params$width %||% resolution(data$x, FALSE)
data$height <- data$height %||% params$height %||% resolution(data$y, FALSE)
data$width <- data$width %||% params$width %||% resolution(data$x, FALSE, TRUE)
data$height <- data$height %||% params$height %||% resolution(data$y, FALSE, TRUE)

transform(data,
xmin = x - width / 2, xmax = x + width / 2, width = NULL,
Expand Down
2 changes: 1 addition & 1 deletion R/geom-violin.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ GeomViolin <- ggproto("GeomViolin", Geom,
data$flipped_aes <- params$flipped_aes
data <- flip_data(data, params$flipped_aes)
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)
# ymin, ymax, xmin, and xmax define the bounding rectangle for each group
data <- dapply(data, "group", transform,
xmin = x - width / 2,
Expand Down
4 changes: 2 additions & 2 deletions R/position-jitter.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ PositionJitter <- ggproto("PositionJitter", Position,
seed <- self$seed
}
list(
width = self$width %||% (resolution(data$x, zero = FALSE) * 0.4),
height = self$height %||% (resolution(data$y, zero = FALSE) * 0.4),
width = self$width %||% (resolution(data$x, zero = FALSE, TRUE) * 0.4),
height = self$height %||% (resolution(data$y, zero = FALSE, TRUE) * 0.4),
seed = seed
)
},
Expand Down
2 changes: 1 addition & 1 deletion R/position-jitterdodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
setup_params = function(self, data) {
flipped_aes <- has_flipped_aes(data)
data <- flip_data(data, flipped_aes)
width <- self$jitter.width %||% (resolution(data$x, zero = FALSE) * 0.4)
width <- self$jitter.width %||% (resolution(data$x, zero = FALSE, TRUE) * 0.4)
# Adjust the x transformation based on the number of 'dodge' variables
possible_dodge <- c("fill", "colour", "linetype", "shape", "size", "alpha")
dodgecols <- intersect(possible_dodge, colnames(data))
Expand Down
2 changes: 1 addition & 1 deletion R/stat-boxplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat,
cli::cli_abort("{.fn {snake_class(self)}} requires an {.field x} or {.field y} aesthetic.")
}

params$width <- params$width %||% (resolution(data$x %||% 0) * 0.75)
params$width <- params$width %||% (resolution(data$x %||% 0, discrete = TRUE) * 0.75)

if (!is_mapped_discrete(data$x) && is.double(data$x) && !has_groups(data) && any(data$x != data$x[1L])) {
cli::cli_warn(c(
Expand Down
2 changes: 1 addition & 1 deletion R/stat-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ StatCount <- ggproto("StatCount", Stat,

if (is.null(params$width)) {
x <- if (params$flipped_aes) "y" else "x"
params$width <- resolution(data[[x]]) * 0.9
params$width <- resolution(data[[x]], discrete = TRUE) * 0.9
}

params
Expand Down
10 changes: 7 additions & 3 deletions R/utilities-resolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#' @param x numeric vector
#' @param zero should a zero value be automatically included in the
#' computation of resolution
#' @param discrete should vectors mapped with a discrete scale be treated as
#' having a resolution of 1?
#' @export
#' @examples
#' resolution(1:10)
Expand All @@ -17,9 +19,11 @@
#' # Note the difference between numeric and integer vectors
#' resolution(c(2, 10, 20, 50))
#' resolution(c(2L, 10L, 20L, 50L))
resolution <- function(x, zero = TRUE) {
if (is.integer(x) || is_mapped_discrete(x) ||
zero_range(range(x, na.rm = TRUE))) {
resolution <- function(x, zero = TRUE, discrete = FALSE) {
if (is.integer(x) || zero_range(range(x, na.rm = TRUE))) {
return(1)
}
if (discrete && is_mapped_discrete(x)) {
return(1)
}

Expand Down
5 changes: 4 additions & 1 deletion man/resolution.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ test_that("vec_rbind0 can combined ordered factors", {
test_that("resolution() gives correct answers", {
expect_equal(resolution(c(4, 6)), 2)
expect_equal(resolution(c(4L, 6L)), 1L)
expect_equal(resolution(mapped_discrete(c(4, 6))), 1L)
expect_equal(resolution(mapped_discrete(c(4, 6)), discrete = TRUE), 1L)
expect_equal(resolution(mapped_discrete(c(4, 6))), 2)
expect_equal(resolution(c(0, 0)), 1L)
expect_equal(resolution(c(0.5, 1.5), zero = TRUE), 0.5)

Expand Down