Skip to content

Mid-point color is now the middle of the diverging color scale #3086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Default labels are now generated more consistently; e.g., symbols no longer
get backticks, and long expressions are abbreviated with `...`
(@yutannihilation, #2981).

* Diverging brewer color scale now has the correct mid-point color (@dariyasydykova, #3072).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move this so it is the first bullet in the section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per discussion with @clauswilke, I leave it where it is.


* Aesthetic mappings now accept functions that return `NULL` (@yutannihilation,
#2997).
Expand Down
9 changes: 5 additions & 4 deletions R/scale-brewer.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#'
#' @note
#' The `distiller` scales extend brewer to continuous scales by smoothly
#' interpolating 6 colours from any palette to a continuous scale.
#' interpolating 7 colours from any palette to a continuous scale.
#'
#' @details
#' The `brewer` scales were carefully designed and tested on discrete data.
Expand Down Expand Up @@ -84,8 +84,9 @@ scale_colour_distiller <- function(..., type = "seq", palette = 1, direction = -
warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE)
}
continuous_scale(aesthetics, "distiller",
gradient_n_pal(brewer_pal(type, palette, direction)(6), values, space), na.value = na.value, guide = guide, ...)
# NB: 6 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
gradient_n_pal(brewer_pal(type, palette, direction)(7), values, space), na.value = na.value, guide = guide, ...)
# NB: 6-7 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
# For diverging scales, you need an odd number to make sure the mid-point is in the center
}

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

# icon.brewer <- function() {
Expand Down
2 changes: 1 addition & 1 deletion man/scale_brewer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/test-scale-brewer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
context("scale_brewer")

test_that("mid-point in diverging brewer color scale", {
d <- data_frame(x = -1:1)

p <- ggplot(d) +
aes(x = x, y = 1, color = x) +
scale_color_distiller(palette = 'RdBu', direction = 1, limits = c(-1, 1))

expect_equal(layer_data(p)$colour, c("#B2182B", "#F7F7F7", "#2166AC"))

p <- ggplot(d) +
aes(x = x, y = 1, fill = x) +
scale_fill_distiller(palette = 'RdBu', direction = 1, limits = c(-1, 1))

expect_equal(layer_data(p)$fill, c("#B2182B", "#F7F7F7", "#2166AC"))
})