Skip to content

Commit 7f9f23e

Browse files
committed
improve strategy for warning on spurious position values
1 parent 766868c commit 7f9f23e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

R/guides-axis.r

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,15 @@ axis_label_element_overrides <- function(axis_position, angle = NULL) {
415415
}
416416

417417
warn_for_guide_position <- function(guide) {
418-
if (empty(guide$key) || nrow(guide$key) == 1) {
418+
# This is trying to catch when a user specifies a position perpendicular
419+
# to the direction of the axis (e.g., a "y" axis on "top").
420+
# The strategy is to check that two or more unique breaks are mapped
421+
# to the same value along the axis.
422+
breaks_are_unique <- !duplicated(guide$key$.value)
423+
if (empty(guide$key) || sum(breaks_are_unique) == 1) {
419424
return()
420425
}
421426

422-
# this is trying to catch when a user specifies a position perpendicular
423-
# to the direction of the axis (e.g., a "y" axis on "top")
424-
425427
if (guide$position %in% c("top", "bottom")) {
426428
position_aes <- "x"
427429
} else if(guide$position %in% c("left", "right")) {
@@ -430,7 +432,7 @@ warn_for_guide_position <- function(guide) {
430432
return()
431433
}
432434

433-
if (length(unique(guide$key[[position_aes]])) == 1) {
435+
if (length(unique(guide$key[[position_aes]][breaks_are_unique])) == 1) {
434436
warn("Position guide is perpendicular to the intended axis. Did you mean to specify a different guide `position`?")
435437
}
436438
}

tests/testthat/test-guides.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ test_that("a warning is generated when guides are drawn at a location that doesn
7373
expect_warning(ggplot_gtable(built), "Position guide is perpendicular")
7474
})
7575

76+
test_that("a warning is not generated when a guide is specified with duplicate breaks", {
77+
plot <- ggplot(mpg, aes(class, hwy)) +
78+
geom_point() +
79+
scale_y_continuous(breaks = c(20, 20))
80+
built <- expect_silent(ggplot_build(plot))
81+
expect_silent(ggplot_gtable(built))
82+
})
83+
7684
test_that("a warning is generated when more than one position guide is drawn at a location", {
7785
plot <- ggplot(mpg, aes(class, hwy)) +
7886
geom_point() +

0 commit comments

Comments
 (0)