Skip to content

scale_color_distiller with divergent palette does not produce mid-color correct #3072

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

Closed
yosukefk opened this issue Jan 19, 2019 · 5 comments
Labels
bug an unexpected problem or unintended behavior tidy-dev-day 🤓 Tidyverse Developer Day

Comments

@yosukefk
Copy link

When scale color distiller is used with divergent color palette, color for mid point does not have the mid point specified by http://colorbrewer2.org. For example, RdBu uses #f7f7f7 for mid color when number of divisions are odd. But with distiller, I am getting #e9e0db which has slight pink shade. I believe it should give neutral color for the mid point. Fix is very easy, change the argument for brewer_pal generated function from 6 to 7. https://github.com/tidyverse/ggplot2/blob/master/R/scale-brewer.r#L87

library(ggplot2)
library(scales)

v <- -1:1
dat <- data.frame(x = v, y = rep(0,3))

# normal way to make plot
p <- ggplot(dat) + geom_point(aes(x=x,y=y,color=v), size=12) +
 scale_color_distiller(palette='RdBu',direction=1, limits=c(-1,1))
# this gives mid point (0) with pink shade
p

# took code from here, 
# https://github.com/tidyverse/ggplot2/blob/master/R/scale-brewer.r#L87-L88
# changed to 7 color per palette, instead of 6
library(scales)
my.scale_colour_distiller <- function(..., type = "seq", palette = 1, direction = -1, values = NULL, space = "Lab", na.value = "grey50", guide = "colourbar", aesthetics = "colour") {
  # warn about using a qualitative brewer palette to generate the gradient
  type <- match.arg(type, c("seq", "div", "qual"))
  if (type == "qual") {
    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)(7), 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
}

# try again with new function
p <- ggplot(dat) + geom_point(aes(x=x,y=y,color=v), size=12) +
 my.scale_colour_distiller(palette='RdBu',direction=1, limits=c(-1,1))
# gives correct color
p

Created on 2019-01-18 by the reprex package (v0.2.1)


@yosukefk yosukefk changed the title scale_color_brewer with divergent palette does not produce mid-color correct scale_color_distiller with divergent palette does not produce mid-color correct Jan 19, 2019
@clauswilke clauswilke added tidy-dev-day 🤓 Tidyverse Developer Day bug an unexpected problem or unintended behavior labels Jan 19, 2019
@clauswilke
Copy link
Member

Yes, I agree. Diverging color scales need an uneven number of colors before interpolations. This may be a good issue for the dev day today.

@dariyasydykova
Copy link
Contributor

I am claiming it for tidyverse dev day.

@karawoo
Copy link
Member

karawoo commented Jan 19, 2019

@dariyasydykova I forgot to mention that when you do a PR for this you should add a bullet to NEWS.md describing the change. It's probably worth adding a test as well. Come find me if you want help with that.

@yosukefk
Copy link
Author

Ow... this dev day was in my town should have known earlier! Have fun, all!

@lock
Copy link

lock bot commented Jul 18, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jul 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior tidy-dev-day 🤓 Tidyverse Developer Day
Projects
None yet
Development

No branches or pull requests

4 participants