Skip to content

Commit 1f259f4

Browse files
RichardJActonclauswilke
authored andcommitted
Warn when a supplied mapping is going to be overwritten by geom_hline / vline / abline (#2950)
* Added warning to geom_hline-vline-abline when a supplied mapping is going to be overwritten. * moved in warnings into act like annotation ifs * removed plot from tests * removed stray 'plot' * removed extraneous if conditions and suggested solution fromwarning * removed 'both' * fixed spacing * updated expected error message * updated expected error message * updated expected error message * roxygen documentation update * added description of changes to NEWS.md * more concise news entry * added missing space in test
1 parent da6ea8d commit 1f259f4

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# ggplot2 3.1.0.9000
2+
3+
* `geom_hline()`, `geom_vline()`, and `geom_abline()` now throw a warning if the user supplies both an `xintercept`, `yintercept`, or `slope` value and a mapping (@RichardJActon, #2950).
24

35
* `scale_color_continuous()` now points at `scale_colour_continuos()` so that it
46
will handle `type = "viridis"` as the documentation states (@hlendway, #3079).

R/geom-abline.r

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ geom_abline <- function(mapping = NULL, data = NULL,
8282

8383
# Act like an annotation
8484
if (!missing(slope) || !missing(intercept)) {
85+
86+
# Warn if supplied mapping is going to be overwritten
87+
if (!missing(mapping)) {
88+
warning(paste0("Using `intercept` and/or `slope` with `mapping` may",
89+
" not have the desired result as mapping is overwritten",
90+
" if either of these is specified\n"
91+
)
92+
)
93+
}
94+
8595
if (missing(slope)) slope <- 1
8696
if (missing(intercept)) intercept <- 0
8797
n_slopes <- max(length(slope), length(intercept))

R/geom-hline.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ geom_hline <- function(mapping = NULL, data = NULL,
1111

1212
# Act like an annotation
1313
if (!missing(yintercept)) {
14+
# Warn if supplied mapping is going to be overwritten
15+
if (!missing(mapping)) {
16+
warning(paste0("Using both `yintercept` and `mapping` may not have the",
17+
" desired result as mapping is overwritten if",
18+
" `yintercept` is specified\n"
19+
)
20+
)
21+
}
1422
data <- new_data_frame(list(yintercept = yintercept))
1523
mapping <- aes(yintercept = yintercept)
1624
show.legend <- FALSE

R/geom-vline.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ geom_vline <- function(mapping = NULL, data = NULL,
1111

1212
# Act like an annotation
1313
if (!missing(xintercept)) {
14+
# Warn if supplied mapping is going to be overwritten
15+
if (!missing(mapping)) {
16+
warning(paste0("Using both `xintercept` and `mapping` may not have the",
17+
" desired result as mapping is overwritten if",
18+
" `xintercept` is specified\n"
19+
)
20+
)
21+
}
1422
data <- new_data_frame(list(xintercept = xintercept))
1523
mapping <- aes(xintercept = xintercept)
1624
show.legend <- FALSE

man/borders.Rd

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

man/map_data.Rd

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

tests/testthat/test-geom-hline-vline-abline.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,33 @@ test_that("curved lines in map projections", {
4343
nzmap + coord_map(projection = 'azequalarea', orientation = c(-36.92, 174.6, 0))
4444
)
4545
})
46+
47+
# Warning tests ------------------------------------------------------------
48+
49+
test_that("Warning if a supplied mapping is going to be overwritten", {
50+
51+
expect_warning(
52+
geom_vline(xintercept = 3, aes(colour = colour)),
53+
"Using both"
54+
)
55+
56+
expect_warning(
57+
geom_hline(yintercept = 3, aes(colour = colour)),
58+
"Using both"
59+
)
60+
61+
expect_warning(
62+
geom_abline(intercept = 3, aes(colour = colour)),
63+
"Using "
64+
)
65+
66+
expect_warning(
67+
geom_abline(intercept = 3, slope = 0.5, aes(colour = colour)),
68+
"Using "
69+
)
70+
71+
expect_warning(
72+
geom_abline(slope=0.5, aes(colour = colour)),
73+
"Using "
74+
)
75+
})

0 commit comments

Comments
 (0)