Skip to content

Commit

Permalink
Fix limits on scale_colour_gradient2() (#2300)
Browse files Browse the repository at this point in the history
* Fix limits on `scale_colour_gradient2()` so limits are imposed before rescaling.

* Add test for limit fix for `scale_*_gradient2()`.
  • Loading branch information
foo-bar-baz-qux authored and hadley committed Nov 15, 2017
1 parent d00d890 commit 582acfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -69,6 +69,8 @@
warnings when mapped to size or alpha (unordered factors do). Viridis used as
default colour and fill scale for ordered factors (@karawoo, #1526).

* Fix bug in `scale_*_gradient2()` where points outside limits can sometimes reappear due to rescaling. Now, any rescaling is performed after the limits are enforced (@foo-bar-baz-qux, #2230).

### Margins

* Strips gain margins on all sides by default. This means that to fully justify
Expand Down
4 changes: 2 additions & 2 deletions R/scale-.r
Expand Up @@ -215,7 +215,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
},

map = function(self, x, limits = self$get_limits()) {
x <- self$oob(self$rescaler(x, from = limits))
x <- self$rescaler(self$oob(x, range = limits), from = limits)

uniq <- unique(x)
pal <- self$palette(uniq)
Expand Down Expand Up @@ -598,7 +598,7 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
#'
#' @export
#' @inheritParams continuous_scale
#' @param breaks One of:
#' @param breaks One of:
#' - `NULL` for no breaks
#' - `waiver()` for the default breaks computed by the
#' transformation object
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-scale-gradient.R
@@ -0,0 +1,13 @@
context("scale_gradient")

# Limits ------------------------------------------------------------------

test_that("points outside the limits are plotted as NA", {
df <- data.frame(x = c(0, 1, 2))
p <- ggplot(df, aes(x, 1, fill = x)) +
geom_col() +
scale_fill_gradient2(limits = c(-1, 1), midpoint = 2, na.value = "orange")

correct_fill <- c("#B26D65", "#DCB4AF", "orange")
expect_equivalent(layer_data(p)$fill, correct_fill)
})

0 comments on commit 582acfe

Please sign in to comment.