Skip to content
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

Warn when a supplied mapping is going to be overwritten by geom_hline / vline / abline #2950

Merged
merged 15 commits into from
Jan 23, 2019
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# ggplot2 3.1.0.9000

* `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).

* `scale_shape_identity()` now works correctly with `guide = "legend"` (@malcolmbarrett, #3029)

Expand Down
10 changes: 10 additions & 0 deletions R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ geom_abline <- function(mapping = NULL, data = NULL,

# Act like an annotation
if (!missing(slope) || !missing(intercept)) {
clauswilke marked this conversation as resolved.
Show resolved Hide resolved

# Warn if supplied mapping is going to be overwritten
if (!missing(mapping)) {
warning(paste0("Using `intercept` and/or `slope` with `mapping` may",
" not have the desired result as mapping is overwritten",
" if either of these is specified\n"
)
)
}

if (missing(slope)) slope <- 1
if (missing(intercept)) intercept <- 0
n_slopes <- max(length(slope), length(intercept))
Expand Down
8 changes: 8 additions & 0 deletions R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ geom_hline <- function(mapping = NULL, data = NULL,

# Act like an annotation
if (!missing(yintercept)) {
# Warn if supplied mapping is going to be overwritten
if (!missing(mapping)) {
warning(paste0("Using both `yintercept` and `mapping` may not have the",
" desired result as mapping is overwritten if",
" `yintercept` is specified\n"
)
)
}
data <- new_data_frame(list(yintercept = yintercept))
mapping <- aes(yintercept = yintercept)
show.legend <- FALSE
Expand Down
8 changes: 8 additions & 0 deletions R/geom-vline.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ geom_vline <- function(mapping = NULL, data = NULL,

# Act like an annotation
if (!missing(xintercept)) {
# Warn if supplied mapping is going to be overwritten
if (!missing(mapping)) {
warning(paste0("Using both `xintercept` and `mapping` may not have the",
" desired result as mapping is overwritten if",
" `xintercept` is specified\n"
)
)
}
data <- new_data_frame(list(xintercept = xintercept))
mapping <- aes(xintercept = xintercept)
show.legend <- FALSE
Expand Down
2 changes: 1 addition & 1 deletion man/borders.Rd

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

2 changes: 1 addition & 1 deletion man/map_data.Rd

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

30 changes: 30 additions & 0 deletions tests/testthat/test-geom-hline-vline-abline.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,33 @@ test_that("curved lines in map projections", {
nzmap + coord_map(projection = 'azequalarea', orientation = c(-36.92, 174.6, 0))
)
})

# Warning tests ------------------------------------------------------------

test_that("Warning if a supplied mapping is going to be overwritten", {

expect_warning(
geom_vline(xintercept = 3, aes(colour = colour)),
"Using both"
)

expect_warning(
geom_hline(yintercept = 3, aes(colour = colour)),
"Using both"
)

expect_warning(
geom_abline(intercept = 3, aes(colour = colour)),
"Using "
)

expect_warning(
geom_abline(intercept = 3, slope = 0.5, aes(colour = colour)),
"Using "
)

expect_warning(
geom_abline(slope=0.5, aes(colour = colour)),
"Using "
)
})