diff --git a/NEWS.md b/NEWS.md index 30b09b9918..010d5c1e22 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) diff --git a/R/geom-abline.r b/R/geom-abline.r index fc3b1250f1..d309fe3058 100644 --- a/R/geom-abline.r +++ b/R/geom-abline.r @@ -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)) diff --git a/R/geom-hline.r b/R/geom-hline.r index 85bba6e67a..0a689ff7f3 100644 --- a/R/geom-hline.r +++ b/R/geom-hline.r @@ -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 diff --git a/R/geom-vline.r b/R/geom-vline.r index 3bfd2704c1..8c68a79761 100644 --- a/R/geom-vline.r +++ b/R/geom-vline.r @@ -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 diff --git a/man/borders.Rd b/man/borders.Rd index 00918d20a6..07544c8cad 100644 --- a/man/borders.Rd +++ b/man/borders.Rd @@ -67,7 +67,7 @@ if (require("maps")) { ia <- map_data("county", "iowa") mid_range <- function(x) mean(range(x)) seats <- do.call(rbind, lapply(split(ia, ia$subregion), function(d) { - data.frame(lat = mid_range(d$lat), long = mid_range(d$long), subregion = unique(d$subregion)) + data.frame(lat = mid_range(d$lat), long = mid_range(d$long), subregion = unique(d$subregion)) })) ggplot(ia, aes(long, lat)) + diff --git a/man/map_data.Rd b/man/map_data.Rd index a86781457f..ddc28e4bc8 100644 --- a/man/map_data.Rd +++ b/man/map_data.Rd @@ -44,6 +44,6 @@ if (require("maps")) { ggplot(choro, aes(long, lat)) + geom_polygon(aes(group = group, fill = assault / murder)) + coord_map("albers", at0 = 45.5, lat1 = 29.5) - } +} } \keyword{internal} diff --git a/tests/testthat/test-geom-hline-vline-abline.R b/tests/testthat/test-geom-hline-vline-abline.R index 58239e8863..83bb42cced 100644 --- a/tests/testthat/test-geom-hline-vline-abline.R +++ b/tests/testthat/test-geom-hline-vline-abline.R @@ -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 " + ) +})