Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ geom_abline <- function(mapping = NULL, data = NULL,
intercept <- 0
}

# Warn if supplied mapping is going to be overwritten
if ((!missing(slope) || !missing(intercept)) && !missing(mapping)) {
warning(paste0("Using both `intercept` and/or `slope` with `mapping` may not have the desired result as mapping is overwritten if either of these is specified\n",
" Consider placing `intercept` / `slope` inside your `aes()` call.\n",
" e.g. `aes(intercept=2,colour=colour)`"
)
)
}

# Act like an annotation
if (!missing(slope) || !missing(intercept)) {
if (missing(slope)) slope <- 1
Expand Down
9 changes: 9 additions & 0 deletions R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ geom_hline <- function(mapping = NULL, data = NULL,
na.rm = FALSE,
show.legend = NA) {

# Warn if supplied mapping is going to be overwritten
if (!missing(yintercept) && !missing(mapping)) {
warning(paste0("Using both `yintercept` and `mapping` may not have the desired result as mapping is overwritten if `yintercept` is specified\n",
" Consider placing `yintercept` inside your `aes()` call.\n",
" e.g. `aes(yintercept=2,colour=colour)`"
)
)
}

# Act like an annotation
if (!missing(yintercept)) {
data <- data.frame(yintercept = yintercept)
Expand Down
9 changes: 9 additions & 0 deletions R/geom-vline.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ geom_vline <- function(mapping = NULL, data = NULL,
na.rm = FALSE,
show.legend = NA) {

# Warn if supplied mapping is going to be overwritten
if (!missing(xintercept) && !missing(mapping)) {
warning(paste0("Using both `xintercept` and `mapping` may not have the desired result as mapping is overwritten if `xintercept` is specified\n",
" Consider placing `xintercept` inside your `aes()` call.\n",
" e.g. `aes(xintercept=2,colour=colour)`"
)
)
}

# Act like an annotation
if (!missing(xintercept)) {
data <- data.frame(xintercept = xintercept)
Expand Down
35 changes: 35 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,38 @@ 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",{
dat <- data.frame(x = LETTERS[1:5], y = 1:5,colour = c(TRUE,TRUE,rep(FALSE,3)))
plot <- ggplot(dat, aes(x, y)) +
geom_col(width = 1) +
geom_point() +
facet_wrap(~colour)

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

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

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

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

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