Skip to content

Commit d7f2241

Browse files
authored
mapped_discrete has resolution of 1 (#5247)
* mapped_discrete has resolution 1 * Add resolution test * Add NEWS bullet
1 parent 375a24b commit d7f2241

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* To improve `width` calculation in bar plots with empty factor levels,
4+
`resolution()` considers `mapped_discrete` values as having resolution 1
5+
(@teunbrand, #5211)
36
* When `geom_path()` has aesthetics varying within groups, the `arrow()` is
47
applied to groups instead of individual segments (@teunbrand, #4935).
58
* The default width of `geom_bar()` is now based on panel-wise resolution of

R/utilities-resolution.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#' resolution(c(2, 10, 20, 50))
1919
#' resolution(c(2L, 10L, 20L, 50L))
2020
resolution <- function(x, zero = TRUE) {
21-
if (is.integer(x) || zero_range(range(x, na.rm = TRUE)))
21+
if (is.integer(x) || is_mapped_discrete(x) ||
22+
zero_range(range(x, na.rm = TRUE))) {
2223
return(1)
24+
}
2325

2426
x <- unique0(as.numeric(x))
2527
if (zero) {

tests/testthat/test-utilities.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,11 @@ test_that("vec_rbind0 can combined ordered factors", {
166166
expect_equal(levels(test$a), c("A", "B", "C"))
167167

168168
})
169+
170+
test_that("resolution() gives correct answers", {
171+
expect_equal(resolution(c(4, 6)), 2)
172+
expect_equal(resolution(c(4L, 6L)), 1L)
173+
expect_equal(resolution(mapped_discrete(c(4, 6))), 1L)
174+
expect_equal(resolution(c(0, 0)), 1L)
175+
expect_equal(resolution(c(0.5, 1.5), zero = TRUE), 0.5)
176+
})

0 commit comments

Comments
 (0)