Skip to content

Commit 86fed3f

Browse files
dariyasydykovakarawoo
authored andcommitted
Mid-point color is now the middle of the diverging color scale (#3086)
* Fixed color number in scale_color_distiller() and scale_fill_distiller() * add unit test * add news bullet. closes #3072 * changed name format
1 parent 3c3062d commit 86fed3f

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* Default labels are now generated more consistently; e.g., symbols no longer
2424
get backticks, and long expressions are abbreviated with `...`
2525
(@yutannihilation, #2981).
26+
27+
* Diverging brewer color scale now has the correct mid-point color (@dariyasydykova, #3072).
2628

2729
* Aesthetic mappings now accept functions that return `NULL` (@yutannihilation,
2830
#2997).

R/scale-brewer.r

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#'
99
#' @note
1010
#' The `distiller` scales extend brewer to continuous scales by smoothly
11-
#' interpolating 6 colours from any palette to a continuous scale.
11+
#' interpolating 7 colours from any palette to a continuous scale.
1212
#'
1313
#' @details
1414
#' The `brewer` scales were carefully designed and tested on discrete data.
@@ -84,8 +84,9 @@ scale_colour_distiller <- function(..., type = "seq", palette = 1, direction = -
8484
warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE)
8585
}
8686
continuous_scale(aesthetics, "distiller",
87-
gradient_n_pal(brewer_pal(type, palette, direction)(6), values, space), na.value = na.value, guide = guide, ...)
88-
# NB: 6 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
87+
gradient_n_pal(brewer_pal(type, palette, direction)(7), values, space), na.value = na.value, guide = guide, ...)
88+
# NB: 6-7 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
89+
# For diverging scales, you need an odd number to make sure the mid-point is in the center
8990
}
9091

9192
#' @export
@@ -96,7 +97,7 @@ scale_fill_distiller <- function(..., type = "seq", palette = 1, direction = -1,
9697
warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE)
9798
}
9899
continuous_scale(aesthetics, "distiller",
99-
gradient_n_pal(brewer_pal(type, palette, direction)(6), values, space), na.value = na.value, guide = guide, ...)
100+
gradient_n_pal(brewer_pal(type, palette, direction)(7), values, space), na.value = na.value, guide = guide, ...)
100101
}
101102

102103
# icon.brewer <- function() {

man/scale_brewer.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-scale-brewer.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
context("scale_brewer")
2+
3+
test_that("mid-point in diverging brewer color scale", {
4+
d <- data_frame(x = -1:1)
5+
6+
p <- ggplot(d) +
7+
aes(x = x, y = 1, color = x) +
8+
scale_color_distiller(palette = 'RdBu', direction = 1, limits = c(-1, 1))
9+
10+
expect_equal(layer_data(p)$colour, c("#B2182B", "#F7F7F7", "#2166AC"))
11+
12+
p <- ggplot(d) +
13+
aes(x = x, y = 1, fill = x) +
14+
scale_fill_distiller(palette = 'RdBu', direction = 1, limits = c(-1, 1))
15+
16+
expect_equal(layer_data(p)$fill, c("#B2182B", "#F7F7F7", "#2166AC"))
17+
})

0 commit comments

Comments
 (0)