Skip to content

Commit 125b1e0

Browse files
authored
Revert default resolution(), add argument (#5765)
* add `discrete` as argument to `resolution()` * explicitly declare `discrete` * add test * document * add news bullet
1 parent f1e84cb commit 125b1e0

16 files changed

+32
-21
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
* Patterns and gradients are now also enabled in `geom_sf()`
1818
(@teunbrand, #5716).
1919
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
20+
* The default behaviour of `resolution()` has been reverted to pre-3.5.0
21+
behaviour. Whether mapped discrete vectors should be treated as having
22+
resolution of 1 is controlled by the new `discrete` argument.
2023
* Fixed bug in `guide_bins()` and `guide_coloursteps()` where discrete breaks,
2124
such as the levels produced by `cut()`, were ordered incorrectly
2225
(@teunbrand, #5757).

R/geom-boxplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
189189
data$flipped_aes <- params$flipped_aes
190190
data <- flip_data(data, params$flipped_aes)
191191
data$width <- data$width %||%
192-
params$width %||% (resolution(data$x, FALSE) * 0.9)
192+
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)
193193

194194
if (isFALSE(params$outliers)) {
195195
data$outliers <- NULL

R/geom-dotplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
193193

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

198198
# Set up the stacking function and range
199199
if (is.null(params$stackdir) || params$stackdir == "up") {

R/geom-errorbar.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
4545
data$flipped_aes <- params$flipped_aes
4646
data <- flip_data(data, params$flipped_aes)
4747
data$width <- data$width %||%
48-
params$width %||% (resolution(data$x, FALSE) * 0.9)
48+
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)
4949
data <- transform(data,
5050
xmin = x - width / 2, xmax = x + width / 2, width = NULL
5151
)

R/geom-errorbarh.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
6060

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

6565
transform(data,
6666
ymin = y - height / 2, ymax = y + height / 2, height = NULL

R/geom-hex.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ GeomHex <- ggproto("GeomHex", Geom,
6666
if (!is.null(data$width)) {
6767
dx <- data$width[1] / 2
6868
} else {
69-
dx <- resolution(data$x, FALSE)
69+
dx <- resolution(data$x, FALSE, TRUE)
7070
}
7171
# Adjust for difference in width and height of regular hexagon. 1.15 adjusts
7272
# for the effect of the overlapping range in y-direction on the resolution
7373
# calculation
7474
if (!is.null(data$height)) {
7575
dy <- data$height[1] / sqrt(3) / 2
7676
} else {
77-
dy <- resolution(data$y, FALSE) / sqrt(3) / 2 * 1.15
77+
dy <- resolution(data$y, FALSE, TRUE) / sqrt(3) / 2 * 1.15
7878
}
7979

8080
hexC <- hexbin::hexcoords(dx, dy, n = 1)

R/geom-raster.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ GeomRaster <- ggproto("GeomRaster", Geom,
9494
}
9595

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

100100
data <- coord$transform(data, panel_params)
101101

R/geom-tile.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ GeomTile <- ggproto("GeomTile", GeomRect,
109109
extra_params = c("na.rm"),
110110

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

115115
transform(data,
116116
xmin = x - width / 2, xmax = x + width / 2, width = NULL,

R/geom-violin.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ GeomViolin <- ggproto("GeomViolin", Geom,
135135
data$flipped_aes <- params$flipped_aes
136136
data <- flip_data(data, params$flipped_aes)
137137
data$width <- data$width %||%
138-
params$width %||% (resolution(data$x, FALSE) * 0.9)
138+
params$width %||% (resolution(data$x, FALSE, TRUE) * 0.9)
139139
# ymin, ymax, xmin, and xmax define the bounding rectangle for each group
140140
data <- dapply(data, "group", transform,
141141
xmin = x - width / 2,

R/position-jitter.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ PositionJitter <- ggproto("PositionJitter", Position,
6868
seed <- self$seed
6969
}
7070
list(
71-
width = self$width %||% (resolution(data$x, zero = FALSE) * 0.4),
72-
height = self$height %||% (resolution(data$y, zero = FALSE) * 0.4),
71+
width = self$width %||% (resolution(data$x, zero = FALSE, TRUE) * 0.4),
72+
height = self$height %||% (resolution(data$y, zero = FALSE, TRUE) * 0.4),
7373
seed = seed
7474
)
7575
},

R/position-jitterdodge.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
4646
setup_params = function(self, data) {
4747
flipped_aes <- has_flipped_aes(data)
4848
data <- flip_data(data, flipped_aes)
49-
width <- self$jitter.width %||% (resolution(data$x, zero = FALSE) * 0.4)
49+
width <- self$jitter.width %||% (resolution(data$x, zero = FALSE, TRUE) * 0.4)
5050
# Adjust the x transformation based on the number of 'dodge' variables
5151
possible_dodge <- c("fill", "colour", "linetype", "shape", "size", "alpha")
5252
dodgecols <- intersect(possible_dodge, colnames(data))

R/stat-boxplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat,
7676
cli::cli_abort("{.fn {snake_class(self)}} requires an {.field x} or {.field y} aesthetic.")
7777
}
7878

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

8181
if (!is_mapped_discrete(data$x) && is.double(data$x) && !has_groups(data) && any(data$x != data$x[1L])) {
8282
cli::cli_warn(c(

R/stat-count.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ StatCount <- ggproto("StatCount", Stat,
6262

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

6868
params

R/utilities-resolution.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#' @param x numeric vector
99
#' @param zero should a zero value be automatically included in the
1010
#' computation of resolution
11+
#' @param discrete should vectors mapped with a discrete scale be treated as
12+
#' having a resolution of 1?
1113
#' @export
1214
#' @examples
1315
#' resolution(1:10)
@@ -17,9 +19,11 @@
1719
#' # Note the difference between numeric and integer vectors
1820
#' resolution(c(2, 10, 20, 50))
1921
#' resolution(c(2L, 10L, 20L, 50L))
20-
resolution <- function(x, zero = TRUE) {
21-
if (is.integer(x) || is_mapped_discrete(x) ||
22-
zero_range(range(x, na.rm = TRUE))) {
22+
resolution <- function(x, zero = TRUE, discrete = FALSE) {
23+
if (is.integer(x) || zero_range(range(x, na.rm = TRUE))) {
24+
return(1)
25+
}
26+
if (discrete && is_mapped_discrete(x)) {
2327
return(1)
2428
}
2529

man/resolution.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-utilities.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ test_that("vec_rbind0 can combined ordered factors", {
170170
test_that("resolution() gives correct answers", {
171171
expect_equal(resolution(c(4, 6)), 2)
172172
expect_equal(resolution(c(4L, 6L)), 1L)
173-
expect_equal(resolution(mapped_discrete(c(4, 6))), 1L)
173+
expect_equal(resolution(mapped_discrete(c(4, 6)), discrete = TRUE), 1L)
174+
expect_equal(resolution(mapped_discrete(c(4, 6))), 2)
174175
expect_equal(resolution(c(0, 0)), 1L)
175176
expect_equal(resolution(c(0.5, 1.5), zero = TRUE), 0.5)
176177

0 commit comments

Comments
 (0)