Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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(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"
)
)
}

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

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 <- 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(mapping)) {
warning(paste0("Using both `xintercept` and `mapping` may not have the",
" desired result as mapping is overwritten if",
" `xintercept` is specified\n"
)
)
}
data <- data.frame(xintercept = xintercept)
mapping <- aes(xintercept = xintercept)
show.legend <- FALSE
Expand Down
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 both"
)

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

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