Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# ggplot2 3.1.0.9000

* `geom_hline()`, `geom_vline()` & `geom_abline()` now throw a warning if the user supplies both an xintercept, yintercept or slope value & a mapping such as this:

```R
geom_hline(yintercept = 3, aes(colour = colour))

```

The above will cause the user supplied mapping to be ignored/overwritten and as users might resaonably expect the above to work given that other geoms do not have this behaviour, they will now be warned about mapping being overwritten. (@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)) {

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