Skip to content

Commit

Permalink
Add midpoint specification
Browse files Browse the repository at this point in the history
This appears to do the trick - I used a tip from tidyverse/ggplot2#3738 (comment)

@gritzenthaler would you mind testing this out to make sure I didn't miss anything?
  • Loading branch information
dlcomeaux committed Oct 16, 2020
1 parent cf7c5b7 commit bbdbd8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
28 changes: 23 additions & 5 deletions R/palettes_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,23 @@ cmap_pal_continuous <- function(palette = "seq_reds", reverse = FALSE) {
}
return(grDevices::colorRampPalette(pal))
}

#' internal helper function to rescale. Credit for idea is due to ijlyttle:
# \url{https://github.com/tidyverse/ggplot2/issues/3738#issuecomment-583750802}
#' @noRd
mid_rescaler2 <- function(mid) {
function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) {
scales::rescale_mid(x, to, from, mid)
}
}

#' Apply continuous CMAP palettes to ggplot2 aesthetics
#'
#' Pick the function depending on the aesthetic of your ggplot object (fill or color)
#'
#' @param palette Choose from 'cmap_gradients' list
#' @param reverse Logical; reverse color order?
#' @param middle Numeric, sets midpoint for diverging color palettes. Default =
#' 0.
#'
#' @examples
#' library(dplyr)
Expand All @@ -121,17 +130,26 @@ cmap_pal_continuous <- function(palette = "seq_reds", reverse = FALSE) {
#'
#' @describeIn cmap_fill_continuous For fill aesthetic
#' @export
cmap_fill_continuous <- function(palette = "seq_reds", reverse = FALSE) {
cmap_fill_continuous <- function(palette = "seq_reds",
reverse = FALSE,
middle = 0) {
ggplot2::scale_fill_gradientn(
colours = cmap_pal_continuous(palette, reverse = reverse)(256)
colours = cmap_pal_continuous(palette, reverse = reverse)(256),
rescaler = mid_rescaler2(middle)
)
}




#' @describeIn cmap_fill_continuous For color aesthetic
#' @export
cmap_color_continuous <- function(palette = "seq_reds", reverse = FALSE) {
cmap_color_continuous <- function(palette = "seq_reds",
reverse = FALSE,
middle = 0) {
ggplot2::scale_colour_gradientn(
colours = cmap_pal_continuous(palette, reverse = reverse)(256)
colours = cmap_pal_continuous(palette, reverse = reverse)(256),
rescaler = mid_rescaler2(middle)
)
}

Expand Down
9 changes: 6 additions & 3 deletions man/cmap_fill_continuous.Rd

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

1 comment on commit bbdbd8c

@gritzenthaler
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi Daniel, this seems to work very well for the color aesthetic. I'm struggling to come up with a good non-map example for testing this in the fill aesthetic, but I have no doubt it would work since the code structures are identical.

Slightly off-topic, this reminds me of a problem I ran into while working on something else this week. I was trying to apply a continuous color palette to a choropleth map with defined classification bins, and ended up using the Viridis package because its fill function has a discrete=TRUE option. Maps are beyond the scope of this project, but perhaps someone might have a graphical need for displaying a continuous palette in binned form? Do you think it's worth taking the time to add a "discrete" argument to the functions? @matthewstern what do you think? (The Viridis source code is here for reference: https://github.com/sjmgarnier/viridis/blob/master/R/scales.R. I'm poking around in it now.)

Please sign in to comment.