Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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)) {

# 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)`"
)
)
}

if (missing(slope)) slope <- 1
if (missing(intercept)) intercept <- 0

Expand Down
18 changes: 18 additions & 0 deletions R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ 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)) {
# 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)`"
)
)
}

data <- data.frame(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(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)`"
)
)
}
data <- data.frame(xintercept = xintercept)
mapping <- aes(xintercept = xintercept)
show.legend <- FALSE
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"
)
})